Hi All,
I have some M-Code to subtract column values from two tables. The Original table is from a template and starts without any applicable Date Header columns (the quantity of date columns along with their header names varies); it is populated by a macro that copies its values into the Copy table and then refreshes the queries. Upon refresh, the M-code runs perfectly and Delta is populated without errors. However, before the columns are created and populated, Power Query is throwing an error. In VBA, there is a way to skip multiple lines of code and offer an alternative line of code if an error is encountered. Does Power Query have some On Error, Goto Line capability? Or is there some other way to adjust the M-code to evaluate differently if an error is encountered?
Illustration of Data:
M-Code:
Ideally, upon encountering an error, I would like the M-Code to skip all subsequent lines of code and return Original instead (so default to whatever the Original table looks like). The error is first encountered at line UnpivotedOriginal (likely because DateHeaders is empty).
Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details:
I have some M-Code to subtract column values from two tables. The Original table is from a template and starts without any applicable Date Header columns (the quantity of date columns along with their header names varies); it is populated by a macro that copies its values into the Copy table and then refreshes the queries. Upon refresh, the M-code runs perfectly and Delta is populated without errors. However, before the columns are created and populated, Power Query is throwing an error. In VBA, there is a way to skip multiple lines of code and offer an alternative line of code if an error is encountered. Does Power Query have some On Error, Goto Line capability? Or is there some other way to adjust the M-code to evaluate differently if an error is encountered?
Illustration of Data:
M-Code:
Power Query:
let
Original = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
NonDateHeaders = List.FirstN(Table.ColumnNames(Original),50),
DateHeaders = List.Skip(Table.ColumnNames(Original),50),
ReplaceNulls = Table.ReplaceValue(Original,null,0,Replacer.ReplaceValue,DateHeaders),
Original1 = Table.AddIndexColumn(ReplaceNulls, "Index", 0, 1),
AllColumnHeaders = Table.ColumnNames(Original1),
ReorderedList = List.Combine({{"Index"},List.FirstN(AllColumnHeaders,List.Count(AllColumnHeaders)-1)}),
Original2 = Table.ReorderColumns(Original1,ReorderedList),
NonDateHeadersI = List.FirstN(Table.ColumnNames(Original2),51),
Copy = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
ReplaceNulls2 = Table.ReplaceValue(Copy,null,0,Replacer.ReplaceValue,DateHeaders),
Copy1 = Table.AddIndexColumn(ReplaceNulls2, "Index", 0, 1),
Copy2 = Table.ReorderColumns(Copy1,ReorderedList),
Destination = Table.SelectColumns(Original2, NonDateHeadersI),
UnpivotedOriginal = Table.UnpivotOtherColumns(Table.RemoveColumns(Original2, NonDateHeaders), {"Index"}, "Attribute", "Value"),
UnpivotedCopy = Table.UnpivotOtherColumns(Table.RemoveColumns(Copy2, NonDateHeaders), {"Index"}, "Attribute", "Value"),
Source = Table.NestedJoin(UnpivotedOriginal, {"Index", "Attribute"}, UnpivotedCopy, {"Index", "Attribute"}, "Copy", JoinKind.Inner),
ExpandCopy = Table.ExpandTableColumn(Source, "Copy", {"Value"}, {"Copy.Value"}),
AddDifference = Table.AddColumn(ExpandCopy, "Difference", each [Copy.Value]-[Value]),
RemovedColumns = Table.RemoveColumns(AddDifference,{"Value", "Copy.Value"}),
RepivotColumns = Table.Pivot(RemovedColumns, List.Distinct(RemovedColumns[Attribute]), "Attribute", "Difference", List.Sum),
MergeQueries = Table.NestedJoin(RepivotColumns, {"Index"},Destination,{"Index"},"Destination",JoinKind.Inner),
ExpandFinal = Table.ExpandTableColumn(MergeQueries, "Destination", NonDateHeaders, NonDateHeaders),
FinalRemoveColumns = Table.RemoveColumns(ExpandFinal, {"Index"}),
ReorderedList2 = List.Combine({NonDateHeaders, DateHeaders}),
FinalReorderColumns = Table.ReorderColumns(FinalRemoveColumns,ReorderedList2)
in
FinalReorderColumns
Ideally, upon encountering an error, I would like the M-Code to skip all subsequent lines of code and return Original instead (so default to whatever the Original table looks like). The error is first encountered at line UnpivotedOriginal (likely because DateHeaders is empty).
Expression.Error: There weren't enough elements in the enumeration to complete the operation.
Details: