cancel
Showing results for
Did you mean:
Highlighted
Level: Powered On

## 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

## Re: Exculde Saturday and Sunday individually while calculating leaves

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

## Re: Exculde Saturday and Sunday individually while calculating leaves

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

#### 'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

#### Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

#### Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (9,631)