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
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Check out the New Ideas Site

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (3,094)