Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
dpbi
Helper I
Helper I

Compare two tables in power query

Hi.

Please advise how to get Power Query solution to the following:

I have tables ‘A’ and ‘B’.

I need to get table ‘C’.

 

 pica.png

 

 

 

 

 

 

 

 

 

 

 

Each row in table ‘A’  scans ( compare with )  each row in table ‘B’ and count matching items.

Basically, I need to add to table ‘A’ ,  4 custom columns for the 4 possible outcomes.

(In both tables, each letter can appear only once in a row and the order doesn’t matter).

 

For example , the calculation for the first row is done as follows:picb.png

 

 

 

 

 

 

 

With DAX I can do it by tranposing the rows (using Union and Row) and count the number of rows of the intersection (using Intersect), however, I need Power Query solution.

 

Best egards

Dan.

2 ACCEPTED SOLUTIONS
Zubair_Muhammad
Community Champion
Community Champion

@dpbi

 

Hi,

 

You can try this one.

Please see attached file with your sample data

 

let
    Source = Table.NestedJoin(#"Table A",{"Custom"},#"Table B",{"Custom"},"Table B",JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(Source, "Custom.1", each Table.SelectColumns([Table B],{"Col1","Col2","Col3"})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Table B"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns", "Custom", each Table.ToList([Custom.1])),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"Custom.1"}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns1", "Custom"),
    #"Added Custom3" = Table.AddColumn(#"Expanded Custom", "Col1 match", each if Text.PositionOf([Custom],[Col1])>-1 then 1 else 0),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Col2 Match", each if Text.PositionOf([Custom],[Col2])>-1 then 1 else 0),
    #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Col3 Match", each if Text.PositionOf([Custom],[Col3])>-1 then 1 else 0),
    #"Added Custom6" = Table.AddColumn(#"Added Custom5", "All matches", each Text.From([Col1 match]+[Col2 Match]+[Col3 Match]) & " matches"),
    #"Removed Columns2" = Table.RemoveColumns(#"Added Custom6",{"Col1 match", "Col2 Match", "Col3 Match", "Custom"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns2", {"Index", "Col1", "Col2", "Col3", "All matches"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"All matches"]), "All matches", "Count"),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Index", "Col1", "Col2", "Col3", "0 matches", "1 matches", "2 matches"})
in
    #"Reordered Columns"

PQSep.png

 

 

View solution in original post

Hi @Zubair_Muhammad

 

Thank you very much for your kind and fast help.

Your solution works perfectly. exactly what I needed.

Thank you also for the uploaded file, it helped me understand the steps in your solution.

As always , one of the best features in Power Bi Desktop is this forum.

 

Best regards

Dan.

View solution in original post

2 REPLIES 2
Zubair_Muhammad
Community Champion
Community Champion

@dpbi

 

Hi,

 

You can try this one.

Please see attached file with your sample data

 

let
    Source = Table.NestedJoin(#"Table A",{"Custom"},#"Table B",{"Custom"},"Table B",JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(Source, "Custom.1", each Table.SelectColumns([Table B],{"Col1","Col2","Col3"})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Table B"}),
    #"Added Custom2" = Table.AddColumn(#"Removed Columns", "Custom", each Table.ToList([Custom.1])),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"Custom.1"}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns1", "Custom"),
    #"Added Custom3" = Table.AddColumn(#"Expanded Custom", "Col1 match", each if Text.PositionOf([Custom],[Col1])>-1 then 1 else 0),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "Col2 Match", each if Text.PositionOf([Custom],[Col2])>-1 then 1 else 0),
    #"Added Custom5" = Table.AddColumn(#"Added Custom4", "Col3 Match", each if Text.PositionOf([Custom],[Col3])>-1 then 1 else 0),
    #"Added Custom6" = Table.AddColumn(#"Added Custom5", "All matches", each Text.From([Col1 match]+[Col2 Match]+[Col3 Match]) & " matches"),
    #"Removed Columns2" = Table.RemoveColumns(#"Added Custom6",{"Col1 match", "Col2 Match", "Col3 Match", "Custom"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns2", {"Index", "Col1", "Col2", "Col3", "All matches"}, {{"Count", each Table.RowCount(_), type number}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[#"All matches"]), "All matches", "Count"),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"Index", "Col1", "Col2", "Col3", "0 matches", "1 matches", "2 matches"})
in
    #"Reordered Columns"

PQSep.png

 

 

Hi @Zubair_Muhammad

 

Thank you very much for your kind and fast help.

Your solution works perfectly. exactly what I needed.

Thank you also for the uploaded file, it helped me understand the steps in your solution.

As always , one of the best features in Power Bi Desktop is this forum.

 

Best regards

Dan.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.