how to create a table like below by using date set above
Thanks in advance
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_datasetGROUP BY `Date`, `Product`
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 aLEFT JOIN transform_data_1 bON 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)
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.
@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?
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.
@iQuanti, feel free to open up a new thread for your new question for better exposure ?
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
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?