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

  • ezmac13
    ezmac13 Member
    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'))

Answers

  • ezmac13
    ezmac13 Member
    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'))

  • @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"
  • 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'))