excel - Power Query: transform a column by multiplying by another column -
i want similar power query transform column based on column, i'm getting stuck on how modify syntax particular goal.
similar linked question, assume have following table:
table 1: column | column b | column c ------------------------------ 1 | 4 | 7 2 | 5 | 8 3 | 6 | 9
instead of changing value of column conditional on column b, want multiply values in multiple columns (column b , column c) in column , replace values in initial columns can following:
table 1: column | column b | column c ------------------------------ 1 | 4 | 7 2 | 10 | 16 3 | 18 | 27
is possible without using multiple sequences of table.addcolumn
followed table.removecolumns
?
i have tried table.transformcolumns
based on this, not been able syntax right achieve this.
table.transformcolumns
won't give column a
unless can index table, possible if columns have unique data.
table.transformrows
let build new rows whatever logic want:
let source = csv.document("column a,column b,column c 1,4,7 2,5,8 3,6,9"), promotedheaders = table.promoteheaders(source), changedtype = table.transformcolumntypes(promotedheaders,{{"column a", type number}, {"column b", type number}, {"column c", type number}}), multipliedrows = table.fromrecords(table.transformrows(changedtype, each [ column = [column a], column b = [column a] * [column b], column c = [column a] * [column c] ])) in multipliedrows
this works columns b , c, if need b through z might want fancier logic avoid repeating yourself.
edit: more general solution many columns use record.transformfields
on list of transforms column names except "column a"
.
let source = csv.document("column a,column b,column c,d,e,f 1,4,7,1,2,3 2,5,8,4,5,6 3,6,9,7,8,9"), promotedheaders = table.promoteheaders(source), changedtype = table.transformcolumntypes(promotedheaders,{{"column a", type number}, {"column b", type number}, {"column c", type number}, {"d", type number}, {"e", type number}, {"f", type number}}), multipliedrows = table.fromrecords(table.transformrows(changedtype, (row) => let columna = row[column a], othercolumns = list.removeitems(record.fieldnames(row), {"column a"}), transforms = list.transform(othercolumns, (name) => { name, (cell) => cell * columna }) in record.transformfields(row, transforms))) in multipliedrows
Comments
Post a Comment