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
iansheridan91
Regular Visitor

Calculating EndDateTime based on StartDateTime, Hours Of Effort Required and Working Hours

Hi,

 

First time posting here, hoping someone is able to help with this.

 

I have 2 tables:

- Working hours (with Date, StartTime, EndTime)

DateDayOfWeekWorkStartTimeWorkEndTimeWorkHours
20/OctWed06:3016:3010
21/OctThur06:3016:3010
22/OctFri06:3012:005.5
23/OctSat00:0000:000
24/OctSun00:0000:000
25/OctMon06:3016:3010
26/OctTues06:3016:3010

 

- Production Orders

OrderNum (Distinct)StartDateTimeHoursOfEffortResult
120/Oct/21 06:30620/Oct/21 06:30
220/Oct/21 06:302422/Oct/21 10:30

 

What I want to be able to do is calculate the Result column (which is the EndDateTime) based on the StartDateTime, the HoursOfEffort and working hours.

 

For example, if an order takes 24 hours of effort to complete it and starts on 20th Oct 2021 @ 06:30 then it calculates that the Result (EndDateTime) would be the 22nd Oct 2021 @ 10:30.

1 ACCEPTED SOLUTION

Hi @iansheridan91 

 

You could use below code to add a column. I add a new column 'StartDate' to 'Production Orders' table before creating this column. 

EndDateTime = 
var vStartDate = 'Production Orders'[StartDate]
var vWorkEndDateTime = vStartDate + MAXX(FILTER('Working hours','Working hours'[Date]=vStartDate),'Working hours'[WorkEndTime])
var vRemainWorkHours = ROUND((VALUE(vWorkEndDateTime) - VALUE('Production Orders'[StartDateTime])) * 24, 2) // Round the hours to have 2 digits
var vRemainEffortHours = 'Production Orders'[HoursOfEffort] - vRemainWorkHours
var vEndDateTime =IF(vRemainWorkHours>='Production Orders'[HoursOfEffort],'Production Orders'[StartDateTime] + 'Production Orders'[HoursOfEffort]/24,
    var vTable = FILTER('Working hours','Working hours'[Date]>vStartDate)
    var vTable2 = ADDCOLUMNS(vTable,"SumOfWorkingHours",SUMX(FILTER(vTable,'Working hours'[Date]<=EARLIER('Working hours'[Date])),'Working hours'[WorkHours]))
    var vEndDate = MINX(FILTER(vTable2,[SumOfWorkingHours]>=vRemainEffortHours),'Working hours'[Date])
    return 
    vEndDate + MAXX(FILTER(vTable2,'Working hours'[Date] = vEndDate),'Working hours'[WorkEndTime]) - (MAXX(FILTER(vTable2,'Working hours'[Date] = vEndDate),[SumOfWorkingHours]) - vRemainEffortHours)/24)
return
vEndDateTime

21102701.jpg

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

4 REPLIES 4
v-jingzhang
Community Support
Community Support

Hi @iansheridan91 

 

It is a little complicated. Will the StartDateTime in 'Production Orders' table be possible to have a time value between the WorkStartTime and WorkEndTime on a date or it will always be identical with a WorkStartTime in 'Working hours' table? 

 

Best Regards,
Community Support Team _ Jing

Hi @v-jingzhang 

 

Yes, the StartDateTime could be any time within the 'Working hours' StartTime & EndTime.

My thought is to try break it down the 2 calculations.

 

Let's say i have a 3rd Production Order with a StartDateTime of 22Oct2021 at 11:00 that takes 5 hours of effort.

 

If i can cumulative sum the Working Hours WorkHours from the Production Order StartDate i should be able to work out the date.

 

So i know theres 1 hour of working hours left for 22Oct2021 (at the working day ends at12) which means i need to cover 4 hours of work. If i add that 1 hour to the next days WorkHours is there enough to cover the 4 hours, if not then add the next two days, and so on until a TRUE is returned... so 4>= 1+0 would be FALSE, then 4>= 1+0+0 would be FALSE, then 4 >= 1+0+0+10 would be TRUE, so thats my EndDate.

 

Then the EndTime would be the 4 hours + the Working Hours StartTime of that TRUE Date.

 

Think it's possible using variables but im struggling with it.

Hi @iansheridan91 

 

You could use below code to add a column. I add a new column 'StartDate' to 'Production Orders' table before creating this column. 

EndDateTime = 
var vStartDate = 'Production Orders'[StartDate]
var vWorkEndDateTime = vStartDate + MAXX(FILTER('Working hours','Working hours'[Date]=vStartDate),'Working hours'[WorkEndTime])
var vRemainWorkHours = ROUND((VALUE(vWorkEndDateTime) - VALUE('Production Orders'[StartDateTime])) * 24, 2) // Round the hours to have 2 digits
var vRemainEffortHours = 'Production Orders'[HoursOfEffort] - vRemainWorkHours
var vEndDateTime =IF(vRemainWorkHours>='Production Orders'[HoursOfEffort],'Production Orders'[StartDateTime] + 'Production Orders'[HoursOfEffort]/24,
    var vTable = FILTER('Working hours','Working hours'[Date]>vStartDate)
    var vTable2 = ADDCOLUMNS(vTable,"SumOfWorkingHours",SUMX(FILTER(vTable,'Working hours'[Date]<=EARLIER('Working hours'[Date])),'Working hours'[WorkHours]))
    var vEndDate = MINX(FILTER(vTable2,[SumOfWorkingHours]>=vRemainEffortHours),'Working hours'[Date])
    return 
    vEndDate + MAXX(FILTER(vTable2,'Working hours'[Date] = vEndDate),'Working hours'[WorkEndTime]) - (MAXX(FILTER(vTable2,'Working hours'[Date] = vEndDate),[SumOfWorkingHours]) - vRemainEffortHours)/24)
return
vEndDateTime

21102701.jpg

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Thank you so much @v-jingzhang , this works perfectly!

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.