Power query & power pivot - empty table and clearing pivot

核能气质少年 提交于 2019-12-11 10:24:45

问题


Hope you could help. I have the below code in my power query and it is working great with a connected power pivot table. As soon as the Source line is returning "This table is empty" it all goes wrong:

  1. Power query return error message that the 2 lines following the source line, are not recognized.
  2. The table in the power pivot is showing the last results from the last working query.

I need the table to be empty if there are no results.

How do i do that?

let

    UrlSource = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(UrlSource,{{"Url", type text}}),
    Url = #"Changed Type"{0}[Url],

    UserInput = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Sagsnummer Type" = Table.TransformColumnTypes(UserInput,{{"Sagsnummer", type text}}),
    Sagsnummer = #"Changed Sagsnummer Type"{0}[Sagsnummer],

    Source = OData.Feed(Url & "/FileContacts?$select=CustomLabel_Summary,Name/Name1&$expand=Name&$filter=File/FileNo eq '" & Sagsnummer & "'"),
    #"Expanded Name" = Table.ExpandRecordColumn(Source, "Name", {"Name1"}, {"Name.Name1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Name",{{"CustomLabel_Summary", "Rolle"}, {"Name.Name1", "Kontakt"}})

in

    #"Renamed Columns"

回答1:


Add one last step that check that both UrlSource and UserInput have a row of input. If not, default to some empty table:

= if Table.RowCount(UrlSource) > 0 and Table.RowCount(UserInput) > 0 then #"Renamed Columns" else #table({"Rolle", "Kontakt"}, {})

All together, your code would look like

let

    UrlSource = Excel.CurrentWorkbook(){[Name="Table6"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(UrlSource,{{"Url", type text}}),
    Url = #"Changed Type"{0}[Url],

    UserInput = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Sagsnummer Type" = Table.TransformColumnTypes(UserInput,{{"Sagsnummer", type text}}),
    Sagsnummer = #"Changed Sagsnummer Type"{0}[Sagsnummer],

    Source = OData.Feed(Url & "/FileContacts?$select=CustomLabel_Summary,Name/Name1&$expand=Name&$filter=File/FileNo eq '" & Sagsnummer & "'"),
    #"Expanded Name" = Table.ExpandRecordColumn(Source, "Name", {"Name1"}, {"Name.Name1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Name",{{"CustomLabel_Summary", "Rolle"}, {"Name.Name1", "Kontakt"}}),

    Custom1 = if Table.RowCount(UrlSource) > 0 and Table.RowCount(UserInput) > 0 then #"Renamed Columns" else #table({"Rolle", "Kontakt"}, {})
in

    Custom1


来源:https://stackoverflow.com/questions/35533984/power-query-power-pivot-empty-table-and-clearing-pivot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!