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.5K All Categories
 8 Connect
 918 Connectors
 250 Workbench
 470 Transform
 1.7K Magic ETL
 69 SQL DataFlows
 477 Datasets
 194 Visualize
 253 Beast Mode
 2.1K Charting
 11 Variables
 17 Automate
 354 APIs & Domo Developer
 89 Apps
 3 Workflows
 20 Predict
 5 Jupyter Workspaces
 15 R & Python Tiles
 247 Distribute
 63 Domo Everywhere
 243 Scheduled Reports
 21 Manage
 42 Governance & Security
 174 Product Ideas
 1.2K Ideas Exchange
 12 Community Forums
 27 Getting Started
 14 Community Member Introductions
 55 Community News
 4.5K Archive