Beast Mode to show total for previous week and two weeks prior
I am trying to create a table that has a column that shows a total from the previous week and a column with a total from 2 weeks prior. The data crosses the new year, 2020 to 2021. When I create the calculation for 2 weeks prior, it yields an accurate result but when I try a similar calculation for the previous week, it is not accurate.
Here is the calculation I am using for the sum of visit 2 weeks prior:
Sum(CASE WHEN WEEK(DATE_SUB(Current_Date(), INTERVAL 2 WEEK)) = WEEK(`Visit Start Date`) AND YEAR(DATE_SUB(Current_Date(), INTERVAL 2 WEEK)) = YEAR(`Visit Start Date`) THEN 1 Else 0 End)
I tried to get the total for the previous week using the calculation below and it is not yielding the correct results.
Sum(CASE WHEN WEEK(DATE_SUB(Current_Date(), INTERVAL 1 WEEK)) = WEEK(`Visit Start Date`) AND YEAR(DATE_SUB(Current_Date(), INTERVAL 1 WEEK)) = YEAR(`Visit Start Date`) THEN 1 Else 0 End)
What calculations would be best for this scenario?
Comments
-
You could try setting up your comparison using the YEARWEEK function instead of WEEK and YEAR separately. So your beast mode would look like this:
Sum(CASE WHEN YEARWEEK(DATE_SUB(Current_Date(), INTERVAL 2 WEEK)) = YEARWEEK(`Visit Start Date`) THEN 1 Else 0 End)
0 -
Hi @user028686
Whenever I typically need to do some period over period comparisons I actually restructure my data so that I have a report date and a "comparison date" (the actual date the metric was recorded on) and calculate it as necessary. This allows you to have custom defined periods (like 2 weeks ago and a week ago).
You'd essentially use the report date in your graph but then have beast modes to determine which type of period to display (last week, 2 weeks ago etc.) and centralizes this logic so you have one place to maintain it rather than across multiple cards and beast modes.
I've written about this method previously here: https://dojo.domo.com/t5/Card-Building/show-percent-on-period-over-period-graph/m-p/50540/highlight/true#M7313
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0 -
Thank you for your response. This worked to show 2 weeks prior but when I do that to only show the previous week, it just displays the total for 2020 data, not 2021. The previous week is 12/27/20 - 1/2/21.
0 -
If you absolutely must use a beast mode you could utilize the DAYOFWEEK function to get the end of the prior week and then subtract the number of weeks you're interested in. DAYOFWEEK returns values between 1 (Sunday) and 7 (Saturday). Subtracting that value for the number of days will return the end of last week
For example - 1 Week Ago / Last Week:
SUM( CASE WHEN (`Date`) > DATE_SUB(CURRENT_DATE(), INTERVAL DAYOFWEEK(CURRENT_DATE()) + (1*7) DAY) AND (`Date`) <= DATE_SUB(CURRENT_DATE(), INTERVAL DAYOFWEEK(CURRENT_DATE()) + (0*7) DAY) THEN 1 ELSE 0 END)
2 Weeks Ago:
SUM( CASE WHEN (`Date`) > DATE_SUB(CURRENT_DATE(), INTERVAL DAYOFWEEK(CURRENT_DATE()) + (2*7) DAY) AND (`Date`) <= DATE_SUB(CURRENT_DATE(), INTERVAL DAYOFWEEK(CURRENT_DATE()) + (1*7) DAY) THEN 1 ELSE 0 END)
To break it down for the week ago example: DATE_SUB(INTERVAL DAYOFWEEK(CURRENT_DATE()) + (0*7) DAY) says give me the Date of the Saturday before today. (0*7 here isn't needed but is to help illustrate the logic saying I don't want any weeks prior to this date)
(1*7) adds an extra week before that. (2*7) is two weeks prior.
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1 -
Again, I'd highly recommend you configure an offset dimension to setup a week prior and 2 weeks prior custom offsets and structure your data as such.
@jaeW_at_Onyx did a great video explaining this whole process as well: https://www.youtube.com/watch?v=CDKNOmKClms
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.6K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 2.9K Transform
- 102 SQL DataFlows
- 626 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 755 Beast Mode
- 61 App Studio
- 41 Variables
- 693 Automate
- 178 Apps
- 456 APIs & Domo Developer
- 49 Workflows
- 10 DomoAI
- 38 Predict
- 16 Jupyter Workspaces
- 22 R & Python Tiles
- 398 Distribute
- 115 Domo Everywhere
- 276 Scheduled Reports
- 7 Software Integrations
- 130 Manage
- 127 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 11 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 110 Community Announcements
- 4.8K Archive