Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

compare two tables and find the difference using power query

hello,    I have two excel tables table 1 and table 2. I want to compare both the tables using power query and find the difference in three forms 1. extra in table 1 2. extra in table 2 3. chang...
  • AlB's avatar
    5 years ago

    Anonymous 

    Place the following M code in a blank query to see the steps. See it all at work in the attached file.

    let
        addedT1_= List.Difference(Table1[ID], Table2[ID]),
        addedT2_= List.Difference(Table2[ID], Table1[ID]),
        T1_ = Table.AddColumn(Table.SelectRows(Table1, each List.Contains(addedT1_, [ID])), "Change description", each "Added in table 1", type text), 
        T2_ = Table.AddColumn(Table.SelectRows(Table2, each List.Contains(addedT2_, [ID])), "Change description", each "Added in table 2", type text),
        TChanges_ = Table.AddColumn(Table.SelectRows(Table.SelectRows(Table2, each not List.Contains(addedT2_, [ID])), each _ <> Table1{[ID = [ID]]}), "Change description", each "Change in table 2 compared to table 1", type text),
        res_ = Table.Combine({T1_, T2_, TChanges_})
    in
        res_

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers