Current Day vs Previous Day in Table

Hi Everyone 

 

DateSalesproduct
6/13/201621A
6/12/201620A


how to create a table  like below  by using date set above 

 

Output                              

                          

ProductCurrent_Dayprevious_dayDoD
A21205%

 

 

Thanks in advance 

 

Best Answer

  • ilikenno
    ilikenno

    domo

    💎

    Accepted 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. 

Answers

  • mdelorey
    mdelorey

    domo

    💎

    @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.

     

    DateCurrent_Dayprevious_daylast 30 days average
    6/1/2015202225

     

     

    Regards,

    iQuanti

  • @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

  • 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?