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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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
Super User
Super User

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

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

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors