Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

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!!

  • Hi Anonymous ,

     

    You can join these tables firstly by merge queris.

    And join kind with Left outer join.

    expand all the columns expcet id column in the table2.

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

    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:

     

    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.

3 Replies

  • v-chenwuz-msft's avatar
    v-chenwuz-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    You can join these tables firstly by merge queris.

    And join kind with Left outer join.

    expand all the columns expcet id column in the table2.

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

    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:

     

    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.

    • mb769's avatar
      mb769
      Icon for Advocate I rankAdvocate I

      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?