web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Set a date and exclude...
Power Apps
Unanswered

Set a date and exclude weekends and vacation days

(0) ShareShare
ReportReport
Posted on by 50

Hello,

 

I have an application which aims to set a task end date.

To define this date I must enter the number of days and then exclude weekends and holidays which are registered in a sharepoint list.

 

I have set up two calculation formulas but it does not work I would like your help.

 

Formula 1 :

the first datapicker is the departure date and the second is the calculated date 

date 2.jpg

 

Spoiler (Highlight to read)
With({startDate:DatePicker_1.SelectedDate;daysToAdd:Value(TextBox_75.Text)};
DateAdd(startDate;daysToAdd)+RoundDown(daysToAdd/5;0)*2+
Switch(Weekday(startDate;StartOfWeek.Monday);5;
If(Mod(daysToAdd;5)>0;2;0);4;
If(Mod(daysToAdd;5)>1;2;0);3;
If(Mod(daysToAdd;5)>2;2;0);2;
If(Mod(daysToAdd;5)>3;2;0);1;
If(Mod(daysToAdd;5)>4;2;0))
+
CountIf(Holiday;HolidayDate>=startDate;HolidayDate<=startDate))
With({startDate:DatePicker_1.SelectedDate;daysToAdd:Value(TextBox_75.Text)};DateAdd(startDate;daysToAdd)+RoundDown(daysToAdd/5;0)*2+Switch(Weekday(startDate;StartOfWeek.Monday);5;If(Mod(daysToAdd;5)>0;2;0);4;If(Mod(daysToAdd;5)>1;2;0);3;If(Mod(daysToAdd;5)>2;2;0);2;If(Mod(daysToAdd;5)>3;2;0);1;If(Mod(daysToAdd;5)>4;2;0))+CountIf(Holiday;HolidayDate>=startDate;HolidayDate<=startDate))

Formula 2 :

the first datapicker is the departure date and the second is the calculated date 

date1.jpg

Spoiler (Highlight to read)

DateAdd(DatePicker_1.SelectedDate;
Value(TextBox_66.Text) +
RoundDown(Value(TextBox_66.Text) / 5; 0)*2+
Switch(Weekday(DatePicker_1SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);
4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);
3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);
2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);
1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0))
)+


CountIf(
Holiday;
HolidayDate >= DateAdd(DatePicker_1.SelectedDate;
Value(TextBox_66.Text) +
RoundDown(Value(TextBox_66.Text) / 5; 0)*2+
Switch(Weekday(DatePicker_1.SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);
4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);
3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);
2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);
1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0))
);
HolidayDate <= DateAdd(DatePicker_1.SelectedDate;
Value(TextBox_66.Text) +
RoundDown(Value(TextBox_66.Text) / 5; 0)*2+
Switch(Weekday(DatePicker_1.SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);
4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);
3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);
2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);
1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0))
)
)

DateAdd(DatePicker_1.SelectedDate;Value(TextBox_66.Text) +RoundDown(Value(TextBox_66.Text) / 5; 0)*2+Switch(Weekday(DatePicker_1SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0)))+CountIf(Holiday;HolidayDate >= DateAdd(DatePicker_1.SelectedDate;Value(TextBox_66.Text) +RoundDown(Value(TextBox_66.Text) / 5; 0)*2+Switch(Weekday(DatePicker_1.SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0)));HolidayDate <= DateAdd(DatePicker_1.SelectedDate;Value(TextBox_66.Text) +RoundDown(Value(TextBox_66.Text) / 5; 0)*2+Switch(Weekday(DatePicker_1.SelectedDate);5;If(Mod( Value(TextBox_66.Text) ; 5)>0;2;0);4;If(Mod( Value(TextBox_66.Text) ; 5)>1;2;0);3;If(Mod( Value(TextBox_66.Text) ; 5)>2;2;0);2;If(Mod( Value(TextBox_66.Text) ; 5)>3;2;0);1;If(Mod( Value(TextBox_66.Text) ; 5)>4;2;0))))

The week starts on a Monday and the sharepoint list which includes vacation days is HolidayDate

