Forum Discussion
Join 2 tables based on multiple conditions
- 6 years agoSo, you could create a column in both tables that concatenate the fields to join on?
- Caroline_206 years agoFrequent Visitor
I understand that I can use this on orderId + name but how does this help me with the part of timestamp between start and end?
- Greg_Deckler6 years agoCommunity ChampionThat's probably an issue for ImkeF or @edhauns
- ImkeF6 years agoCommunity Champion
Hi Caroline_20
you have to select the matching rows from Table1 for each row of Table2, so this is not going to be a fast query. To improve the speed a bit at least, you should buffer Table1 (see attached file)
You add a column with this code:
Table.SelectRows( Table1, (x) => x[orderId] = [orderId] and ( (x[start] < [timestamp] and x[end] > [timestamp]) or ( ( [timestamp] > x[start] and [timestamp] < ( x[end] + #duration(0,2,0,0))) and x[name] = "done") ))[ID]{0}x stands for Table1 and you don't need a prefix for Table2 (the keyword "each" in the autogenerated M-code creates the required syntax sugar for it).
[ID] looks up the ID-column of the filtered table and {0} selects the first element from it (M is zero-based indexed)
- Caroline_206 years agoFrequent Visitor
Hi Greg,
you were right. I managed to join the tables with calculated columns checking for my conditions and removing the rows that are useless.
e.g. column: timestamp - start. That column is filtered only for positive values.