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 / Trying to get this for...
Power Apps
Answered

Trying to get this formula to skip over weekends and federal holidays

(1) ShareShare
ReportReport
Posted on by 7
I am trying to skip weekends and federal holidays when selecting from a date picker it will auto project the next date into the date picker ahead of it skipping weekends and holidays it currently does not skip weekends, I am using the default date control in the datepicker thats ahead of this one
With(
    {
        baseDate: Planning_STEP01DataCardValue.SelectedDate,  // Starting point
        varHoliday: FEDholiday_list_2024_2030.HolyDate,  // List of holidays
        daysToAdd: Step2_Duration,  // Number of workdays to add
        currentDate: Planning_STEP01DataCardValue.SelectedDate,  // Initialize current date as base date
        workdaysAdded: 0  // Initialize workdays counter
    },
    // Loop until we add the required number of workdays
    While(
        workdaysAdded < daysToAdd,
        // Check if the current date is a weekend (Saturday=7, Sunday=1) or holiday
        If(
            Weekday(currentDate, StartOfWeek.Monday) in [7, 1] || currentDate in varHoliday,
            // If weekend or holiday, move to the next day
            Set(currentDate, DateAdd(currentDate, 1, TimeUnit.Days)),
            // If it's a valid workday, increment the counter
            Set(workdaysAdded, workdaysAdded + 1),
            // Always move to the next day
            Set(currentDate, DateAdd(currentDate, 1, TimeUnit.Days))
        )
    ),
    // Return the final ProjectedDate after loop ends
    currentDate
)
 
Categories:
I have the same question (0)
  • Verified answer
    WarrenBelz Profile Picture
    153,136 Most Valuable Professional on at
    Try this format
    With(
       {
          _Current: Planning_STEP01DataCardValue.SelectedDate,
          _HolidayList: FEDholiday_list_2024_2030,
          _Add: Step2_Duration
       },
       With(
          {
             _End: 
             DateAdd(
                _Current,
                _Add + 
                RoundDown(
                   (
                      Weekday(
                         _Current,
                         StartOfWeek.Monday
                      ) + 
                      _Add - 1
                   ) / 5,
                   0
                ) * 2,
                TimeUnit.Days
             )
          },
          With(
             {
                _Holidays: 
                CountRows(
                   Filter(
                      _HolidayList,
                      HolyDate >= _Current && HolyDate <= _End
                   )
                )
             },
             With(
                {
                   _TempDate: 
                   DateAdd(
                      _End,
                      _Holidays,
                      TimeUnit.Days
                   )
                },
                If(
                   Weekday(
                      _TempDate,
                      StartOfWeek.Monday
                   ) <= 5,
                   _TempDate,
                   DateAdd(
                      _TempDate,
                      8 - 
                      Weekday(
                         _TempDate,
                         StartOfWeek.Monday
                      ),
                      TimeUnit.Days
                   )
                )
             )
          )
       )
    )

     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • CU23011842-0 Profile Picture
    7 on at
    @WarrenBelz Hi Warren, How would I modify the formula if I want to add say 90 days and have it skip weekends instead of just 2. When I change the * 2 to 90 it seems to break
  • CU23011842-0 Profile Picture
    7 on at
    @WarrenBelz Sorry for spamming but I realized it wasn't the round down arguments it was referencing holydate using less than signs i think I got this error Invalid argument type. Expecting one of the following: Number, Decimal, Date, Time, DateTime, UntypedObject.
  • WarrenBelz Profile Picture
    153,136 Most Valuable Professional on at
    Sorry - a little lost with your question - there are only three parameters here: -
    • Planning_STEP01DataCardValue is a Date Picker with the start date selected.
    • Step2_Duration is a Variable with the number of days to add (90 in your example).
    • FEDholiday_list_2024_2030 is a data source (assume SharePoint list) with a date field HolyDate containing all of the holiday dates.
    If you use the code I posted and enter the first two parameters, it will display the end date accounting for all weekends and holidays.
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn   
     
  • CU23011842-0 Profile Picture
    7 on at
    @WarrenBelz When I use the step3 Duration Variable it calculates 3 months but it ends up on a weekend is there something I am missing in the code to adjust or is it flawed when using longer durations
  • WarrenBelz Profile Picture
    153,136 Most Valuable Professional on at
    I have modified the end of the code - should work now.
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee

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 740 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 342 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard