cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dpbi Regular Visitor
Regular Visitor

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

Accepted Solutions
Super User
Super User

Re: Compare two tables in power query

@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

 

 

dpbi Regular Visitor
Regular Visitor

Re: Compare two tables in power query

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.

2 REPLIES 2
Super User
Super User

Re: Compare two tables in power query

@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

 

 

dpbi Regular Visitor
Regular Visitor

Re: Compare two tables in power query

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
Back to School Contest

Back to School Contest

Engage and empower students with Power BI!

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Summit Australia 2019

Summit Australia 2019

Travel to Melbourne and network with thousands of peers!

PBI Community Highlights

PBI Community Highlights

Check out what's new in the Power BI Community!

Top Ideas
Users Online
Currently online: 289 members 3,060 guests
Please welcome our newest community members: