Forum Discussion
hcze
Helper II
1 year agoLoop through a table using M
Hi all, I am trying to create function in Power Query to build a string based on values in a table. My table is Code Key Value Query 1 Key1 name Query 1 Key2 address Que...
- 1 year ago
This function meets your reqs I think.
CombineKeyValue
(InputTable as table, CodeValue as text) as text => [ TargetRows = Table.SelectRows( InputTable, each [Code] = CodeValue and Text.Trim([Value]) <> "" and [Value] <> null ), Parts = Table.TransformRows(TargetRows, each [Key] & "=" & [Value]), Output = Text.Combine(Parts, ",") ][Output]If we assume the name of your original table/query is "Original", then we can invoke like so:
Output:
AlienSx
Super User
1 year agomy_function = (tbl, code_value) => List.Accumulate(
Table.ToList(Table.SelectRows(tbl, (x) => x[Code] = code_value), (x) => x),
"",
(s, c) => s & (if c{2} is null then "" else "," & c{1} & "=" & c{2})
)