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 the differen...
Power Apps
Answered

Calculate the difference between two dates

(0) ShareShare
ReportReport
Posted on by 2,297

In MyCollection below:

 

WebPortal_0-1675354732300.png

I need to calculate the total number of days, between start and end

  • for 2023 only
  • excluding weekends

So, it should be 10, from the 1st row + 11 from the second  = 21.

 

Can anyone help?

 

Thanks!

Categories:
  • Verified answer
    CarlosFigueira Profile Picture
    Microsoft Employee on at

    Here you go. Buckle your seatbelts, as it will be a wild ride :-D. This can be written in a more compact form, but I broke down the expression into something that (I hope) is more readable

    Sum(
     MyCollection,
     With(
     {start2023: Max(start, Date(2023,1,1)), end2023:Min(end, Date(2023,12,31))},
     With(
     {
     startWeekday:
     With(
     {wd:Weekday(start2023, StartOfWeek.Sunday)},
     If(
     wd = 1, start2023 + 1, // Sunday, move to following Monday
     wd = 7, start2023 + 2, // Saturday, move to following Monday
     start2023)),
     endWeekday:
     With(
     {wd:Weekday(end2023, StartOfWeek.Sunday)},
     If(
     wd = 1, end2023 - 2, // Sunday, move to previous Friday
     wd = 7, end2023 - 1, // Saturday, move to previous Friday
     end2023))
     },
     With(
     {
     firstMondayAfterStart: startWeekday + 7 - Weekday(startWeekday, StartOfWeek.MondayZero),
     firstMondayBeforeEnd: endWeekday - Weekday(endWeekday, StartOfWeek.MondayZero)
     },
     With(
     {
     fullWeeks: (firstMondayBeforeEnd - firstMondayAfterStart) / 7,
     extraDaysStartWeek: 7 - Weekday(startWeekday, StartOfWeek.Sunday),
     extraDaysEndWeek: Weekday(endWeekday, StartOfWeek.Monday)
     },
     fullWeeks * 5 + extraDaysStartWeek + extraDaysEndWeek)))))
    

    Going from top to bottom in the With function calls:

    • Since you only want dates from 2023, the first With 'clamps' the dates so that they are all within this year (start2023 and end2023)
    • Since you only want week days, the we move forward Saturday and Sundays to the next Monday (for the start date) and move back to the preceding Friday (startWeekday, endWeekday)
    • To make the calculation easier, we take the first Monday after the start date, and the first Monday before (or at) the end date, so that we can calculate whole weeks (firstMondayAfterStart, firstMondayBeforeEnd)
    • At that point we can find the number of whole weeks, and find the number of weekdays that we need to count from the start week, and the number of weekdays we need to count from the end week (fullWeeks, extraDaysStartWeek, extraDaysEndWeek)
    • Finally, we use those values to calculate the expression that will be used in the Sum function

    Hope this helps!

  • WebPortal Profile Picture
    2,297 on at

    @CarlosFigueirathank you, that sure looks scary 🙂

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 224 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 189 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 126

Last 30 days Overall leaderboard