Forum Discussion
Replacing multiple values in multiple columns
- 8 years ago
If each translation table is for 1 column, then I would suggest to use Table.TransformColumns instead of Table.ReplaceValues.
The query below has a double loop:
1. Outer loop over the columns in which values must be replaced (in the example: Name and Surname)
with the translation tables to use (in the example TranslationTable1 and -2),
2. Inner loop over each entry in the translation list.
r1 = Transformspecs, r1{0} = Column name to transform, r1{1} = translation list
t1/t2 = the table that is being transformed
r2 = translation list: r2{0} = old value, r2{1} = new value.
let ColumnsToTransform = {"Name", "Surname"}, TranslationLists = List.Buffer({Table.ToRows(TranslationTable1),Table.ToRows(TranslationTable2)}), TransformSpecs = List.Buffer(List.Zip({ColumnsToTransform,TranslationLists})), Source = Table1, Replaced = List.Accumulate( TransformSpecs, Source, (t1,r1) => List.Accumulate( r1{1}, t1, (t2,r2) => Table.TransformColumns( t2, {r1{0}, each Replacer.ReplaceValue(_,r2{0},r2{1})} ) ) ), Result = Value.ReplaceType(Replaced,Value.Type(Source)) in Result
If each translation table is for 1 column, then I would suggest to use Table.TransformColumns instead of Table.ReplaceValues.
The query below has a double loop:
1. Outer loop over the columns in which values must be replaced (in the example: Name and Surname)
with the translation tables to use (in the example TranslationTable1 and -2),
2. Inner loop over each entry in the translation list.
r1 = Transformspecs, r1{0} = Column name to transform, r1{1} = translation list
t1/t2 = the table that is being transformed
r2 = translation list: r2{0} = old value, r2{1} = new value.
let
ColumnsToTransform = {"Name", "Surname"},
TranslationLists = List.Buffer({Table.ToRows(TranslationTable1),Table.ToRows(TranslationTable2)}),
TransformSpecs = List.Buffer(List.Zip({ColumnsToTransform,TranslationLists})),
Source = Table1,
Replaced =
List.Accumulate(
TransformSpecs,
Source,
(t1,r1) =>
List.Accumulate(
r1{1},
t1,
(t2,r2) =>
Table.TransformColumns(
t2,
{r1{0},
each Replacer.ReplaceValue(_,r2{0},r2{1})}
)
)
),
Result = Value.ReplaceType(Replaced,Value.Type(Source))
in
Result