I have tried to find solution and tried few thing on my own but I realized that I'm stuck and that I need help.
So first I will describe the table and then the problem.
I need to change columns based on some condition and based on comparison of other columns.
In real case there are many (50-100) columns and they often change names. I have solved how to get list of columns that needs to be transformed so for the purpose of this description I will simplify table and we will assume that columns stay same.
Table1
| Number1 | Number2 | Percent | A | B | C |
|---------|---------|---------|---|---|---|
| | | | | | |
| | | | | | |
I need to change number values in columns A, B, and C if condition is not met, if condition is meet they need to stay as they are.
The condition look like this:
if [Number1] = [Number2] then A else A * [Percent]
if [Number1] = [Number2] then B else B * [Percent]
if [Number1] = [Number2] then C else C * [Percent]
I have used code for specific (in this case 3) number of columns and it worked: (Power Query Transform a Column based on Another Column)
=Table.FromRecords(
Table.TransformRows(
Table1,
(r) =>
Record.TransformFields(
r,
{
{"Column A", each if [Number1] = [Number2] then _ else _ * [Percent]},
{"Column B", each if [Number1] = [Number2] then _ else _ * [Percent]},
{"Column C", each if [Number1] = [Number2] then _ else _ * [Percent]}
}
)
),
Value.Type(Table1)
)
But as I have many columns I would like to feed Record.TransformFields
with a generated list.
Is there a way to use List.Transform
to generate a list transformOperationsfor Record.TransformFields
?
Something like this:
=Table.FromRecords(
Table.TransformRows(
Table1,
(r) =>
Record.TransformFields(
r,
{
List.Transform(
{"A", "B"},
each "{"""& _ & """" & ", each if [Number1] = [Number2] then _ else _ * [Percent])}"
)
}
)
),
Value.Type(Table1)
)
I also tried with Table.TransformColumns
but it didn't work:
= Table.TransformColumns(Table1,List.Transform(
{"A", "B"},
each "{"""& _ & """" & ", each if [Number1] = [Number2] then _ else _ * [Percent])}"
)),
I'm guessing that I would need to use List.Accumulate but not figured out yet.