I have a power query table that contains 52 columns. The first two columns contain percentage values for each row, and the remaining 50 columns contain numeric values. I'd like to use a transform function to apply a calculation to the numeric values which draws from the percentage values (see ApplyCalculation in code below), but I'm coming up empty. I'd have thought this would have been pretty simple, but I'm starting to think I won't be able to do this without creating 50 new columns, each calculating for each existing column. I'd like to avoid this if at all possible.
This is my current code (for ease I've reduced the number of numeric columns down to 3, rather than 50. I figure what works for 3 will work for 50).
let
// Define the custom function for the calculation
ApplyCalculation = (value, Percentage1, Percentage2) =>
if value > 1 then ((value - 1) * Percentage1) + 1
else 1 - ((1 - value) * Percentage2),
Source = #"Rates Table",
#"Merged Queries" = Table.NestedJoin(Source, {"MONTH"}, #"Contract Details", {"Month"}, "Contract Details", JoinKind.Inner),
#"Expanded Contract Details" = Table.ExpandTableColumn(#"Merged Queries", "Contract Details", {"Percentage 1", "Percentage 2"}, {"Percentage 1", "Percentage 2"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Contract Details",{"DNO", "MONTH", "DATE", "Percentage 1", "Percentage 2", "1", "2", "3"}),
// Apply the calculation to columns "1" to "50" with proper parameter values
CustomTransform = Table.AddColumn(#"Reordered Columns", "Calculated Columns", each Record.TransformFields(_, List.Transform(List.Transform({1..3}, each Text.From(_)), each [Name = _ & "_Calculated", Value = ApplyCalculation(_[Column1], [Percentage 1], [Percentage 2])])))
in
CustomTransform
The above code doesn't error, but fails to apply any calculation to the numbers. Checking the figures before and after the CustomTransform step confirms they haven't changed. I've tested the custome function in an isolated query with hardcoded values for the 'value', 'Percentage1' and 'Percentage2' parameters, but I don't know why it won't work here. I read something online about a similar issue being down to the function receiving an entire column rather than a single value - is that the issue? I'm at a loss.
The closest existing post I've found is here - Power Query Applying a Function Across Every Column - though I don't think this solution is suitable for me due to the fact I'm wanting to apply a custom function rather than a hardcoded calculation.
Thanks in advance.
