Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Total revenue for multiple periods
Calculating deltas
Creating "revenue in quarters" reports


Very well done! Let's try one more exercise before we move on.


For each month of 2017, calculate the total monthly revenue from orders shipped to the USA and the percentage revenue change (delta) as compared to the previous month. Show three columns: RevenueMonth, TotalRevenue, and DeltaPercentage.

In the first row, leave the delta value as NULL. Order the rows by month. Here's how you can count DeltaPercentage:

\frac{\textrm{total amount} - \textrm{total amount from preceding months}}{\textrm{total amount from preceding months}} \cdot 100

Remember to round the result to the second decimal point.

Stuck? Here's a hint!

Use the following expression in the last column:

  100 * 
  (SUM(Amount) - LAG(SUM(Amount), 1) OVER (ORDER BY DATEPART(Month, OrderDate))
  / CAST(LAG(SUM(Amount), 1) OVER (ORDER BY DATEPART(Month, OrderDate)) AS decimal(10,2))),
  2) AS DeltaPercentage