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
-
@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"0 -
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
- 10.8K All Categories
- 3 Private Company Board
- 1 APAC User Group
- 12 Welcome
- 39 Domo News
- 9.7K Using Domo
- 1.9K Dataflows
- 2.5K Card Building
- 2.2K Ideas Exchange
- 1.2K Connectors
- 343 Workbench
- 260 Domo Best Practices
- 11 Domo Certification
- 465 Domo Developer
- 50 Domo Everywhere
- 106 Apps
- 717 New to Domo
- 85 Dojo
- Domopalooza
- 1.1K 日本支部
- 4 道場-日本支部へようこそ
- 27 お知らせ
- 64 Kowaza
- 299 仲間に相談
- 654 ひらめき共有