Domo Idea Exchange  Beast Modes  Business Days Prior to Today
Greetings! This is another post highlighting a beast mode from my Domo IDEAs conference session. This one covers how to calculate the number of business days between two dates.
Problem:
How to calculate the number of business days (Monday  Friday) between two dates.
Solution:
We need to calculate the number of days in between the two dates, remove the days which are weekends and an extra day if it's not a Saturday. The logic works because we don't want to count today so we subtract the extra day but because Saturday comes before a Sunday we still need to remove that single day.
Let's break it down into the different steps.
First, get the number of days between the two dates:
 Author:  Created:  Last Modified:  Description:  Calculate the number of actual days between the two dates DATEDIFF(CURRENT_DATE(), `dt`)
Next determine the number whole weeks and multiply that number by 2 to subtract from the date difference. We determine the number of whole weeks between the two by calculating the first day of the week for each (This is the same as subtracting the difference of 7 and the day of week for the date)
 Author:  Created:  Last Modified:  Description:  Days of weekends between the two dates DATEDIFF(CURRENT_DATE() + INTERVAL (1  DAYOFWEEK(CURRENT_DATE())) DAY, `dt` + INTERVAL (1  DAYOFWEEK(`dt`)) DAY) / 7 * 2
Finally we have a condition to check if the start date is a Saturday or not. This is to remove the current date with Saturday being the exception since it precedes Sunday and is a partial week.
 Author:  Created:  Last Modified:  Description:  If we don't start on a Saturday remove the current day so we don't count it CASE WHEN DAYOFWEEK(`dt`) <> 7 THEN 1 ELSE 0 END
Finally tying it all together:
 Author:  Created:  Last Modified:  Description:  Calculate the number of actual days between the two dates DATEDIFF(CURRENT_DATE(), `dt`)  Calculate the number of whole weeks:  Calculate the number of days between the start of the week for both dates  DATEDIFF(  Get the last of the prior week for our current date CURRENT_DATE() + INTERVAL (1  DAYOFWEEK(CURRENT_DATE())) DAY,  Get the last day of the prior week for our start date `dt` + INTERVAL (1  DAYOFWEEK(`dt`)) DAY ) / 7 * 2  Divide by 7 to get the number of whole weeks and multiply it by 2 to get the number of weekend days (Saturday and Sunday)  If we don't start on a Saturday remove the current day so we don't count today  (CASE WHEN DAYOFWEEK(`dt`) <> 7 THEN 1 ELSE 0 END)
Notes:
This is a simplified calculation of business days. It doesn't take into account holidays or other days your business may be closed.
Also, this will return positive numbers for the past and negative numbers for dates in the future. This can be changed by simply multiplying the final business days number by 1.
**Did this solve your problem? Accept it as a solution!**
Comments

Here's a link to the video of my session outlining this beast mode: https://www.youtube.com/watch?v=gO8OLpsAk4M&index=6
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1
Categories
 10.6K All Categories
 13 Getting Started in the Community
 31 Beastmode & Analytics
 2.1K Data Platform & Data Science
 59 Domo Everywhere
 2.7K Charting
 2.4K Ideas Exchange
 1.3K Connectors
 362 Workbench
 301 Use Cases & Best Practices
 500 APIs
 118 Apps
 48 News
 753 Onboarding
 1.1K 日本支部
 Private Company Board