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:
ronrsnfld
Super User
1 year agoPerhaps, if a null in the Value column = EOF, then:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCixNLapUMFTSUfJOrQRReYm5qUqxOmgyRkAqMSWlKLW4GEnSCKc2IzzaoHLGQEopNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Code = _t, Key = _t, Value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Key", type text}, {"Value", type text}}),
String = [a=#"Changed Type"[Key],
b=#"Changed Type"[Value],
c=List.Zip({a,b}),
d=List.Select(c, each not (_{1} = "")),
e=List.Transform(d, each _{0} & "=" & _{1}),
f=Text.Combine(e,", ")][f]
in
String
Source Table:
Results:
Key1=name, Key2=address, Key1=name, Key2=address