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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Merge two tables with multiple join conditions in Power Query

Hi Team,

I have 2 tables as below (Table1 and Table2). I have to join these 2 tables with multiple joining criteria:

  1. Join on Transaction_ID from both the tables and
  2. Also, join on Table1.Transaction_Dt should be between Table2.Sale_From_Dt and Table2.Sale_To_Dt).

However, I'm able to do it in SQL (mentioned below). But then not able to do it using Power Query.

 

 

 

 

SELECT A.* FROM TABLE1 A
LEFT JOIN TABLE2 B ON A.Transaction_ID = B.Transaction_ID
     AND A.Transaction_Dt>=B.Sale_From_Dt AND A.Transaction_Dt<=B.Sale_To_Dt

 

 

 

 

Table1

Transaction_IDItem_NameTransaction_Dt
1001Pen21-May-22
1002Laptop6-Aug-22
1003Phone1-Nov-22
1004Case25-Apr-22
1005Wire19-Aug-22
1006Cake15-Mar-22
1007Icecream16-Mar-22
1008Book5-Nov-22

 

Table2

Transaction_IDAmountSale_From_DtSale_To_Dt
10055001-Aug-2230-Aug-22
10081201-Oct-2230-Oct-22
10073451-Mar-2230-Mar-22
10096431-Feb-2228-Feb-22
10029001-Oct-2230-Oct-22
100434981-Apr-2230-Apr-22
1001341-Mar-2230-Mar-22
10034561-Nov-2230-Nov-22
10053451-Jul-2230-Jul-22
10083451-Nov-2230-Nov-22
10079081-May-2230-May-22
10091251-Jan-2230-Jan-22
100237871-Aug-2230-Aug-22
10049871-Dec-2230-Dec-22
100176341-May-2230-May-22
10037601-Nov-2230-Nov-22

 

 Can someone please help me with the same?

@amitchandak - Any help here is much appriciated.

Thanks!!

2 ACCEPTED SOLUTIONS
v-chenwuz-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can join these tables firstly by merge queris.

vchenwuzmsft_0-1653889727056.png

And join kind with Left outer join.

vchenwuzmsft_1-1653889848594.png

expand all the columns expcet id column in the table2.

vchenwuzmsft_2-1653889933678.png

Add a custom column to identify if the date between table2's date range.

vchenwuzmsft_3-1653889993121.png

At last, filter all the row with TRUE and null. It means remove the rows which do not match conditions.

 

The M code:

let
    Source = Table.NestedJoin(Table1, {"Transaction_ID"}, Table2, {"Transaction_ID"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Amount", "Sale_From_Dt", "Sale_To_Dt"}, {"Table2.Amount", "Table2.Sale_From_Dt", "Table2.Sale_To_Dt"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "if between", each [Transaction_Dt]>=[Table2.Sale_From_Dt] and
[Transaction_Dt]<=[Table2.Sale_To_Dt]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([if between] <> false)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"if between"})
in
    #"Removed Columns"

Result:

vchenwuzmsft_4-1653890171518.png

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Anonymous
Not applicable

3 REPLIES 3
v-chenwuz-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can join these tables firstly by merge queris.

vchenwuzmsft_0-1653889727056.png

And join kind with Left outer join.

vchenwuzmsft_1-1653889848594.png

expand all the columns expcet id column in the table2.

vchenwuzmsft_2-1653889933678.png

Add a custom column to identify if the date between table2's date range.

vchenwuzmsft_3-1653889993121.png

At last, filter all the row with TRUE and null. It means remove the rows which do not match conditions.

 

The M code:

let
    Source = Table.NestedJoin(Table1, {"Transaction_ID"}, Table2, {"Transaction_ID"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Amount", "Sale_From_Dt", "Sale_To_Dt"}, {"Table2.Amount", "Table2.Sale_From_Dt", "Table2.Sale_To_Dt"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table2", "if between", each [Transaction_Dt]>=[Table2.Sale_From_Dt] and
[Transaction_Dt]<=[Table2.Sale_To_Dt]),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([if between] <> false)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"if between"})
in
    #"Removed Columns"

Result:

vchenwuzmsft_4-1653890171518.png

 

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I have been implementing a very similar problem, and your solution helps enormously, but it is unfortunately not entirely correct.

I use a LeftOuterJoin condition, which means I want to keep all records from Table 1.

 

The problem arises in this situation:

- there is a (at least 1) related record in Table 2

- all related records fail on the second condition ( Transaction_Dt between Sale_From_Dt and Sale_To_Dt)

In this situation we lose the original record from Table1, though I want to keep it with null values in the Table2 columns.

 

Any solution for this?

 

Anonymous
Not applicable

Thanks @v-chenwuz-msft - That helps!

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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