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: Join on Transaction_ID from both the tables and Also, join on Table1.Transacti...
  • v-chenwuz-msft's avatar
    4 years ago

    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.

  • Anonymous's avatar
    Anonymous
    4 years ago

    Thanks v-chenwuz-msft - That helps!