Forum Discussion
Calculate working hours between two dates
See if these help:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Net-Work-Days/m-p/367362#M109
https://community.powerbi.com/t5/Quick-Measures-Gallery/Hour-Breakdown/m-p/625085#M306
- naomi_j6 years agoFrequent Visitor
Thanks Greg - it seems the Net Work Days option should be close to what I want. Although I will have the same issue as the other user who commented in the thread (start and finish times potentially occurring out of work hours). Was there a solution for avoiding the negative numbers?
Thanks again!
- v-chuncz-msft6 years agoCommunity Support
You may calculate working hours/minutes since Mon, Feb 24, 2020.
DIFF ( <Base>, <Resolved> ) - DIFF ( <Base>, <Created> )- naomi_j6 years agoFrequent Visitor
Thanks for your response, v-chuncz-msft. I've not seen this option - are there any articles to explain the function?
What would be the <Base> value here?
- naomi_j6 years agoFrequent Visitor
Greg_Deckler - I tried your net workdays option and it worked (thankfully no negative numbers output)! Thanks!
I changed the output to hours, which works better for me - although this seems to round off rather than offering part hours. Do you know if there's a way to have the output in part-hours (eg. 1.25 for 1hr12mins instead of 1)?
- Greg_Deckler6 years agoCommunity Championnaomi_j - Not entirely certain exactly what your code is but perhaps try increasing the number of decimals?
- naomi_j6 years agoFrequent VisitorGreg_Deckler see code below. I tried to increase the number of decimals, but this doesn't work after the calculation is complete (.00 for all values).hoursNetworkDuration =// Get the start and end datesVAR __dateStart = Master_Appended[Created].[Date]VAR __dateEnd = Master_Appended[ResolvedNoBlanks].[Date]// Calculate the Net Work Days between the start and end datesVAR __NetWorkDays = COUNTX(FILTER(ADDCOLUMNS(CALENDAR(__dateStart,__dateEnd),"WeekDay",WEEKDAY([Date],2)),[WeekDay]<6),[Date])// Set this to the start of the work day (8:00 AM)VAR __startTime = TIME(8,0,0)// Set this variable to the end of the work day (5:00 PM)VAR __endTime = TIME(17,0,0)// Calculate the duration of a full day, in this case in minutesVAR __fullDayhours = DATEDIFF(__startTime,__endTime,HOUR)// Calculate teh number of full days, this accounts for the possibility that tickets start and end on the same dayVAR __fullDays = IF(__NetWorkDays < 2,0,__NetWorkDays-2)// Calculate the total duration of all full days.VAR __fullDaysDuration = __fullDays * __fullDayhours// Calculate the start time of the current recordVAR __startDayTime = TIME(HOUR(__dateStart),MINUTE(__dateStart),SECOND(__dateStart))VAR __startDayTime1 = SWITCH(TRUE(),__startDayTime>__endTime,__endTime,__startDayTime<__startTime && __startDayTime>TIME(0,0,0),__startTime,__startDayTime)// Caclulate the duration of time for the first dayVAR __startDayDuration = DATEDIFF(__startDayTime1,__endTime,HOUR)// Calculate the end time of the current recordVAR __endDayTime = TIME(HOUR(__dateEnd),MINUTE(__dateEnd),SECOND(__dateEnd))VAR __endDayTime1 = SWITCH(TRUE(),__endDayTime>__endTime,__endTime,__startDayTime<__startTime && __startDayTime>TIME(0,0,0),__startTime,__endDayTime)// Calculate the duration of time for the last dayVAR __endDayDuration = DATEDIFF(__startTime,__endDayTime1,HOUR)// The total duration is the duration of all full days plus the durations of time for the first and last daysRETURNIF(__NetWorkDays=1,DATEDIFF(__dateStart,__dateEnd,HOUR),__fullDaysDuration + __startDayDuration + __endDayDuration)