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.
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))
(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)
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))
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))
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
- 10.5K All Categories
- 8 Connect
- 918 Connectors
- 250 Workbench
- 473 Transform
- 1.7K Magic ETL
- 69 SQL DataFlows
- 478 Datasets
- 209 Visualize
- 257 Beast Mode
- 2.1K Charting
- 12 Variables
- 85 Cards, Dashboards, Stories
- 18 Automate
- 355 APIs & Domo Developer
- 89 Apps
- 3 Workflows
- 20 Predict
- 5 Jupyter Workspaces
- 15 R & Python Tiles
- 247 Distribute
- 63 Domo Everywhere
- 243 Scheduled Reports
- 21 Manage
- 42 Governance & Security
- 183 Product Ideas
- 1.2K Ideas Exchange
- 11 Community Forums
- 27 Getting Started
- 14 Community Member Introductions
- 55 Community News
- 4.5K Archive