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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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

@ 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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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