Current Day vs Previous Day in Table

Hi Everyone 



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







Thanks in advance 


Best Answer

  • Gimli



    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.


    ,SUM(`Sales`) As 'Sales'

    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. 


    ,a.`Sales` AS 'Current_Day'
    ,b.`Sales` As 'Previous_Day'

    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)


    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"


  • mdelorey



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





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