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
Anonymous
Not applicable

Joint 3 tables with 2 relationship

Hi, I have 3 tables:

 

1. Matching indicator

Ref No.1Ref No.2
C0001A0001
C0001A0002
C0002A0003

 

2. Table small

SKU numberRef No.2Qty
1022-00200-5118-34-10A00011
1022-00200-5118-36-10A00022
1022-00200-5118-34-10A00033

 

3. Table big

SKUQtyRef No.1
1022-00200-5118-34-101C0001
1022-00200-5118-36-102C0001
1022-00200-5118-34-104C0002

 

 

How can I join them together and come up with table like this:

Ref No.1Ref No.2SKU numberSmallBig
C0001A00011022-00200-5118-34-1011
C0001A00021022-00200-5118-36-1022
C0002A00031022-00200-5118-34-1034

 

I tried to make like this:

 

I want to make like this:

2019_11_28_15_22_06_Untitled_Power_BI_Desktop.png

 

But I got warning:

2019_11_28_15_23_20_Edit_relationship.png

 

May I know how to achieve that?

 

Thanks

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

We can get a merged table in power query as below.

Capture.PNG

M code for your reference.

let
    Source = Table.NestedJoin(Big, {"SKU"}, Small, {"SKU"}, "Small", JoinKind.FullOuter),
    #"Expanded Small" = Table.ExpandTableColumn(Source, "Small", {"SKU", "Ref No.2", "Qty"}, {"Small.SKU", "Small.Ref No.2", "Small.Qty"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Small", {"Small.Ref No.2"}),
    #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each if [SKU] = null then [Small.SKU] else [SKU]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"SKU"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Qty", "Ref No.1", "Small.SKU", "Small.Ref No.2", "Small.Qty"}),
    #"Merged Queries" = Table.NestedJoin(#"Reordered Columns", {"Small.Ref No.2"}, #"Matching indicator", {"Ref No.2"}, "Matching indicator", JoinKind.LeftOuter),
    #"Expanded Matching indicator" = Table.ExpandTableColumn(#"Merged Queries", "Matching indicator", {"Ref No.1"}, {"Matching indicator.Ref No.1"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Matching indicator", "Custom.1", each if [Matching indicator.Ref No.1] = null then [Ref No.1] else [Matching indicator.Ref No.1]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Matching indicator.Ref No.1", "Ref No.1"}),
    #"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns1",{"Custom", "Custom.1", "Small.Ref No.2", "Qty", "Small.SKU", "Small.Qty"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Reordered Columns1",{"Qty", "Small.SKU", "Small.Qty"}),
    #"Merged Queries1" = Table.NestedJoin(#"Removed Columns2", {"Custom", "Small.Ref No.2"}, Small, {"SKU", "Ref No.2"}, "Small", JoinKind.LeftOuter),
    #"Expanded Small1" = Table.ExpandTableColumn(#"Merged Queries1", "Small", {"Qty"}, {"Small.Qty"}),
    #"Merged Queries2" = Table.NestedJoin(#"Expanded Small1", {"Custom", "Custom.1"}, Big, {"SKU", "Ref No.1"}, "Big", JoinKind.LeftOuter),
    #"Expanded Big" = Table.ExpandTableColumn(#"Merged Queries2", "Big", {"Qty"}, {"Big.Qty"})
in
    #"Expanded Big"

 

 

Pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

5 REPLIES 5
Thim
Resolver V
Resolver V

instead of setting up relations to each table, you could go under Quiry Editor and Merge the tables.

 

I would use table 2 as base, since this is the table that has relations to the other 2 tables.

 

Click Quiry Editor (Edit Quirres)

Then, on the left side, click on table 2

Then on top right, there is a button called "Merge Queries" Click this.

For table one choose the field "Ref No.2" for both table as the matchin coloumn.

For the three, choose the field "SKU" for both table as the matchin coloumn.

 

Then you can just expand the tables, and all is now collected on table 2.

 

Hope this helps. 🙂

 
Anonymous
Not applicable

Hi @Thim  Thans,

 

After merge, I should have 5 columns,right?

 

However I dont see it after save & close?

 

May I know how to use the merged table?

 

Anonymous
Not applicable

@Thim  One more question, does this merging achieve following situation as well?

 

1. Matching indicator

Ref No.1Ref No.2
C0001A0001
C0001A0002
C0002A0003
C0002A0004

 

2. Table small

