Forum Discussion
vc25
7 months agoHelper I
how to dynamically replace values for multiple columns
In power query, I previously split columns by a delimiter, so some of the columns in my query start the same "mt" with variations like "mt1" "mt2" mt3" etc. Each "mt" column is datatype text and has ...
- 6 months ago
ronrsnfld
6 months agoSuper User
You can do this using the Table.ReplaceValue function with a custom replace function.
First as others have recommended, create a Mapping Table.
For example:
You can then use this code:
let
//Replace next line with your actual data source of the splitted table
Source = Table,
//create list of columns to evaluate
Cols = List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "mt")),
//Lookup the Key
//Assumes Mapping Table is a two column table the columns named "State" and "Key"
Mapping = List.Buffer(Table.ToRecords(#"Mapping Table")),
//Execute the replacements
#"Replace with State" = Table.ReplaceValue(
Source,
null,
Mapping,
(x,y,z)=>if x=null then null else List.Select(z, each [Key]=Number.From(x)){0}[State] ,
Cols)
in
#"Replace with State"
With an initial "split" table that looks like:
After running the code: