Search code examples
powerquerym

Compare 2 tables with Delimited values in ID column


I have 2 tables loaded into Power Query that contain a group of IDs within a column (Col1) delimited by comma. For example Table 1 Grouped_ID can be "123456, 654454, 34565" but in table 2 IDGROUP can be "654454, 123456, 34565".

What I am trying to do is for Each Row in Table1, split and list all of the IDs and then iterate over all of the IDGROUP from table 2. and check to see if all of the delimited IDs match if so Return a value from Table 2.

What I have tried is to split each list and then check for a match but it is not providing a positive outcome. Is there anyone who could offer some advice?

let
            idList1 = Text.Split([FAC_Group_ID], ", "),
            idList2 = Table2[FAC_Group_ID],
            idList2Split = List.Transform(idList2, each Text.Split(_, ", ")),
            matches = List.Transform(idList2Split, each List.Sort(_) = List.Sort(idList1)),
            allMatches = List.AllTrue(matches)
        in
            if allMatches then "Match" else "No Match"

Table 1 - Table 1

Table 2 - Table 2

Result enter image description here


Solution

  • For each table, calculate a sorted list

    = Text.Combine(List.Sort(Text.Split([Column1],", ")),",")
    

    Then check one list against another

    let  Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Check" = List.Buffer(Table.AddColumn(Table1, "Check", each Text.Combine(List.Sort(Text.Split([ID GROUP],", ")),","))[Check]),
    #"Added Custom" = Table.AddColumn(Source, "Result", each if List.Contains(Check,Text.Combine(List.Sort(Text.Split([Grouped_ID],", ")),",")) then "Match" else "No Match")
    in #"Added Custom"
    

    enter image description here