Forum Discussion

hcze's avatar
hcze
Icon for Helper II rankHelper II
1 year ago
Solved

Loop 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...
  • MarkLaf's avatar
    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: