Forum Discussion
Merge two tables with multiple join conditions in Power Query
- 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.
- Anonymous4 years ago
Thanks v-chenwuz-msft - That helps!
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.
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?