Forum Discussion
Anonymous
7 years agoNot applicable
Table Transformations for Dynamic Columns
Hi, Input: I have a table with changing #'s of columns. Process: I need to take the first two characters of these columns. So I need PQ to dynamically identify which columns to take the fir...
- 7 years ago
HI Anonymous
If I understand the problem correctly
Suppose in your above sample, you want to transform the Columns starting with V
for example multiply all values by 10 or divide them by 10 you can use something like this
let Source = Table.FromRows({{"New York", 23, 51, 732}, {"Chicago", 25, 421, 23}, {"Los Angeles", 632, 22, 423}}, {"City", "Value 1", "Value 2", "Column 3"}), ColumnsToTransform= List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "V")), ActionToPerform=List.Repeat({each _ *10},List.Count(ColumnsToTransform)), Transformed=Table.TransformColumns(Source,List.Zip({ColumnsToTransform,ActionToPerform})) in Transformed - 7 years ago
Please try this function:
(InputTable as table, ListOfColNames, TransformFunction as function) => let TransformFunctionList = List.Transform(ListOfColNames, (x) => {x, TransformFunction}), Transform = Table.TransformColumns(InputTable, TransformFunctionList) in TransformName it "fnTransformMany" and call it like so:
let Source = Table.FromRows({{"New York", "23", "51", "732"}, {"Chicago", "25", "421", "23"}, {"Los Angeles", "632", "22", "423"}}, {"City", "Value 1", "Value 2", "Column 3"}), ColumnsToRemove = List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "V")), Result = fnTransformMany(Source, ColumnsToRemove, each Text.Start(_, 2)) in Result
Greg_Deckler
7 years agoCommunity Champion
I do not but ImkeF might.
ImkeF
7 years agoCommunity Champion
Please try this function:
(InputTable as table, ListOfColNames, TransformFunction as function) =>
let
TransformFunctionList = List.Transform(ListOfColNames, (x) => {x, TransformFunction}),
Transform = Table.TransformColumns(InputTable, TransformFunctionList)
in
TransformName it "fnTransformMany" and call it like so:
let
Source = Table.FromRows({{"New York", "23", "51", "732"}, {"Chicago", "25", "421", "23"}, {"Los Angeles", "632", "22", "423"}}, {"City", "Value 1", "Value 2", "Column 3"}),
ColumnsToRemove = List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "V")),
Result = fnTransformMany(Source, ColumnsToRemove, each Text.Start(_, 2))
in
Result
- Anonymous7 years agoNot applicable
ImkeF, Zubair_Muhammad, Greg_Deckler - Thank you so much. You all are awesome and I really appreciate the help. Brilliant solutions!