Cumulative Sales between December and January not calculating correctly.

Hello,
I'm trying to create a table with daily net sales by date and also cumulative sales up to the current date (1/3 ATM) between two years:
For the "Date" column, I used a beast mode to create date index formatted as %m/%d value:
- CASE
- WHEN YEAR(`created_at`) = '2024' AND DAYOFYEAR(`created_at`) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(`created_at`'%m/%d')
- WHEN DATE(`created_at`) > '2023-02-01' THEN DATE_FORMAT(`created_at`'%m/%d')
- WHEN YEAR(`created_at`) = '2023' AND DAYOFYEAR(`created_at`) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(`created_at`'%m/%d')
- WHEN YEAR(`created_at`) = '2022' AND DAYOFYEAR(`created_at`) > DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(`created_at`'%m/%d')
- END
Given this "Date" column, I filtered out any rows that are blank, which would remove sales for the period after 1/4 in 2023.
My current formula to calculate the cumulative sales is below where the same "Date" field above is used in the ORDER BY clause:
- SUM(SUM(CASE WHEN DATE(created_at) < '2023-02-01' THEN order_net END ))
- OVER (PARTITION BY YEAR(created_at) ORDER BY
- CASE
- WHEN YEAR(created_at) = '2024' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'%m/%d')
- WHEN DATE(created_at) > '2023-02-01' THEN DATE_FORMAT(created_at,'%m/%d')
- WHEN YEAR(created_at) = '2023' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'%m/%d')
- WHEN YEAR(created_at) = '2022' AND DAYOFYEAR(created_at) > DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'%m/%d')
- END)
This formula seems to work well up until Jan, at which point sales from 1/1 to 1/3 aren't added to the total cumulative sales from December but instead seems to be added to September when sales started for the year. Attached is the data from the chart.
Initially, the chart was ordered to have 1/1 to 1/4 at the bottom, but I achieved this sort order by using two helper beast modes Sort Month and Sort Day:
- CASE WHEN MONTH(created_at) = 1 THEN 13 ELSE MONTH(created_at) END
and
- DAY(created_at)
I'm not sure where to go from here, and can't find a way to get cumulative sales to add correctly. Any suggestions would be greatly appreciated!
Best Answer
-
The problem here is that when you use DATE_FORMAT the output is no longer a date but a STRING, and the sorting of it is not the same as the sorting you're displaying in your table. You'll need to play with your ORDER BY clause to make sure that 01/01 comes after 12/31 (I'd usually use a year indicator like 01 and 02), haven't tried but something like this might work:
- SUM(SUM(CASE WHEN DATE(created_at) < '2023-02-01' THEN order_net END ))
- OVER (PARTITION BY YEAR(created_at) ORDER BY
- CASE
- WHEN YEAR(created_at) = '2024' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'02/%m/%d')
- WHEN DATE(created_at) > '2023-02-01' THEN DATE_FORMAT(created_at,'01/%m/%d')
- WHEN YEAR(created_at) = '2023' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'02/%m/%d')
- WHEN YEAR(created_at) = '2022' AND DAYOFYEAR(created_at) > DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'01/%m/%d')
- END)
1 - SUM(SUM(CASE WHEN DATE(created_at) < '2023-02-01' THEN order_net END ))
Answers
-
The problem here is that when you use DATE_FORMAT the output is no longer a date but a STRING, and the sorting of it is not the same as the sorting you're displaying in your table. You'll need to play with your ORDER BY clause to make sure that 01/01 comes after 12/31 (I'd usually use a year indicator like 01 and 02), haven't tried but something like this might work:
- SUM(SUM(CASE WHEN DATE(created_at) < '2023-02-01' THEN order_net END ))
- OVER (PARTITION BY YEAR(created_at) ORDER BY
- CASE
- WHEN YEAR(created_at) = '2024' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'02/%m/%d')
- WHEN DATE(created_at) > '2023-02-01' THEN DATE_FORMAT(created_at,'01/%m/%d')
- WHEN YEAR(created_at) = '2023' AND DAYOFYEAR(created_at) <= DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'02/%m/%d')
- WHEN YEAR(created_at) = '2022' AND DAYOFYEAR(created_at) > DAYOFYEAR(CURDATE()) THEN DATE_FORMAT(created_at,'01/%m/%d')
- END)
1 - SUM(SUM(CASE WHEN DATE(created_at) < '2023-02-01' THEN order_net END ))
Categories
- All Categories
- 2K Product Ideas
- 2K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 311 Workbench
- 7 Cloud Amplifier
- 9 Federated
- 3K Transform
- 114 SQL DataFlows
- 654 Datasets
- 2.2K Magic ETL
- 4.1K Visualize
- 2.5K Charting
- 807 Beast Mode
- 80 App Studio
- 45 Variables
- 763 Automate
- 189 Apps
- 480 APIs & Domo Developer
- 76 Workflows
- 18 DomoAI
- 40 Predict
- 17 Jupyter Workspaces
- 23 R & Python Tiles
- 408 Distribute
- 119 Domo Everywhere
- 279 Scheduled Reports
- 10 Software Integrations
- 142 Manage
- 138 Governance & Security
- 8 Domo Community Gallery
- 47 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 114 Community Announcements
- 4.8K Archive