SKU numberRef No.2Qty
1022-00200-5118-34-10A00011
1022-00200-5118-36-10A00022
1022-00200-5118-34-10A00033
1022-00200-5118-35-10A00042

 

3. Table big

SKUQtyRef No.1
1022-00200-5118-34-101C0001
1022-00200-5118-36-102C0001
1022-00200-5118-34-104C0002
1022-00200-5118-37-102C0002

 

 

How can I join them together and come up with table like this:

Ref No.1Ref No.2SKU numberSmallBig
C0001A00011022-00200-5118-34-1011
C0001A00021022-00200-5118-36-1022
C0002A00031022-00200-5118-34-1034
C0002A00041022-00200-5118-35-102 
C0002 1022-00200-5118-37-10 2

 

 

Thanks a lot!

Hi @Anonymous ,

 

We can get a merged table in power query as below.

Capture.PNG

M code for your reference.

let
    Source = Table.NestedJoin(Big, {"SKU"}, Small, {"SKU"}, "Small", JoinKind.FullOuter),
    #"Expanded Small" = Table.ExpandTableColumn(Source, "Small", {"SKU", "Ref No.2", "Qty"}, {"Small.SKU", "Small.Ref No.2", "Small.Qty"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Small", {"Small.Ref No.2"}),
    #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "Custom", each if [SKU] = null then [Small.SKU] else [SKU]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"SKU"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Custom", "Qty", "Ref No.1", "Small.SKU", "Small.Ref No.2", "Small.Qty"}),
    #"Merged Queries" = Table.NestedJoin(#"Reordered Columns", {"Small.Ref No.2"}, #"Matching indicator", {"Ref No.2"}, "Matching indicator", JoinKind.LeftOuter),
    #"Expanded Matching indicator" = Table.ExpandTableColumn(#"Merged Queries", "Matching indicator", {"Ref No.1"}, {"Matching indicator.Ref No.1"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Matching indicator", "Custom.1", each if [Matching indicator.Ref No.1] = null then [Ref No.1] else [Matching indicator.Ref No.1]),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Matching indicator.Ref No.1", "Ref No.1"}),
    #"Reordered Columns1" = Table.ReorderColumns(#"Removed Columns1",{"Custom", "Custom.1", "Small.Ref No.2", "Qty", "Small.SKU", "Small.Qty"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Reordered Columns1",{"Qty", "Small.SKU", "Small.Qty"}),
    #"Merged Queries1" = Table.NestedJoin(#"Removed Columns2", {"Custom", "Small.Ref No.2"}, Small, {"SKU", "Ref No.2"}, "Small", JoinKind.LeftOuter),
    #"Expanded Small1" = Table.ExpandTableColumn(#"Merged Queries1", "Small", {"Qty"}, {"Small.Qty"}),
    #"Merged Queries2" = Table.NestedJoin(#"Expanded Small1", {"Custom", "Custom.1"}, Big, {"SKU", "Ref No.1"}, "Big", JoinKind.LeftOuter),
    #"Expanded Big" = Table.ExpandTableColumn(#"Merged Queries2", "Big", {"Qty"}, {"Big.Qty"})
in
    #"Expanded Big"

 

 

Pbix as attached.

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

@v-frfei-msft  Hi Frank, Thanks for your answer.

I was replicating your solution and found that Im too simplified the questions.

Reason is that in your answer, you remove the duplicate item in "Small.Ref No.2"

However in the real data

1. each "Small.Ref No.2" has multiple products as well,

2. One "Ref No.1", may contain different "Ref No.2", with same products 

 

Is Power BI able to do that?

 

Thanks a lot

 

 

1. Matching indicator

Ref No.1Ref No.2
C0001A0001
C0001A0002
C0002A0003
C0002A0004

 

2. Table small

SKU numberRef No.2Qty
1022-00200-5118-34-10A00011
1022-00200-5118-36-10A00012
1022-00200-5118-36-10A00022
1022-00200-5118-34-10A00033
1022-00200-5118-35-10A00042

 

3. Table big

SKUQtyRef No.1
1022-00200-5118-34-101C0001
1022-00200-5118-36-104C0001
1022-00200-5118-34-104C0002
1022-00200-5118-37-102C0002

 

 

Ideal Result:

Ref No.1Ref No.2SKU numberSmallBig
C0001A00011022-00200-5118-34-1011
C0001A00011022-00200-5118-36-1022
C0001A00021022-00200-5118-36-1022
C0002A00031022-00200-5118-34-1034
C0002A00041022-00200-5118-35-102 
C0002 1022-00200-5118-37-10 2

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.