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
 7.6K All Categories
 Connect
 916 Connectors
 241 Workbench
 472 Transform
 1.8K Magic ETL
 60 SQL DataFlows
 445 Datasets
 28 Visualize
 196 Beast Mode
 2K Charting
 6 Variables
 1 Automate
 348 APIs & Domo Developer
 82 Apps
 Workflows
 14 Predict
 3 Jupyter Workspaces
 11 R & Python Tiles
 241 Distribute
 59 Domo Everywhere
 241 Scheduled Reports
 14 Manage
 35 Governance & Security
 19 Product Ideas
 1.1K Ideas Exchange
 Community Forums
 15 Getting Started
 1 Community Member Introductions
 49 Community News
 18 Event Recordings
 579 日本支部