Forum Discussion
Replace values in different columns using a table (tblBeforeAfter)
- 4 years ago
Your table approach for replacement word is the way to go.
In the below code, I am replacing it in all the columns of the table through
ColumnList = Table.ColumnNames(Source)
But if you need to do in selected columns, then just specify the column names in this ColumList. (You can once again make a table which you can convert to list)
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxKVtJRikwtBpJA5JtYmZQKpFPSUoAkmPDLB0nF6kQrpQEZRWBZNOyXn5cKNQWkDqwGrA0kWQTE/mkKzvmlRcWpYBPA8kCyCGIeSAvEATDbwXqR9UC0QRhKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]), ColumnList = Table.ColumnNames(Source), ChangeValues = Table.FromRecords(Table.TransformRows(Source, (r) => List.Accumulate(Table.ColumnNames(Source), r, (s,c)=> Record.TransformFields(s,{{c, each try tblBeforeAfterLight {[Before=_]}[After] otherwise _}}))) , Value.Type(Source)) in ChangeValues
1.- First transpose your table of names and promote headers so you only have one row.
let
Source = Excel.CurrentWorkbook(){[Name="Table_Names"]}[Content],
#"Transposed Table" = Table.Transpose(Source),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true])
in
#"Promoted Headers"
2.- then add this M code in the formula bar
Table.TransformColumnNames( Source, each try Table.Column(Table_Names,_){0} otherwise _)
let
Source = Excel.CurrentWorkbook(){[Name="Table_Data"]}[Content],
Custom1 = Table.TransformColumnNames( Source, each try Table.Column(Table_Names,_){0} otherwise _)
in
Custom1