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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
LarsAustin
Helper I
Helper I

Merge Two Tables Based on Two-Columns To Keep All Unique Records/Items From Both Table

Hi,

 

I was trying to merge an Actuals table to a Standard table based on columns Product and Component using a Left Outer join. Below is the M code from the Advanced Editor:

let
Source = Table.NestedJoin(Actual, {"Product", "Component"}, Standard, {"Product", "Component"}, "Standard", JoinKind.LeftOuter),
#"Expanded Standard" = Table.ExpandTableColumn(Source, "Standard", {"Standard"}, {"Standard"})
in
#"Expanded Standard"

 

LarsAustin_0-1658983459561.png

 

LarsAustin_1-1658983498144.png

 

This is the ouput that I get as expected:

 

LarsAustin_2-1658983549582.png

 

But my desired output is the table below:

 

LarsAustin_3-1658983664566.png

 

I want to be able to capture one of the items in the standard that is missing from the actual.

 

I tried using different merge types but it is not giving me the exact result i wanted.

 

I will appreciate your inputs to make this work.

 

Thank you

 

Jojemar

1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this code

let
    ProductsList = List.Distinct(Actual[Product]),
    #"Merged Queries" = Table.NestedJoin(Actual, {"Product", "Component"}, Standard, {"Product", "Component"}, "Standard.1", JoinKind.LeftOuter),
    #"Expanded Standard.1" = Table.ExpandTableColumn(#"Merged Queries", "Standard.1", {"Standard"}, {"Standard"}),
    #"Appended Query" = Table.Combine({#"Expanded Standard.1", Standard}),
    #"Added Custom" = Table.AddColumn(#"Appended Query", "Custom", each List.Contains(ProductsList,[Product])),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns", {"Product", "Component"})
in
    #"Removed Duplicates"

View solution in original post

7 REPLIES 7
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this code

let
    ProductsList = List.Distinct(Actual[Product]),
    #"Merged Queries" = Table.NestedJoin(Actual, {"Product", "Component"}, Standard, {"Product", "Component"}, "Standard.1", JoinKind.LeftOuter),
    #"Expanded Standard.1" = Table.ExpandTableColumn(#"Merged Queries", "Standard.1", {"Standard"}, {"Standard"}),
    #"Appended Query" = Table.Combine({#"Expanded Standard.1", Standard}),
    #"Added Custom" = Table.AddColumn(#"Appended Query", "Custom", each List.Contains(ProductsList,[Product])),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns", {"Product", "Component"})
in
    #"Removed Duplicates"

Hi Vijay,

 

Slight change in the scenario. I added Work Oder column to my original Actual table as below:

 

LarsAustin_0-1658993735632.png

 

The Standard table stays the same.

 

Now, my desired output table will be something like this:

 

LarsAustin_1-1658993837580.png

What needs to be change in the M code?

 

Thanks so much.

 

Jojemar

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Insert an Index column in Actual table. Index column is required to preserve sort order in result.

Use following code

let
    ProductsList = List.Distinct(Actual[Product]),
    #"Merged Queries" = Table.NestedJoin(Actual, {"Product", "Component"}, Standard, {"Product", "Component"}, "Standard.1", JoinKind.LeftOuter),
    #"Expanded Standard.1" = Table.ExpandTableColumn(#"Merged Queries", "Standard.1", {"Standard"}, {"Standard"}),
    #"Appended Query" = Table.Combine({#"Expanded Standard.1", Standard}),
    #"Added Custom" = Table.AddColumn(#"Appended Query", "Custom", each List.Contains(ProductsList,[Product])),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns", {"Product", "Component", "Work Order"}),
    #"Filtered Rows1" = Table.SelectRows(#"Removed Duplicates", each ([Index] <> null)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows1",{{"Index", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns1",{"Product", "Component", "Actual", "Standard", "Work Order"})
in
    #"Reordered Columns"

Hi Vijay,

 

Thank you for the feedback. But it didn't provide the solution I wanted. Below is the end output based on the M code you have provided (I added an Index column on the Actual table as you indicated):

 

LarsAustin_0-1659044131295.png

 

It is missing component G for both works order that is in the Standard table but not on Actual table.

 

Thank you

 

Jojemar

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use this and also there is no need for Index column now

let
    ProductsList = List.Distinct(Actual[Product]),
    #"Merged Queries" = Table.NestedJoin(Actual, {"Product", "Component"}, Standard, {"Product", "Component"}, "Standard.1", JoinKind.LeftOuter),
    #"Expanded Standard.1" = Table.ExpandTableColumn(#"Merged Queries", "Standard.1", {"Standard"}, {"Standard"}),
    #"Appended Query" = Table.Combine({#"Expanded Standard.1", Standard}),
    #"Added Custom" = Table.AddColumn(#"Appended Query", "Custom", each List.Contains(ProductsList,[Product])),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Columns", {"Product", "Component", "Work Order"}),
    #"Removed Duplicates1" = Table.Distinct(#"Removed Duplicates", {"Product", "Component"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Duplicates1",{"Product", "Component", "Actual", "Standard", "Work Order"})
in
    #"Reordered Columns"

Hi Vijay,

 

Thanks again for exploring the solution for my problem. It is showing now the Component from the Standard that is missing from the Actuals, but it only shows the result for Work Oder AA1 and not in AA2. Below is the end output based on your amended M code:

 

LarsAustin_0-1659075326126.png

 

Thank you

 

Jojemar

Hi Vijay,

 

Amazing. Works perfectly. 

 

Thanks so much..

 

Jojemar 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors