Week to Week Sales Comparison
I'm looking for a way to use beast mode to calculate last week's sales and compare them to this week's sales.
The syntax I used for the monthly calculation worked but I'm failing to make the weekly one work.
Last Month:
SUM((CASE WHEN (month(curdate()) = 1) THEN (CASE WHEN ((year(`filter_date`) = (year(curdate()) - 1)) AND (month(`filter_date`) = 12)) THEN (`premium_amount` * 1) END) WHEN ((Year(`filter_date`) = Year(CURDATE())) AND (MONTH(`filter_date`) = (Month(CURDATE()) - 1))) THEN (`sales` * 1) END))
MTD:
(SUM((CASE WHEN (((Year(`filter_date`) = Year(CURDATE())) AND (MONTH(`filter_date`) = Month(CURDATE()))) AND (DAYOFMONTH(`filter_date`) <= (DAYOFMONTH(CURRENT_DATE) - 1))) THEN `sales` END)) * 1)
Last week:
SUM((CASE WHEN (((YEAR(`filter_date`) = YEAR(CURDATE())) AND (WEEKOFYEAR(`filter_date`) = (WEEKOFYEAR(CURDATE()) - 1))) AND (DAYOFWEEK(`filter_date`) <= (DAYOFWEEK(CURRENT_DATE) - 1))) THEN `sales` END))
WTD:
SUM((CASE WHEN (((Year(`filter_date`) = Year(CURDATE())) AND (WEEKOFYEAR(`filter_date`) = WEEKOFYEAR(CURDATE()))) AND (DAYOFWEEK(`filter_date`) <= (DAYOFWEEK(CURRENT_DATE) - 1))) THEN (`sales` * 1) END))
Answers
-
Are you getting a syntax error? Or is the result just not want you are expecting?
There are a couple other date functions you might want to consider using to make the function a little easier to read.
You might consider using DATE_SUB to get the previous month and previous week as it has an interval parameter that lets you specify week or month for how much you want to subtract.
You could also use the YEARWEEK function as it will put it in YYYYWW format, such 202116 for the 16th week of 2021. You can then just subtract 1 for the previous week.
These functions would reduce the number of ANDs that you have to determine if you are looking at the correct date.
**Check out my Domo Tips & Tricks Videos
**Make sure to any users posts that helped you.
**Please mark as accepted the ones who solved your issue.1 -
Hey @MarkSnodgrass ,
No syntax error it's just not producing accurate results. I will give your suggestions a try...
0 -
Hi @Mike_Shrift ,
I cases of period over period I highly recommend creating a date dimension table to join to with your different offsets (day, week, year, etc) This gives you more flexibility with your graphs. Plus if you're looking to do period over period this way give more accurate representation of the current month as it'll compare the same number of days and not a whole prior month to a current partial month. You'll just need to use a Line + Bar type graph instead of a Period over Period graph.
I've done a prior writeup of this under https://dojo.domo.com/discussion/comment/50540#Comment_50540
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**1
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
- 753 Beast Mode
- 61 App Studio
- 41 Variables
- 692 Automate
- 177 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