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
Caroline_20
Frequent Visitor

Join 2 tables based on multiple conditions

Hi community,

 

I am fairly new to Power Query and can't figure out how to translate this sql into a power query. I can't do an sql query, as I transform the table once it is in Power BI and only then Table 1 is in the right form.

 

I have two tables that I want to join on several conditions order, timestamp and name.

 

Table1

ID

orderId

start

end

name

1

1452

20.01.20 16:51:00

20.01.20 21:59:00

running

2

1654

20.01.20

17:04:00

20.01.20

18:55:00

running

3

1452

20.01.20

21:59:00

20.01.20

23:55:00

done

 

Table2

id

orderId

timestamp

number

name

8

1452

20.01.20 16:55:00

233

 

9

1452

20.01.20 17:13:00

203

 

10

1654

20.01.20

18:27:00

423

 

11

1452

20.01.20 21:04:00

203

 

12

1452

20.01.20 22:30:00

265

 

13

1452

21.01.20 01:22:00

255

 

 

 

Table3 – joined

id

orderId

timestamp

number

Table2.ID

8

1452

20.01.20 17:01:00

233

1

9

1452

20.01.20 17:13:00

203

1

10

1654

20.01.20

18:27:00

423

2

11

1452

20.01.20 21:04:00

203

1

12

1452

20.01.20 22:30:00

265

3

13

1452

21.01.20 01:22:00

255

3

 

Within sql queries I would tried something along these lines. 

RIGHT JOIN table2 ON table1.orderId=table2.orderId WHERE table2.timestamp BETWEEN table1.start AND table1.end OR WHERE (table2.timestamp BETWEEN table1.start AND (DATEADD(HOUR, 2, table1.end) AND name=”done”

 

Can anybody help me figure this problem out within power query?

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

So, you could create a column in both tables that concatenate the fields to join on?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

So, you could create a column in both tables that concatenate the fields to join on?


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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.

 

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?

That's probably an issue for @ImkeF or @edhauns


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

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)

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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