Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Answered

Get user defined total hours between two dates

(0) ShareShare
ReportReport
Posted on by 106

Hi, I am new to power apps. I need help to resolve one tricky issue. details mentioned below:

 

shrikantswnt_0-1661030185225.png

List 1 : Season Working Hours : Working hours are different according to dates.

shrikantswnt_2-1661030851094.png

 

List 2 : HolidaysCompany : where company maintain company holidays.

 

shrikantswnt_4-1661031201839.png

 

 

I need calculate "No of Hours" for the days between "From Date" and "End Date" excluding weekends and "company holidays (Mentioned in the list)". Currently I have hard coded 7 so it is showing 14 value.

my code is below : 

 

With(
{
// generate a one-column table of all dates between start date & end date
varDateRange: ForAll(
Sequence(DCV_ToDate.SelectedDate - DCV_FromDate.SelectedDate + 1),
DCV_FromDate.SelectedDate + Value - 1
)
},
If(
And(
IsBlank(DCV_FromDate.SelectedDate),
IsBlank(DCV_ToDate.SelectedDate)
),
// show nothing if any date pickers are blank
0,
// show only dates Sunday to Thursday and exclude holidays
CountIf(
varDateRange,
And(
Weekday(Value) in [1, 2, 3, 4, 5],
Not(Value in HolidaysCompany.Date)
)
)*7
)
)

 

  • According to the  selected dates '7' hours for '31 Oct 2022'
  • Skip the company holiday '1 Nov'
  • '9' hours for '2 Nov'

So total working hours should show 16 hours instead of 14 hours.

 

I am fighting from the last 2 days to resolve the issue.

  • Community Power Platform Member Profile Picture
    on at
    Re: Get user defined total hours between two dates

    Great!

    Thanks a lot ArtjolaZ!

  • shrikantswnt Profile Picture
    106 on at
    Re: Get user defined total hours between two dates

    Thanks ArtjolaZ, I have implemented you solution by doing some changes as per my requirement and it works 😍. This is exactly what I was looking for.
    Thanks a lot...!🤗

  • Verified answer
    ArtjolaZ Profile Picture
    18 on at
    Re: Get user defined total hours between two dates

     

    With(
     {
     // generate a one-column table of all dates between start date & end date
     varDateRange: ForAll(
     Sequence(DCV_ToDate.SelectedDate - DCV_FromDate.SelectedDate + 1),
     {
     Date: DCV_FromDate.SelectedDate + Value - 1,
     Hours: First(
     Filter(
     'Season Working hours',
     'End Date' >= DateAdd(
     DCV_FromDate.SelectedDate,
     Value - 1,
     Days
     ),
     'Start Date' <= DateAdd(
     DCV_FromDate.SelectedDate,
     Value,
     Days
     )
     )
     ).Hours
     }
     )
     },
     If(
     And(
     IsBlank(DCV_FromDate.SelectedDate),
     IsBlank(DCV_ToDate.SelectedDate)
     ),
    // show nothing if any date pickers are blank
     0,
    // show only dates Sunday to Thursday and exclude holidays
     Sum(
     varDateRange,
     If(
     And(
     Weekday(Date) in [
     2,
     3,
     4,
     5,
     6
     ],
     Not(Date in 'Holidays Companies'.date)
     ),
     Hours,
     0
     )
     )
     )
    )

     

    I have found this solution, but I have changed the weekday array to match my time zone.

  • shrikantswnt Profile Picture
    106 on at
    Re: Get user defined total hours between two dates

    Thanks buddy.

  • GTR95 Profile Picture
    25 on at
    Re: Get user defined total hours between two dates

    I get it now, you have transition between two months and different working hours. I will try to think about solution.

  • shrikantswnt Profile Picture
    106 on at
    Re: Get user defined total hours between two dates

    Thanks GTR95 for replying.
    Your last words are "multiply it with hours" which varies according to the date that's why I am not getting exact hours and facing an issue while retrieving it.
    please let me know if you find something 🙂

  • GTR95 Profile Picture
    25 on at
    Re: Get user defined total hours between two dates

    Not sure if I understood correctly, but you could count rows of HolidaysCompany list between two selected dates by filtering list, so that it returns number of only those rows that are within the selected dates. Then you will know how many company holidays is there to exclude from working hours. As for excluding weekends, you can substract: WeekNum(To date) -WeekNum(From date), so you could know the diff and multiply it with hours.

  • shrikantswnt Profile Picture
    106 on at
    Re: Get user defined total hours between two dates

    It is not yet working after spending daily hour on this. Any hint or update guys?

  • shrikantswnt Profile Picture
    106 on at
    Re: Get user defined total hours between two dates

    Thanks brother,

    But as HR is maintaining 3 rows then why HR will add 365 rows?
    For sure the team won't agree and not the client as well.

     

    Can we add ForALL or Switch loop in place of hardcoded 7? Because I did it using javascript in SP on-premise and it works but I don't know how to apply that in PowerApps

     

  • AnkushBelpande Profile Picture
    2 on at
    Re: Get user defined total hours between two dates

    Bro,

     

    If U need a quick solution, then try maintaining a list for all the 365 days.

    You can easily do that using an excel sheet which can be later imported as a list in SPO.

     

    List columns:

     

    1. Date

    2. Working Hours

    3. Holiday (Yes / No) -- If needed, else the first 2 columns should be sufficient.

     

    Then use this list to calculate the hours.

     

    You can also use your Holidays list to update the hours in this list using a flow or may be try a lookup.

     

    But if you cannot change the already implemented logic, SeasonWorking Hours + Holidays list then I'll get back to you soon with a solution using that.. 😇👍

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 55 Most Valuable Professional

#2
mmbr1606 Profile Picture

mmbr1606 44 Super User 2025 Season 1

#3
Michael E. Gernaey Profile Picture

Michael E. Gernaey 31 Super User 2025 Season 1

Overall leaderboard