Converting Week Numbers to Dates for custom week date range (Monday - Sunday)
Currently, I am using the DOMO out of the box solution for converting week numbers to dates. This works great for a Sunday to Saturday week; however, a request is being made to view our week as Monday - Sunday. I have attempted DATE_ADD and WEEK variations but have been unsuccessful.
Here is my current Beast Mode:
CONCAT(DATE_ADD(DATE_FORMAT(SUBDATE(`DATE`,WEEKDAY(`DATE`)-1),'%b %d'),1),' - ',DATE_ADD(DATE_FORMAT(SUBDATE(`DATE`,WEEKDAY(`DATE`)-7),'%b %d'),1))
FYI...I did change the format to the following but that made a mess of my numbers.
CONCAT(DATE_ADD(DATE_FORMAT(SUBDATE(`DATE`,WEEKDAY(`DATE`)-2),'%b %d'),1),' - ',DATE_ADD(DATE_FORMAT(SUBDATE(`DATE`,WEEKDAY(`DATE`)-8),'%b %d'),1))
Any ideas or suggestions would be greatly appreciated.
Best Answer
-
I was able to find a solution that correctly identifies the week date range starting Monday and ending Sunday. If there are any other suggestions/solutions please let me know.
CONCAT(DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-1),'%b %d'),' - ',DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-7),'%b %d'))
0
Answers
-
I was able to find a solution that correctly identifies the week date range starting Monday and ending Sunday. If there are any other suggestions/solutions please let me know.
CONCAT(DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-1),'%b %d'),' - ',DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-7),'%b %d'))
0 -
@ezmac13 if the math works, don't change.
To solve problems like this, I usually pop open Excel and figure it out there because it's super easy to find 'excel equivalent in mysql'
alternatively, if you used an article like https://stackoverflow.com/questions/6944055/how-do-i-get-the-first-day-of-the-week-of-a-date-in-mysql then be careful that you refactor for simplicity
since subDate and date_add do the same thing, I would just use date_add to make my code a little easier to read.
with date_add(...) indicate the interval you're assuming (day)
have a closer look at WEEKDAY(DATE_ADD(`DATE`,-1))-1, off the top of my head I don't recall if weekday does zero based indexing or not (0 - 6 vs 1-7)
WHAT I WOULD DO.
I would build this calc into a date dimension table. and then JOIN the date dimension to my fact table. This way, when the business changes their mind next week, OR when different people have different ways of slicing days and weeks, you don't have to maintain a ton of cards and beast modes.
Jae Wilson
Check out my 🎥 Domo Training YouTube Channel 👨💻
**Say "Thanks" by clicking the ❤️ in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1 -
Using the below formula how could I switch the dates from Wednesday - Tuesday date range?
CONCAT(DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-1),'%b %d'),' - ',DATE_FORMAT(SUBDATE(`DATE`, WEEKDAY(DATE_ADD(`DATE`,-1))-7),'%b %d'))
0
Categories
- All Categories
- 1.7K Product Ideas
- 1.7K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 295 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.8K Transform
- 97 SQL DataFlows
- 607 Datasets
- 2.1K Magic ETL
- 3.8K Visualize
- 2.4K Charting
- 708 Beast Mode
- 49 App Studio
- 39 Variables
- 667 Automate
- 170 Apps
- 446 APIs & Domo Developer
- 44 Workflows
- 7 DomoAI
- 33 Predict
- 13 Jupyter Workspaces
- 20 R & Python Tiles
- 391 Distribute
- 111 Domo Everywhere
- 274 Scheduled Reports
- 6 Software Integrations
- 115 Manage
- 112 Governance & Security
- Domo Community Gallery
- 31 Product Releases
- 9 Domo University
- 5.3K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 103 Community Announcements
- 4.8K Archive