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
IamTDR
Responsive Resident
Responsive Resident

Power Query: Date/Time Falls Between Business Hours

Hi
I'm struggling to come up with something in power query that will show the following.

 

I have a records that have a 'Created At' field, which is a date/time field.  I also have a '1st Response Time' field, date/time.

The trouble is I need to tell if this '1st Response Time' field falls within the following windows.

- Monday through Thur between 9am to 3pm, Friday 9am to 10:30am

 

The goal is to look at the 'Created At' field, and see if the first response is within 2 hours...but need to make sure that the 'Created At' field is within business hours.

I'm really stumped here.  Any advice would be appreciated

Thanks in advance

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @IamTDR

 

First go to query editor> split columns :Split the date/time columns into 2 columns: date and time,as you see below:

Annotation 2020-05-18 170008.png

Then create a calculated column to get the weekday of the field "create at":

 

Weekday = FORMAT(WEEKDAY('Table (2)'[Create at.1],1),"DDDD")

 

Finally create 2 measures as below:

 

is within 2 hours = 
var a=DATEDIFF(MAX('Table (2)'[Create at.2]),MAX('Table (2)'[the first response.2]),MINUTE)/60 Return
IF(a<=2,1,0)
is within business hours = IF(MAX('Table (2)'[Weekday]) in FILTERS('Table'[Weekday ])&&MAX('Table (2)'[Create at.2])>=MAX('Table'[Working hour start ])&&MAX('Table (2)'[the first response.2])<=MAX('Table'[Working hour end]),1,0)

 

And you will see:

Annotation 2020-05-18 170332.png

For the related .pbix file ,pls click here.

 

 
Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

View solution in original post

5 REPLIES 5
v-kelly-msft
Community Support
Community Support

Hi @IamTDR

 

First go to query editor> split columns :Split the date/time columns into 2 columns: date and time,as you see below:

Annotation 2020-05-18 170008.png

Then create a calculated column to get the weekday of the field "create at":

 

Weekday = FORMAT(WEEKDAY('Table (2)'[Create at.1],1),"DDDD")

 

Finally create 2 measures as below:

 

is within 2 hours = 
var a=DATEDIFF(MAX('Table (2)'[Create at.2]),MAX('Table (2)'[the first response.2]),MINUTE)/60 Return
IF(a<=2,1,0)
is within business hours = IF(MAX('Table (2)'[Weekday]) in FILTERS('Table'[Weekday ])&&MAX('Table (2)'[Create at.2])>=MAX('Table'[Working hour start ])&&MAX('Table (2)'[the first response.2])<=MAX('Table'[Working hour end]),1,0)

 

And you will see:

Annotation 2020-05-18 170332.png

For the related .pbix file ,pls click here.

 

 
Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

Thanks so much!  This looks like this will fit my need.

Also I appreicate the attached PBIX file as it will help me seee the steps and learn.

 

Thanks again

Greg_Deckler
Community Champion
Community Champion

I believe that the decimal portion of the date/time value is the time part. So, you could remove the integer portion and * by 24 to get the hour.

 

For day of week: https://docs.microsoft.com/en-us/powerquery-m/date-dayofweek



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

Is there a way for me to create a field in this table that would later allow me to pivot on whether the 2 hour window has been fulfilled?

 

Should I be researching DateDiff for this?  

Sorry, you asked about Power Query. DATEDIFF is DAX. Personally, Power Query's date time functions are fairly terrible to work with so DAX may not be a bad way to go. Yes, you could use DATEDIFF to get the number of hours between two dates very easily. It will be more of a pain in Power Query.

 

Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



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

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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