web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id : 8/MQEwJi3Sqp7Y6oeXr6+v
Power Apps - Building Power Apps
Answered

Find time (working hours) between dates excluding weekends and holidays.

Like (0) ShareShare
ReportReport
Posted on 13 Jan 2022 12:01:15 by 26

Hi Guys, 

I have a help desk system designed with power apps and I would like to calculate SLA time for each ticket. In order to achieve this, I must calculate the number of hours a ticket has spent to the end of its lifecycle. 

The SLA not count weekends and public holidays. The time We work from 8am - 5pm. My data is stored on a sharepoint list. 

How do I proceed? 

 

Thanks

 

 

 

 

Categories:
  • BadriPradhan Profile Picture
    30 on 20 Oct 2023 at 09:49:23
    Re: Find time (working hours) between dates excluding weekends and holidays.

    I assume there is a StartDateTime and an EndDateTime and a Holidylist:

    Since there are 9 hours between 9 am and 6 pm, I think that the work hour of the day is 9.

    You can use this formula to calculate work hours:

     

    With(
    {
    varStartDateTime: DateTimeValue(varSelectedRecorddetails.InitialResponseOn),
    varEndDateTime: If(varSelectedRecorddetails.ResolutionDate=Blank(),Now(),DateTimeValue(varSelectedRecorddetails.ResolutionDate)),
    HolidayList: {Date:"2023/01/30"}
    },
    With(
    {
    varMinuts: If(DateDiff(varStartDateTime,varEndDateTime, TimeUnit.Minutes) > 0,
    Sum(
    ForAll(Sequence(DateDiff(varStartDateTime,varEndDateTime) + 1,1,1),
    If(
    Weekday(DateAdd(varStartDateTime,Value - 1,TimeUnit.Days)) in [1,7] ||
    Text(DateAdd(varStartDateTime,Value - 1,TimeUnit.Days),"yyyy/mm/dd") in HolidayList.Date,
    {Value: 0},
    {
    Value: If(
    ThisRecord.Value = 1,
    ((If(
    DateValue(varStartDateTime) = DateValue(varEndDateTime),
    Hour(varEndDateTime) + (Minute(varEndDateTime) / 60),
    18
    )) - (Hour(varStartDateTime)+Minute(varStartDateTime) / 60)),
    Value(ThisRecord.Value) = DateDiff(
    varStartDateTime,
    varEndDateTime,
    TimeUnit.Days
    ) + 1,
    ((Hour(varEndDateTime) + (Minute(varEndDateTime) / 60)) - 9),
    9
    )
    }
    )
    ),
    Value
    ) * 60,
    0
    )
    },
    RoundDown(varMinuts / 60,0) & " hr " & Mod(varMinuts,60) & " mins"
    )
    )

  • Robin1988 Profile Picture
    249 on 01 Nov 2022 at 08:13:01
    Re: Find time (working hours) between dates excluding weekends and holidays.

    I think I solved this myself!

    If(
     DateDiff(
     StartDateTime,
     EndDateTime
     ) > 1,
     Sum(
     ForAll(
     Sequence(
     DateDiff(
     StartDateTime,
     EndDateTime
     ) - 1,
     1,
     1
     ),
     If(
     Weekday(
     DateAdd(
     StartDateTime,
     Value,
     Days
     )
     ) in [
     0,
     7
     ] || Text(
     DateAdd(
     StartDateTime,
     Value,
     Days
     ),
     "yyyy/mm/dd"
     ) in HolidayList.Date,
     {Value: 0},
     {Value: 10}
     )
     ),
     Value
     ) * 60,
     0
    ) + If(
     DateDiff(
     StartDateTime,
     EndDateTime,
     Days
     ) = 0,
     If(
     Text(
     StartDateTime,
     "yyyy/mm/dd"
     ) in HolidayList.Date || Weekday(StartDateTime) in [
     0,
     7
     ],
     0,
     If(
     Int(
     Text(
     StartDateTime,
     "HH"
     )
     ) >= 18,
     0,
     DateDiff(
     StartDateTime,
     EndDateTime,
     Minutes
     )
     )
     ),
     If(
     Text(
     StartDateTime,
     "yyyy/mm/dd"
     ) in HolidayList.Date || Weekday(StartDateTime) in [
     0,
     7
     ],
     0,
     If(
     Int(
     Text(
     StartDateTime,
     "HH"
     )
     ) >= 18,
     0,
     DateDiff(
     StartDateTime,
     DateTimeValue(
     Concatenate(
     Text(
     StartDateTime,
     "yyyy-mm-dd"
     ),
     "T18:00:00"
     )
     ),
     Minutes
     )
     )
     ) + If(
     Text(
     EndDateTime,
     "yyyy/mm/dd"
     ) in HolidayList.Date || Weekday(EndDateTime) in [
     0,
     7
     ],
     0,
     If(
     Int(
     Text(
     EndDateTime,
     "HH"
     )
     ) <= 8,
     0,
     DateDiff(
     DateTimeValue(
     Concatenate(
     Text(
     EndDateTime,
     "yyyy-mm-dd"
     ),
     "T08:00:00"
     )
     ),
     EndDateTime,
     Minutes
     )
     )
     )
    )
  • Robin1988 Profile Picture
    249 on 31 Oct 2022 at 12:43:09
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @v-bofeng-msft  do you have any idea how to change your code to receive the exact amount of minutes? 🙂 Would be very helpful. 

  • Robin1988 Profile Picture
    249 on 29 Oct 2022 at 11:21:45
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Perfect, how can I make this returns in exact minutes?

  • Emma Etigu Profile Picture
    26 on 19 Jan 2022 at 10:54:52
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @v-bofeng-msft ,

     

    Thanks for your response. Your solution works really well. Much appreciated. 

     

    Thanks

     

    Emma.

  • Verified answer
    v-bofeng-msft Profile Picture
    on 19 Jan 2022 at 06:40:32
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @eetigu :

    I assume there is a StartDateTime and an EndDateTime and a Holidylist:

    Set(StartDateTime,DateTimeValue("2022/1/17 20:00:00"));
    Set(EndDateTime,DateTimeValue("2022/1/21 7:00:00"));
    ClearCollect(HolidayList,{Date:"2022/01/30"})

    Since there are 9 hours between 8am and 5pm, I assume that the work hour of the day is 9.

    You can use this formula to calculate work hours:

     

    If(
     DateDiff(StartDateTime,EndDateTime)>1,
     Sum(
     ForAll(
     Sequence(DateDiff(StartDateTime,EndDateTime)-1,1,1),
     If(
     Weekday(DateAdd(StartDateTime,Value,Days)) in [0,7] || Text(DateAdd(StartDateTime,Value,Days),"yyyy/mm/dd") in HolidayList.Date,
     {Value:0},
     {Value:9}
     )
     ),
     Value
     ),
    0)+
    If(
     DateDiff(StartDateTime,EndDateTime,Days)=0,
     If(
     Text(StartDateTime,"yyyy/mm/dd") in HolidayList.Date || Weekday(StartDateTime) in [0,7],
     0,
     If(
     Int(Text(StartDateTime,"HH"))>=17,
     0,
     9-Max(0,Text(StartDateTime,"HH")-8)-Max(0,17-Text(EndDateTime,"HH"))
     )
     ),
     If(
     Text(StartDateTime,"yyyy/mm/dd") in HolidayList.Date || Weekday(StartDateTime) in [0,7],
     0,
     If(
     Int(Text(StartDateTime,"HH"))>=17,
     0,
     Max(0,17-Text(StartDateTime,"HH"))
     )
     )+
     If(
     Text(EndDateTime,"yyyy/mm/dd") in HolidayList.Date || Weekday(EndDateTime) in [0,7],
     0,
     If(
     Int(Text(EndDateTime,"HH"))<=8,
     0,
     Min(9,Text(EndDateTime,"HH")-8)
     )
     )
    )
     

    Best Regards,

    Bof

     

  • WarrenBelz Profile Picture
    148,894 Most Valuable Professional on 18 Jan 2022 at 09:14:58
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @eetigu ,

    I assumed you would multiply the days by the number of hours worked in a day, minus the time left on the start day.

  • Emma Etigu Profile Picture
    26 on 18 Jan 2022 at 08:41:01
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi Guys, 

     

    Any suggestions on how to go about this?

     

    Thanks. 

  • Emma Etigu Profile Picture
    26 on 14 Jan 2022 at 07:44:54
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @WarrenBelz ,

     

    Thank you for your response. However the solution you have suggested works best for getting date difference while excluding weekends and holidays but not business hours. I am currently trying to factor in the hours and if I get a solution I will post it here. Thanks

  • WarrenBelz Profile Picture
    148,894 Most Valuable Professional on 13 Jan 2022 at 21:31:16
    Re: Find time (working hours) between dates excluding weekends and holidays.

    Hi @eetigu ,

    You might start with this document.

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    Visit my blog Practical Power Apps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 791 Most Valuable Professional

#2
MS.Ragavendar Profile Picture

MS.Ragavendar 410 Super User 2025 Season 2

#3
mmbr1606 Profile Picture

mmbr1606 275 Super User 2025 Season 2