Forum Discussion
MyOx
2 years agoFrequent Visitor
Divide row A by row B for all column
Hello,
I trying to build an attrition table in power query (not Dax).
I have two table containing all my topics: one with the amount the remaining items (A), one with the maximum item (B).
I want, for each column to do A/B.
Here's an exemple:
| Topic | Row | Début | M1 | M2 | M3 | M4 | M5 |
| Topic 1 | A | 100 | 80 | 60 | 40 | 20 | 0 |
| Topic 2 | A | 100 | 85 | 70 | 55 | 40 | 25 |
| Topic | Row | Début | M1 | M2 | M3 | M4 | M5 |
| Topic 1 | B | 100 | 100 | 90 | 85 | 80 | 75 |
| Topic 2 | B | 100 | 100 | 80 | 60 | 40 | 20 |
The final table should look like this
| Topic | Row | M1 | M2 | M3 | M4 | M5 | |
| Topic 1 | Final Result (A/B) | 80% | 67% | 47% | 25% | 0% | |
| Topic 2 | Final Result (A/B) | 85% | 88% | 92% | 100% | 125% |
Concidering that I have a thousand rows (Topic 1 to Topic 1000) and a thousand column (M1 to M1000), how could I do that?
Note: So far I found that if both Table are merge, I can do this = Table.ReplaceValue(#"Added Index", each [M2], each [M2]/#"Added Index"[M2]{[Index] -1}, Replacer.ReplaceValue,{"M2"}) to get my answer for one column. Not enough but it'
Hello, MyOx
let tbl1 = your_table_one, tbl2 = your_table_two, topics = List.Buffer(tbl1[Topic]), names = List.Buffer(List.Skip(Table.ColumnNames(tbl1), 3)), rows_one = List.Buffer(Table.ToRows(Table.SelectColumns(tbl1, names))), rows_two = List.Buffer(Table.ToRows(Table.SelectColumns(tbl2, names))), positions = List.Positions(rows_one), tx = List.Transform( positions, (x) => [a = List.Zip({rows_one{x}, rows_two{x}}), b = {topics{x}} & List.Transform(a, each Value.Divide(_{0}, _{1}))][b] ), fnl = Table.FromRows(tx, {"Topic"} & names) in fnl
1 Reply
- AlienSxSuper User
Hello, MyOx
let tbl1 = your_table_one, tbl2 = your_table_two, topics = List.Buffer(tbl1[Topic]), names = List.Buffer(List.Skip(Table.ColumnNames(tbl1), 3)), rows_one = List.Buffer(Table.ToRows(Table.SelectColumns(tbl1, names))), rows_two = List.Buffer(Table.ToRows(Table.SelectColumns(tbl2, names))), positions = List.Positions(rows_one), tx = List.Transform( positions, (x) => [a = List.Zip({rows_one{x}, rows_two{x}}), b = {topics{x}} & List.Transform(a, each Value.Divide(_{0}, _{1}))][b] ), fnl = Table.FromRows(tx, {"Topic"} & names) in fnl