cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
RTW
Advocate III
Advocate III

Subtracting Business Days

Hi everyone,

 

I'm working on an app to help with scheduling. Basically the goal is, knowing the desired end date of a project and a few variables, I want to be able to work back and calculate when certain steps should be met. Basically, I need to subtract business days from a value. 

I made a quick test app. Basically it has a text input with a number, and a date picker, and a text field with the following code, based off of this blog post. 

 

 

With(
    {
        varStartDate: DatePicker1.SelectedDate,
        varAddWeekdays: Value(TextInput1.Text)
    },
    DateAdd(
        varStartDate,
        Value(varAddWeekdays) + RoundDown(
            (Weekday(
                varStartDate,
                Monday
            ) + Value(varAddWeekdays) - 1) / 5,
            0
        ) * 2,
        Days
    )
)

 

 


My issue is that it works fine when I add days, but it doesn't when I need to subtract days. I've fiddled around with it but I can't quite figure it out. For example, if I have the date set to Monday, July 11th and I want to subtract 1 business day, I expect the result to be Friday July 8th, but the app returns the Sunday the 10th.

 

The only solution I can think of is to make a collection of all the dates backwards from the end date, and then use that to calculate everything but there has to be an easier way, right?

2 REPLIES 2
WarrenBelz
Super User
Super User

Hi @RTW ,
I felt like a bit of fried brain this morning and this one intrigued me, so try this (I have it working here in both directions)

With(
   {
      wStartDate: DatePicker1.SelectedDate,
      wAddWeekdays: Value(TextInput1.Text)
   },
   With(
      {
         wDays: 
         Ungroup(
            ForAll(
               Sequence(Abs(wAddWeekdays)),
               Table(
                  {
                     DoW: 
                     DateAdd(
                        wStartDate,
                        If(
                            wAddWeekdays < 0,
                            -Value + 1,
                            Value - 1
                        )
                     )
                  }
               )
            ),
            "Value"
         )
      },
      With(
         {
            wCount: CountRows(wDays) + 
            CountRows(
               Filter(
                  wDays,
                  Weekday(DoW) = 1 || Weekday(DoW) = 7
               )
            )
         },
         DateAdd(
            wStartDate,
            If(
               wAddWeekdays < 0,
               -wCount,
               wCount
            ),
            Days
         )
      )
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

WarrenBelz
Super User
Super User

Hi @RTW ,

Just checking if you got the result you were looking for on this thread. Happy to help further if not.

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Top Kudoed Authors
Users online (1,553)