Finding the lead or lag of n-business days

Hello,

I'm trying to easily find what is the nth business days before and after an event. With the table below. Business days are uniquely defined in the table. So for example for OPR_DATE 2026-04-01, 7 business days would be 2026-04-09.

If anyone can help me with the code that would be greatly appreciated, thanks!

1 Like

Hi @NoSourApples
Check-out the {bizdays} package which is designed for this. You can also supply a list of holidays that are included as non-business days in your location.
HTH

1 Like

This is one of those problems that sounds simple until you try to make it fully reliable in SQL or code :slightly_smiling_face:

The key is that you can’t really treat “nth business day” as arithmetic on dates — you have to turn your business-day definition into an ordered sequence first. Once you have that, the problem becomes a lookup instead of a calculation.

Typical approach is:

Build a calendar table (or CTE) that contains all dates in your range
Mark each date as business day or not based on your table definition
Assign a running index only to business days (ROW_NUMBER() over ordered dates)
Then you can join back using that index ± N

So conceptually:

Filter to business days
Add business_day_index = ROW_NUMBER()
Find the index for your OPR_DATE (or nearest prior/next valid date depending on rule)
Then just do index + 7 or index - 7 and join back to get the resulting date

The important edge case is your example: if OPR_DATE itself is not always a business day, you need to define whether you:

start counting from the next business day, or
include the current day if it qualifies

That changes the offset by 1 in practice.

1 Like