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
StephenK
Resolver I
Resolver I

Automated time intervals between StartTime and EndTime in Power Query

Hello all,

 

Lets say I have a table:

DateProviderStartTimeEndTime
2/26/2020Provider A8:00 AM4:00 PM
2/26/2020Provider B9:00 AM5:00 PM
2/26/2020Provider C7:45 AM3:30 PM
2/26/2020Provider D10:00 AM7:00 PM
2/26/2020Provider E8:00 AM5:00 PM

 

I want to use M to create a new column that automatically fills in time values at preset intervals between the StartTime field and EndTime field.

 

Example:

DateProviderSlotTimeStartTimeEndTime
2/26/2020Provider A8:00 AM8:00 AM4:00 PM
2/26/2020Provider A8:15 AM8:00 AM4:00 PM
2/26/2020Provider A8:30 AM8:00 AM4:00 PM
2/26/2020Provider A8:45 AM8:00 AM4:00 PM

 

Ideally this solution could accomodate conditions that change the interval depending on the Provider. For example, Provider A intervals at 15 minutes, Provider B at 10 minutes, etc.

 

The current way I am doing this very clunky and does not allow for conditionally changing the intervals. 

I basically have a whole bunch of functions like the following that increment off of each other:

 

= Table.AddColumn(#"Filtered Rows", "Time1", each if ([StartTime]+#duration(0,0,15,0)) < [EndTime] then ([StartTime]+#duration(0,0,15,0)) else null)

= Table.AddColumn(Custom1, "Time2", each if [Time1] = null then null else if ([Time1]+#duration(0,0,15,0)) < [EndTime] then ([Time1]+#duration(0,0,15,0)) else null)

= Table.AddColumn(#"Added Custom", "Time3", each if [Time2] = null then null else if ([Time2]+#duration(0,0,15,0)) < [EndTime] then ([Time2]+#duration(0,0,15,0)) else null)

 

I then unpivot all the columns to get my time values into rows incremented by 15 minute intervals.

 

Does anyone have any ideas on how this could be done in a more efficient way where the interval can be changed based on the value of another field?

 

1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @StephenK ,

 

Try this:

 

1. Add a custom column.

= if [Provider] = "Provider A" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15+1,#duration(0,0,15,0)) 
else if[Provider] = "Provider B" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0)) 
else if[Provider] = "Provider C" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15+1,#duration(0,0,15,0)) 
else if[Provider] = "Provider D" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0))
else if[Provider] = "Provider E" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0))  
else null

slot.PNG

 

2. Expand to new rows.

slot2.jpg

 

Then, you can get this:

slot3.PNG

 

For more details, please check the attached PBIX file.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
Icey
Community Support
Community Support

Hi @StephenK ,

 

Try this:

 

1. Add a custom column.

= if [Provider] = "Provider A" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15+1,#duration(0,0,15,0)) 
else if[Provider] = "Provider B" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0)) 
else if[Provider] = "Provider C" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15+1,#duration(0,0,15,0)) 
else if[Provider] = "Provider D" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0))
else if[Provider] = "Provider E" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10+1,#duration(0,0,10,0))  
else null

slot.PNG

 

2. Expand to new rows.

slot2.jpg

 

Then, you can get this:

slot3.PNG

 

For more details, please check the attached PBIX file.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@Icey This is great! Thank you so much. I'm gonna throw a curveball now--so with my current clunky formula, it stops the interval at the last 15 minutes before the EndTime so that the EndTime does not get counted in the interval. Example:

DateProviderSlotTimeStartTimeEndTime
2/27/2020Provider A8:00 AM8:00 AM8:45 AM
2/27/2020Provider A8:15 AM8:00 AM8:45 AM
2/27/2020Provider A8:30 AM8:00 AM8:45 AM

The idea is that 8:30 is the last 15 minute slot and the schedule closes at 8:45. The new formula is counting the 8:45 slot as well.

DateProviderSlotTimeStartTimeEndTime
2/27/2020Provider A8:00 AM8:00 AM8:45 AM
2/27/2020Provider A8:15 AM8:00 AM8:45 AM
2/27/2020Provider A8:30 AM8:00 AM8:45 AM
2/27/2020Provider A8:45 AM8:00 AM8:45 AM

 

How would you modify the formula to stop one interval before the EndTime?

Icey
Community Support
Community Support

Hi @StephenK ,

 

In the original formula, there is a "+1", just delete it.

 

Duration.TotalMinutes (...) / 15 + 1

 

 

So, try this:

= if [Provider] = "Provider A" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15,#duration(0,0,15,0)) 
else if[Provider] = "Provider B" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10,#duration(0,0,10,0)) 
else if[Provider] = "Provider C" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/15,#duration(0,0,15,0)) 
else if[Provider] = "Provider D" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10,#duration(0,0,10,0))
else if[Provider] = "Provider E" then List.Times([StartTime], Duration.TotalMinutes([EndTime]-[StartTime])/10,#duration(0,0,10,0))  
else null

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Perfect! Thanks so much @Icey! Was able to finally close some gaps on a major report thanks to your help.

Greg_Deckler
Super User
Super User

I'm guessing @ImkeF might.


@ 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.