Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Tgilchrist
Frequent Visitor

Joining two tables where TABLE1.Datetime is between TABLE2.StartDatetime and TABLE2.EndDatetime

I have two tables TABLE1 and TABLE2.

Currently there are no relationships between them but TABLE1 contains the Datetime of an event and TABLE2 contains a StartDatetime and EndDatetime for each shift. I would like to merge the two tables based on if the Datetime in TABLE1 is inbetween the StartDatetime and EndDatetime of TABLE2. 

 

In Qlik Sense it is wrote as...

Left Join (TABLE1)

IntervalMatch(DateTime)

Load StartDatetime, EndDatetime

Resident TABLE2;

 

Example Data...

 

TABLE1:

Event Code

Datetime

001

5/1/2020 2:40:00 PM

088

5/1/2020 6:20:00 PM

003

5/1/2020 11:11:00 AM

006

5/2/2020 4:10:00 PM

088

5/3/2020 8:33:00 AM

 

TABLE2:

StartDateTime

EndDateTime

5/1/2020 6:30:00 AM

5/1/2020 2:29:59 PM

5/1/2020 2:30:00 PM

5/1/2020 10:29:59 PM 

5/1/2020 10:30:00 PM

5/2/2020 6:29:59 AM 

5/2/2020 6:30:00 AM

5/2/2020 2:29:59 PM 

5/2/2020 2:30:00 PM

5/2/2020 10:29:59 PM 

5/2/2020 10:30:00 PM

5/3/2020 6:29:59 AM 

5/3/2020 6:30:00 AM

5/3/2020 2:29:59 PM 

5/3/2020 2:30:00 PM

5/3/2020 10:29:59 PM 

5/3/2020 10:30:00 PM

5/4/2020 6:29:59 AM 

5/4/2020 6:30:00 AM

5/4/2020 2:29:59 PM 

5/4/2020 2:30:00 PM

5/4/2020 10:29:59 PM 

5/4/2020 10:30:00 PM

5/5/2020 6:29:59 AM 

 

Desired result:

Event Code

Datetime

StartDatetime

EndDatetime

001

5/1/2020 2:40:00 PM

5/1/2020 2:30:00 PM

5/1/2020 10:29:59 PM 

088

5/1/2020 6:20:00 PM

5/1/2020 2:30:00 PM

5/1/2020 10:29:59 PM 

003

5/1/2020 11:11:00 AM

5/1/2020 6:30:00 PM

5/1/2020 2:29:59 AM 

006

5/2/2020 4:10:00 PM

5/2/2020 2:30:00 PM

5/2/2020 10:29:59 PM 

088

5/3/2020 8:33:00 AM

5/3/2020 6:30:00 AM

5/3/2020 2:29:59 PM 

 

Thanks in advance for any help

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

See if this helps. It returns this in Power Query:

2020-05-06 13_56_44-20200506 - Power Query filtering Other Table - Power Query Editor.png

To get this result, add a column to your Table 1 with this formula:

Table.SelectRows(
    Table2,
    (Ranges) => ([Datetime] >= Ranges[StartDateTime])
                and
                ([Datetime] <= Ranges[EndDateTime])

)

That will return a nested table from Table2 that has only the records in that range.

Then just expand that added custom column.

 

See this file for full details.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

4 REPLIES 4
edhans
Super User
Super User

See if this helps. It returns this in Power Query:

2020-05-06 13_56_44-20200506 - Power Query filtering Other Table - Power Query Editor.png

To get this result, add a column to your Table 1 with this formula:

Table.SelectRows(
    Table2,
    (Ranges) => ([Datetime] >= Ranges[StartDateTime])
                and
                ([Datetime] <= Ranges[EndDateTime])

)

That will return a nested table from Table2 that has only the records in that range.

Then just expand that added custom column.

 

See this file for full details.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thanks for your help, the solution you provided works perfectly.

 

I found an article yesterday that was matching between dates and was able to modify the steps to do the same with a Datetime but decreased the time resolution portion of datetime to reduce the subsequent generated rows using...

= Table.AddColumn(#"Removed Columns", "TimestampHH:SS", each [Timestamp] - #duration(0,0,0,Time.Second([Timestamp])))

 

Below are the applied steps I used to create the intveral match I was looking for... 

  1. = Table.AddColumn(#"Renamed Columns", "TotalMinutes", each Duration.TotalMinutes([ShiftPlannedEnd] - [ShiftPlannedStart]), Int64.Type)
  2. = Table.AddColumn(#"Inserted Date Subtraction", "Timestamp_hh:mm", each List.DateTimes([ShiftPlannedStart],[TotalMinutes]+1,#duration(0,0,1,0)))
  3. = Table.ExpandListColumn(#"Added Custom2", "Timestamp_hh:mm")
  4. Then used "Merge Queries as new" using a left outer merge to create the final table.

But your solution seems much more efficient J

 

 

 

 

 

Glad it helped out @Tgilchrist . I was trying to do it without an actual merge.

 

As you've discovered though, if there is one way to do it in Power Query, there are probably 10 ways to do it! 😁



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
Greg_Deckler
Super User
Super User

@ImkeF @edhans

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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

Check out the June 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors