cancel
Showing results for
Did you mean:
Frequent Visitor

## Exculde Saturday and Sunday individually while calculating leaves

Hello,

I have a requirement which i need to exclude weekends at the time of leave calculation. I am able to do this successfully using the formula

RoundDown(DateDiff(LeaveStartDatePicker.SelectedDate, LeaveEndDatePicker.SelectedDate, Days) / 7, 0) * 5 +
Mod(5 + Weekday(LeaveEndDatePicker.SelectedDate) - Weekday(LeaveStartDatePicker.SelectedDate), 5) -
CountIf(Table2, HolidayDate >= LeaveStartDatePicker.SelectedDate, HolidayDate <= LeaveEndDatePicker.SelectedDate) + 1

However, If i select date range that includes either Saturday or Sunday individually (Not combined), it gives wrong results.

How can i correctly calculate correct days if i apply leaves from let's say Thurday to Saturday - The correct count should be 2.

Thanks,

Mitali

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

Ideally we could define which dates are allowable for selection and exclude Saturday & Sunday.  However,the datepicker does provide any way to eliminate invalid choices.  Therefore, you'll have to design a user interface to alert the user when the have made an incorrect decision.

Example:

Wednesday to Friday (Valid)

Wednesday to Saturday (Invalid)

Here's the relevant code:

EndDate.BorderColor = If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],Red,Black)

ErrorLabel.Text = If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],"Must select a weekday","")

DaysLeaveRequestedLabel.Text = If(Or(Weekday(StartDate.SelectedDate,StartOfWeek.Monday) in [6,7],Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7]),"Error",RoundDown(DateDiff(StartDate.SelectedDate, EndDate.SelectedDate, Days) / 7, 0) * 5 +
Mod(5 + Weekday(EndDate.SelectedDate) - Weekday(StartDate.SelectedDate), 5))

The key piece of code driving this solution is the conditional statement:

`If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],true,false)`

Note: I did not include holidays in my solution

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

Super User

Ideally we could define which dates are allowable for selection and exclude Saturday & Sunday.  However,the datepicker does provide any way to eliminate invalid choices.  Therefore, you'll have to design a user interface to alert the user when the have made an incorrect decision.

Example:

Wednesday to Friday (Valid)

Wednesday to Saturday (Invalid)

Here's the relevant code:

EndDate.BorderColor = If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],Red,Black)

ErrorLabel.Text = If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],"Must select a weekday","")

DaysLeaveRequestedLabel.Text = If(Or(Weekday(StartDate.SelectedDate,StartOfWeek.Monday) in [6,7],Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7]),"Error",RoundDown(DateDiff(StartDate.SelectedDate, EndDate.SelectedDate, Days) / 7, 0) * 5 +
Mod(5 + Weekday(EndDate.SelectedDate) - Weekday(StartDate.SelectedDate), 5))

The key piece of code driving this solution is the conditional statement:

`If(Weekday(EndDate.SelectedDate,StartOfWeek.Monday) in [6,7],true,false)`

Note: I did not include holidays in my solution

---
Please click "Accept as Solution" if my response helped to solve your issue so that others may find it more quickly. If your thought the post was helpful please give it a "Thumbs Up."

Announcements

#### April 4th Copilot Studio Coffee Chat | Recording Now Available

Did you miss the Copilot Studio Coffee Chat on April 4th? This exciting and informative session with Dewain Robinson and Gary Pretty is now available to watch in our Community Galleries!   This AMA discussed how Copilot Studio is using the conversational AI-powered technology to aid and assist in the building of chatbots. Dewain is a Principal Program Manager with Copilot Studio. Gary is a Principal Program Manager with Copilot Studio and Conversational AI. Both of them had great insights to share with the community and answered some very interesting questions!     As part of our ongoing Coffee Chat AMA series, this engaging session gives the Community the unique opportunity to learn more about the latest Power Platform Copilot plans, where we’ll focus, and gain insight into upcoming features. We’re looking forward to hearing from the community at the next AMA, so hang on to your questions!   Watch the recording in the Gallery today: April 4th Copilot Studio Coffee Chat AMA