M Code: Function: Previous business day

Cubelife

New Member
Joined
Feb 21, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I'm very new to using functions within my queries and am running into an interesting dilema. I want to "simply" have a step in my query that takes a date column (or even a defined date) and subtracts 1 business day. I found the following M code somewhere else but it is only useful for adding business days. I thought I could figure out how to modify the code to allow for negative values but I keep running into roadblocks. I have a feeling that I need to somehow change step 1 to create a list of backdated dates, but I can't figure out what needs changed to make that happen. Anyways, I've spent hours on this and figured maybe one of you can take a look at it and say "oh yeah, do this".


let
fnPreviousBusinessDay = (startDate, days) =>
let
Step1 = List.Dates(Date.AddDays(startDate, 1), days + Number.RoundUp(days/7*3+4,0), #duration(1,0,0,0)), //provision list of added days with 3 more days per each added week, starting from startDate + 1 day
Step2 = List.Select(Step1, (item) => Date.DayOfWeek(item, Day.Monday) < 5), // select only workdays
Step3 = List.FirstN(Step2, days), //select required number of workdays
Output = if days <= 0 then startDate else List.Last(Step3)
in
Output
in
fnPreviousBusinessDay
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
like this? Date.AddDays(startDate, -1)

When I make that change, it accepts it just fine with no errors. Once I try to invoke the function, I just receive today's date as a result.


1582640751720.png


1582640460467.png
1582640479546.png


The newbie in me then thought, "Easy, I'll type a negative 1 as my day value". That leads to an error which I'm guessing just means I shouldn't pursue trying to enter a negative day value since it won't accept negative durations.

1582640568238.png
 

Attachments

  • 1582640614560.png
    1582640614560.png
    19.6 KB · Views: 2
Upvote 0
duration cannot be negative

View attachment 7564
honestly I don't know what you want to achieve :confused:
That's EXACTLY what I want to acheive, at least what you have in your screenshot. I just realized, I had a few other items changed in the code from my previous troubleshooting attempts. After starting back with the code I posted above, and then changing the value to -1, it worked! Glad it was a simple change, thanks @sandy666 !!!
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top