I am using Power Query in Excel for Microsoft 365.
This post is a follow-on to this one:
Power Query: Combine Subset of List of Tables that Exist
The Power Query shown below is not the solution I ultimately went with in the post linked above, but it does present a problem I am trying to understand.
Consider this working Power Query (which is saved directly in my workbook):
// Combine the subset of tables Table1, Table2, Table3 that actually exist.
let
remove_errors_f = (input_list as list) as list =>
List.RemoveNulls(
List.Transform(
List.Positions(input_list),
each try input_list{_} otherwise null
)
),
combiner_f = (AccumulatedTable as table, TablesToCombine as list) as table =>
if List.Count(TablesToCombine) = 0 then
AccumulatedTable
else
@combiner_f(Table.Combine({AccumulatedTable, TablesToCombine{0}}), List.RemoveFirstN(TablesToCombine, 1)),
#"MyTables" = remove_errors_f({Table1, Table2, Table3}),
#"All Column Names" = {"Column1", "Column2", "Column3"},
Source = combiner_f(#table(#"All Column Names", {}), #"MyTables"),
#"Source Sorted" = Table.Sort(Source, List.Transform(Table.ColumnNames(Source), each {_, Order.Ascending}))
in
#"Source Sorted"
Now, suppose I move this Power Query out of my .xlsx
file and move it character-for-character verbatim into a text file named combine_worksheet_subset.pq
. (This is for the purpose of version control.)
I then create a "stub" Power Query directly in my workbook to load and execute combine_worksheet_subset.pq
:
let
// The source must be an absolute path
Source = Text.FromBinary(File.Contents("C:\my_path\combine_worksheet_subset.pq")),
EvaluatedExpression = Expression.Evaluate(Source, #shared)
in
EvaluatedExpression
This works fine as long as all of Table1
, Table2
, and Table3
exist. However, it fails if any of these tables are not present. For example, if I remove Table2
, I get the following error:
Why does this Power Query have this failure case when it is loaded from an external file (but not if it's stored directly in the workbook)?