Categories:
I have the same question (0)
  • Ramole Profile Picture
    Super User 2024 Season 1 on at

    Hi @EdouardVH 

    Do you want to exclude weekend ?
    have look this post more https://powerusers.microsoft.com/t5/Building-Power-Apps/Exclude-the-weekends/m-p/901790 

  • EdouardVH Profile Picture
    50 on at

    Hi , @Ramole 

    yes I would like to exclude the weekends and the days present in my sharepoint list.

    The topics I find explain how to exclude weekends and days between two dates.

    except that in my case I have only one departure date to which I add a number of days.

  • WarrenBelz Profile Picture
    153,026 Most Valuable Professional on at

    Hi @EdouardVH ,

    You might look at this excellent article from @timl 

  • EdouardVH Profile Picture
    50 on at

    hi @WarrenBelz ,

     


    Thank you for your help, !!

     

     

    Yes I have read and tested, but it does not work.

    Take the example if I select November 30, 2021 and I want to put -18 days I fall on the weekend. November 6, 2021

     

    toto.png

  • WarrenBelz Profile Picture
    153,026 Most Valuable Professional on at

    Hi @EdouardVH ,

    I don't think it was meant to have negative values.

  • EdouardVH Profile Picture
    50 on at

    Thanks @WarrenBelz ,

     

    I would have liked to find a solution, it would have been my Christmas present 😂

  • WarrenBelz Profile Picture
    153,026 Most Valuable Professional on at

    Hi @EdouardVH ,

    I will tag the author @timl here, but he will probably be offline due to his timezone.

  • timl Profile Picture
    36,383 Super User 2025 Season 2 on at

    Hi @WarrenBelz - Thanks for tagging me and for linking my post. You're correct - most of these solutions are not designed to work with negative values.

    The general technique relies on us calculating the number of working days based on full weeks, and to make an adjustment based on the number of partial days. The places where we 'hardcode' these additions would need to be subtractions in the case of where we want to subtract working days. Where we calculate the partial days, rather than calculate the number of days to the weekend, we need to calculate the number of days from the weekend since we're now counting backwards.

     

    @EdouardVH - To cope with negative values, my initial thought is to base the calculation on the absolute positive value, and to introduce if statements to account for the negative variations. The code would look like this:

     

    With({weekDaysToAdd: -18},
    With(
     { weekDaysToAddAbs: Abs(weekDaysToAdd),
     startDate:Date(2021,11,30) },
     With(
     {
     fullWeeks: RoundDown(weekDaysToAddAbs / 5, 0),
     remainingDays: Mod(weekDaysToAddAbs, 5),
     daysToWeekend: 
     If(weekDaysToAdd<0,Weekday(startDate, StartOfWeek.Monday)
     ,5- Weekday(startDate, StartOfWeek.Monday)) 
     },
     With(
     { daysToAdd: fullWeeks * 7 + remainingDays + If(remainingDays > daysToWeekend, 2, 0) },
     DateAdd(startDate, If(weekDaysToAdd<0,-1,1)*daysToAdd )))
     )
    )

     

    This produces the following result:

    timl_0-1640347102591.png

    This verifies the same formula against your original question of +45 days.

    timl_1-1640347189184.png

     

    Because @CarlosFigueira 's formula was neater, it was easier to add these variations to his formula so credit goes to Carlos.

    To make the adjustment for vacation days, you can count the number of days and add that as a positive value to weekDaysToAddAbs.

     

  • EdouardVH Profile Picture
    50 on at

    Hi @WarrenBelz  ,  @timl , 

     

    Sorry for the delay, Thank you very much for your help with my project.

     

    @timl  can you confirm to me that adding the number of days to weekDaysToAdAbs is correct? 

     

    Spoiler (Highlight to read)
    With({weekDaysToAdd: -18},
    With(
    { weekDaysToAddAbs: Abs(weekDaysToAdd),
    startDate:Date(2021,11,30) },
    With(
    {
    fullWeeks: RoundDown(weekDaysToAddAbs / 5, 0),
    remainingDays: Mod(weekDaysToAddAbs, 5),
    daysToWeekend:
    If(weekDaysToAdd<0,Weekday(startDate, StartOfWeek.Monday)
    ,5- Weekday(startDate, StartOfWeek.Monday))
    },
    With(
    { daysToAdd: fullWeeks * 7 + remainingDays + If(remainingDays > daysToWeekend, 2, 0) },
    DateAdd(startDate, If(weekDaysToAdd<0,-1,1)*daysToAdd )))
    ) + CountIf(Holiday;HolidayDate>=startDate;HolidayDate<=startDate) )
    )
    With({weekDaysToAdd: -18},With({ weekDaysToAddAbs: Abs(weekDaysToAdd),startDate:Date(2021,11,30) },With({fullWeeks: RoundDown(weekDaysToAddAbs / 5, 0),remainingDays: Mod(weekDaysToAddAbs, 5),daysToWeekend:If(weekDaysToAdd<0,Weekday(startDate, StartOfWeek.Monday),5- Weekday(startDate, StartOfWeek.Monday))},With({ daysToAdd: fullWeeks * 7 + remainingDays + If(remainingDays > daysToWeekend, 2, 0) },DateAdd(startDate, If(weekDaysToAdd<0,-1,1)*daysToAdd )))) + CountIf(Holiday;HolidayDate>=startDate;HolidayDate<=startDate) ))

    thank you very much 

  • Verified answer
    timl Profile Picture
    36,383 Super User 2025 Season 2 on at

    @EdouardVH 

    In terms of accounting for public holidays, adding the holiday days to weekDaysToAddAbs will be what you need.

     

    That is because if you're subtracting days, you don't want to add the holiday days to the final end result because you'll end up adjusting the result in the wrong direction (ie, you want to subtract the holiday days).

     

    ===

    With({weekDaysToAdd: -18},
    With(
    { weekDaysToAddAbs: Abs(weekDaysToAdd) + CountIf(Holiday;HolidayDate>=startDate;HolidayDate<=startDate) ,
    startDate:Date(2021,11,30) },
    With(
    {
    fullWeeks: RoundDown(weekDaysToAddAbs / 5, 0),
    remainingDays: Mod(weekDaysToAddAbs, 5),
    daysToWeekend:
    If(weekDaysToAdd<0,Weekday(startDate, StartOfWeek.Monday)
    ,5- Weekday(startDate, StartOfWeek.Monday))
    },
    With(
    { daysToAdd: fullWeeks * 7 + remainingDays + If(remainingDays > daysToWeekend, 2, 0) },
    DateAdd(startDate, If(weekDaysToAdd<0,-1,1)*daysToAdd )))
    ) )
    )

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard