问题
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:
- Power query return error message that the 2 lines following the source line, are not recognized.
- 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