Domo Ideas Exchange - Beast Modes - First / Last Days of the Month / Week
How Do I calculate the start or end date of the week or month?
Last Day of the Month:
This is fairly simple as we can use utilize the
LAST_DAY function which will return the last day of the month for the provided date.
First Day of the Month:
For this we can do some simple math to subtract a specific number of days. We need to get back to day one. The
DAYOFMONTH function will return the day number of the supplied date. We just need to subtract one less from the date's day number
-- Author: -- Created: -- Last Modified: -- Description: Subtract the number of days of the date (except one) to get the first of the month -- 24-(24-1) => day 24 - 23 days = day 1 DATE_SUB(`dt`, INTERVAL (DAYOFMONTH(`dt`) - 1) DAY)
Last Day of the Week:
Similar to the first day of the month we can utilize math to add a specific number of days to get to Saturday. There is a function called
DAYOFWEEK which will return a number between 1 (Sunday) and 7 (Saturday). We simply need to calculate the difference from the given date to Saturday and add that number of days.
-- Author: -- Created: -- Last Modified: -- Description: Get the last day of the week. -- This is done by getting the day of the week (1-7) and adding the number -- of days needed to equal 7 (Saturday) DATE_ADD(`dt`, INTERVAL (7- DAYOFWEEK(`dt`)) DAY)
First Day of the Week:
For the First day of the week we do the opposite of the Last Day of the Week and subtract days until we get to day 1 of the week.
-- Author: -- Created: -- Last Modified: -- Description: Get the first day of the week -- This is done by subtracting the day number (1-7) from the given date. -- 5 (Thursday) - (5-1) days => 5 - 4 = 1 (Sunday) DATE_SUB(`dt`, INTERVAL (DAYOFWEEK(`dt`) - 1) DAY)
**Did this solve your problem? Accept it as a solution!**
- 7.3K All Categories
- 13 Getting Started in the Community
- 142 Beastmode & Analytics
- 1.8K Data Platform & Data Science
- 54 Domo Everywhere
- 2K Charting
- 1K Ideas Exchange
- 904 Connectors
- 237 Workbench
- 342 APIs
- 77 Apps
- 19 Governance & Productivity
- 235 Use Cases & Best Practices
- 50 News
- 473 Onboarding
- 573 日本支部