I want to make changes to some of the columns in my query MAIN.
Here is some sample data (all columns are numerical):
ID | col1 | col2 | cola | colb
1 | 1 | 17 | 3 | 10
2 | 3 | 6 | 5 | 5
Another query called CHANGES has a list of the required changes (all text columns):
varnm | cndvr| cndvl
col1 | col2 | 16,17,18,19
cola | colb | 5
The change I am trying to do – for every row in table CHANGES:
find the columns named in varnm
and cndvr
– for every row check if the value in the cndvr
column exists among the list of values in cndvl
, the value in the varnm
column remains unchaged. If, on the other hand, the value in the cndvr
column does NOT exists among the list of values in cndvl
, the value in the varnm
column is changed to 9999.
So the expected result in MAIN look like this:
ID | col1 | col2 | cola | colb
1 | 1 | 17 | 9999 | 10
2 | 9999 | 6 | 5 | 5
I tried to do it by creating a function called REPFUNC:
(row as record, dt as table) as table =>
let
// Extract condition values
varnm = Record.Field(row, "varnm"), // Column to transform
cndvr = Record.Field(row, "cndvr"), // Column for condition
cndvl = Text.Split(T(Record.Field(row, "cndvl")), ","),
// Transform data based on the condition
TransformedData = Table.ReplaceValue(
dt,
each varnm,
each if List.Contains(cndvl, Text.From(cndvr)) then varnm else 999999,
Replacer.ReplaceValue,
{varnm}
)
in
TransformedData
I call the function in my MAIN query like this:
let
Source = ......,
// Process each row in CHANGES
ProcessedData = List.Accumulate(
Table.ToRecords(CHANGES),
Source,
(state, currentRow) => Function.Invoke(REPFUNC, {currentRow, state})
)
in
ProcessedData
The result: NOTHING CHANGES.
I’ve tried a long list of changes, all resulting in errors. The above code doesn’t throw any errors but as they say – “He who does nothing makes no mistakes”…