cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mitalidesai
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
mdevaney
Super User III
Super User III

@mitalidesai 

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)

img1.png

 

Wednesday to Saturday (Invalid)

img2.png

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."

View solution in original post

1 REPLY 1
mdevaney
Super User III
Super User III

@mitalidesai 

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)

img1.png

 

Wednesday to Saturday (Invalid)

img2.png

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."

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Query PA Forum 768x460.png

Check it out!

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

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

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)