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 III

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 III

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

#### Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

#### Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

#### 2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Top Solution Authors
Top Kudoed Authors
Users online (2,087)