Current Day vs Previous Day in Table
Best Answer
-
Hello @iQuanti,
This can be accomplished through a SQL dataflow.
You will have two transforms.
Transform 1 will be to group your data down to a day view.SELECT
`Date`
,SUM(`Sales`) As 'Sales'
,`Product`
FROM input_dataset
GROUP BY `Date`, `Product`**Key notes
- We can add more columns into the Select statement. We need to ensure they are added in the GROUP BY as well as the JOIN of the next transform
- Your value column, (`sales` in this example) needs to have an aggregation. In this example I use SUM()
- If your Date column is a Date/Time we will need to remove the time value. This can be accomplished by wrapping your column in DATE(). -- DATE(`Date`) -- This will also need to be done in the group by.
Transform 2 is were we join the data back onto itself.
SELECT
a.`Date`
,a.`Sales` AS 'Current_Day'
,b.`Sales` As 'Previous_Day'
,a.`Product`
FROM transform_data_1 a
LEFT JOIN transform_data_1 b
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 DAY)
AND a.`Product` = b.`Product`Your output of your dataflow would simply be:
SELECT * FROM transform_data_2
This also works for other date comparisons. If you wanted to compare today vs same day last week, we would only change the time key word:
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 DAY)
To:
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 WEEK)
Doing a total for a week compared to total for last week would require a change in our Grouping as well as in the JOIN conditions.
**Say “Thanks" by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1
Answers
-
@iQuanti: Can you clarify what you're trying to do with the data? Right now it looks like the data has one row per day per product, and you're looking to get it to have one row per product that shows the current and previous day level as well as the percent change between current and previous day. Is that correct?
Retired Domo Alum0 -
Hello @iQuanti,
This can be accomplished through a SQL dataflow.
You will have two transforms.
Transform 1 will be to group your data down to a day view.SELECT
`Date`
,SUM(`Sales`) As 'Sales'
,`Product`
FROM input_dataset
GROUP BY `Date`, `Product`**Key notes
- We can add more columns into the Select statement. We need to ensure they are added in the GROUP BY as well as the JOIN of the next transform
- Your value column, (`sales` in this example) needs to have an aggregation. In this example I use SUM()
- If your Date column is a Date/Time we will need to remove the time value. This can be accomplished by wrapping your column in DATE(). -- DATE(`Date`) -- This will also need to be done in the group by.
Transform 2 is were we join the data back onto itself.
SELECT
a.`Date`
,a.`Sales` AS 'Current_Day'
,b.`Sales` As 'Previous_Day'
,a.`Product`
FROM transform_data_1 a
LEFT JOIN transform_data_1 b
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 DAY)
AND a.`Product` = b.`Product`Your output of your dataflow would simply be:
SELECT * FROM transform_data_2
This also works for other date comparisons. If you wanted to compare today vs same day last week, we would only change the time key word:
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 DAY)
To:
ON a.`Date` = DATE_ADD(b.`Date`, INTERVAL 1 WEEK)
Doing a total for a week compared to total for last week would require a change in our Grouping as well as in the JOIN conditions.
**Say “Thanks" by clicking the thumbs up in the post that helped you.
**Please mark the post that solves your problem by clicking on "Accept as Solution"1 -
I really appreciate your help in resolving the issue ilikenno. Could you please solve the one more request from me, how to add last 30 days average in the same table by using SQL Dataflow.
Date Current_Day previous_day last 30 days average 6/1/2015 20 22 25 Regards,
iQuanti
0 -
I really like the way you did this, next is capturing the difference from day to day so all I did was say
(a.users - b.users) as 'Change' and I get my day to day change
0 -
Hi,
When using this solution I am running into the issue with the "1" in the "interval 1 day" part of the formula and seeing the following error message - "The database reported a syntax error. ERROR: syntax error at or near "'Date'" Position: 83". Is there a special way to write out this part to avoid this error?
0
Categories
- All Categories
- 1.8K Product Ideas
- 1.8K Ideas Exchange
- 1.5K Connect
- 1.2K Connectors
- 300 Workbench
- 6 Cloud Amplifier
- 8 Federated
- 2.9K Transform
- 100 SQL DataFlows
- 616 Datasets
- 2.2K Magic ETL
- 3.9K Visualize
- 2.5K Charting
- 738 Beast Mode
- 57 App Studio
- 40 Variables
- 685 Automate
- 176 Apps
- 452 APIs & Domo Developer
- 47 Workflows
- 10 DomoAI
- 36 Predict
- 15 Jupyter Workspaces
- 21 R & Python Tiles
- 394 Distribute
- 113 Domo Everywhere
- 275 Scheduled Reports
- 6 Software Integrations
- 124 Manage
- 121 Governance & Security
- 8 Domo Community Gallery
- 38 Product Releases
- 10 Domo University
- 5.4K Community Forums
- 40 Getting Started
- 30 Community Member Introductions
- 108 Community Announcements
- 4.8K Archive