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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Calculate number of we...
Power Apps
Answered

Calculate number of weekdays

(0) ShareShare
ReportReport
Posted on by 138

Hey 

 

I'm trying to create a holiday request tool. I have 2 Textboxes on a form which display a "Start Date" and an "End Date" of a vacation request. 

I'm using the following code to display the number of days between the two dates in a label: 

 

DateDiff(DateTimeValue(TextInput2.Text),DateTimeValue(TextInput3.Text))

 

Is it possible to filter this, so it only counts the number of weekdays? So if someone requests a week off work, the label would count 5 instead of 7? 

 

Thanks, 

 

 

Categories:
  • Verified answer
    Ami K Profile Picture
    15,689 Super User 2024 Season 1 on at

    Hi @stephenjcarr ,

     

    You can find many methods to achieve this on the forums (including a few using custom functions) but my go-to is the expression from @mdevaney here:

     

     

    With(
     {
     // generate a one-column table of all dates between start date & end date
     varDateRange: ForAll(
     Sequence(DateValue(TextInput3.Text) - DateValue(TextInput2.Text) + 1),
     DateValue(TextInput2.Text) + Value - 1
     )
     },
     If(
     And(
     IsBlank(TextInput2.Text),
     IsBlank(TextInput3.Text)
     ),
     // show nothing if any date pickers are blank
     0,
     // include only dates Monday to Friday
     CountIf(
     varDateRange,
     Weekday(Value) in [2, 3, 4, 5, 6]
     )
     )
    )

     

     

    ------------------------------------------------------------------------------------------------------------------------------

     

    If I have answered your question, please mark your post as Solved. Remember, you can accept more than one post as a solution.

    If you like my response, please give it a Thumbs Up.

  • stephenjcarr Profile Picture
    138 on at

    Hey @Amik 
    The code shows the correct number of days now, but when I submit the form I get an error "The first argument to Sequence must be between 0 and 50,000."

     

    Do you know what might be causing this?

     

    Thanks for your help! 

  • Ami K Profile Picture
    15,689 Super User 2024 Season 1 on at

    @stephenjcarr - the solution above is associated to updating a Label, you did not mention anything about submitting that data into a form...

     

    I assume you have applied the above expression onto the Default property of a Text Input control?

     

    ------------------------------------------------------------------------------------------------------------------------------

     

    If I have answered your question, please mark your post as Solved. Remember, you can accept more than one post as a solution.

    If you like my response, please give it a Thumbs Up.

  • stephenjcarr Profile Picture
    138 on at

    Sorry, Yes I replaced the label with a Text Input and added your code to the Default property. It's on a form that submits to a Sharepoint list. 

     

    Thanks, 

     

  • Verified answer
    Ami K Profile Picture
    15,689 Super User 2024 Season 1 on at

    I would apply that function to the Default property of the Data Card instead of the Data Card value.

     

    1. Other than the expression, have you made any other customisations to that Data Card?
    2. Could you also share the code you have used.

    ------------------------------------------------------------------------------------------------------------------------------

     

    If I have answered your question, please mark your post as Solved. Remember, you can accept more than one post as a solution.

    If you like my response, please give it a Thumbs Up.

     

     

  • stephenjcarr Profile Picture
    138 on at

    Thanks @Amik - Worked fine when I moved it into the Default property of the Data Card

  • stephenjcarr Profile Picture
    138 on at

    Can you help me with one more thing with this code please @Amik ?

    I've added a Toggle to the form (DataCardValue8) which relates to the "Half Day" Yes/No column in my Sharepoint List (Holiday Request Tool) 

    How do I add to this code, so that 0.5 will be subtracted from the result if the toggle is switched on?

     

    Thanks

  • Verified answer
    Ami K Profile Picture
    15,689 Super User 2024 Season 1 on at

    @stephenjcarr - add an additional With:

     

     

    With(
     {
     // generate a one-column table of all dates between start date & end date
     varDateRange: ForAll(
     Sequence(DateValue(TextInput3.Text) - DateValue(TextInput2.Text) + 1),
     DateValue(TextInput2.Text) + Value - 1
     )
     },
     With(
     {
     // subtract 0.5 if toggle value is true
     HalfDay: If(
     DataCardValue8.Value,
     0.5,
     0
     )
     },
     If(
     And(
     IsBlank(TextInput2.Text),
     IsBlank(TextInput3.Text)
     ),
     // show nothing if any date pickers are blank
     0,
     // include only dates Monday to Friday
     CountIf(
     varDateRange,
     Weekday(Value) in [
     2,
     3,
     4,
     5,
     6
     ]
     ) - HalfDay
     )
     )
    )

     

     

     

    ------------------------------------------------------------------------------------------------------------------------------

     

    If I have answered your question, please mark your post as Solved. Remember, you can accept more than one post as a solution.

    If you like my response, please give it a Thumbs Up.

     

  • stephenjcarr Profile Picture
    138 on at

    Hi @Amik 

    Thanks for your help again!

     

    This seems to change the text to to "0.5", rather than subtract 0.5 from the original value. Am I doing something wrong?

     

    Thanks 

  • stephenjcarr Profile Picture
    138 on at

    Ignore previous comment. Silly me! 

    Working Perfectly! Thanks again! 

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 221 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 188 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 120

Last 30 days Overall leaderboard