cswork / libraryOpen practice workspace

Coding questions · Meta

Forecast Next Year's Revenue Using YoY% Analysis

This question evaluates data manipulation and time-series analysis skills, including rolling aggregations and year-over-year percentage calculations using SQL or Python.

ad_revenue

+------------+---------+
| date       | revenue |
+------------+---------+
| 2023-01-01 | 1000    |
| 2023-01-02 | 1200    |
| 2024-01-01 | 1500    |
| 2024-01-02 | 1600    |
| 2024-01-03 | 1550    |
+------------+---------+
Scenario

Advertising platform wants to understand and forecast revenue trends.

Question

Using table ad_revenue(date, revenue) compute the 30-day rolling sum of revenue for every calendar day. For each day, calculate the year-over-year percentage change (YoY%). Based on the latest YoY% figure, project total revenue for the next calendar year.

Hints

Apply WINDOW functions (SUM OVER, LAG, DATEADD) and extrapolate with YoY growth.

30-day Rolling Ad Revenue YoY and Forecast

Using table ad_revenue(date, revenue) compute the 30-day rolling sum of revenue for every calendar day between the minimum and maximum dates in the data, including days with no rows as 0 revenue. For each day, calculate the year-over-year percentage change (YoY%) based on the 30-day rolling sums versus the same calendar date in the prior year. Then, based on the latest available YoY% figure, project total revenue for the next calendar year using the last fully completed calendar year as the base. For validation, return the daily rolling-revenue and YoY metric rows; the next-year projection can be discussed from the latest YoY value.

Table ad_revenue

columntypeconstraints
dateDATE
revenueINTEGER

Sample data:

daterevenue
2023-01-011000
2023-01-021200
2024-01-011500
2024-01-021600
2024-01-031550

Expected output

For each calendar day between the minimum and maximum dates in the sample data, output the raw revenue for that date, the 30-day rolling sum, the prior-year rolling sum on the same calendar date, and YoY% rounded to two decimals.

dateprior_year_rolling_30d_revenuerevenuerolling_30d_revenueyoy_pct
2023-01-01NULL10001000NULL
2023-01-02NULL12002200NULL
2023-01-03NULL02200NULL
2024-01-0110001500150050
2024-01-0222001600310040.91
2024-01-03220015504650111.36

Hints

  1. Build a date spine from MIN(date) to MAX(date) to ensure every calendar day is present.
  2. Fill missing days with 0 revenue before computing rolling sums.
  3. Use SUM(revenue) OVER (ORDER BY date ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) for a 30-day rolling window.
  4. Join to the prior year's date using DATEADD(year, -1, date) to fetch the prior-year 30-day rolling sum.
  5. Guard against division by zero or NULL when computing YoY%.
  6. Project next year's total by applying the latest YoY% to the last fully completed year's total revenue.