12-Month Rolling Revenue (PySpark)

PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP

Problem

Finance reports revenue on a trailing twelve month basis: for each month, the sum of that month and the eleven before it.

Schema — `monthly_revenue`

| Column |
| --- |
| month_start |
| revenue |

Example Input — `monthly_revenue`

| month_start | revenue |
| --- | --- |
| 2023-01 | 100 |
| 2023-02 | 120 |
| 2023-03 | 110 |
| 2023-04 | 130 |
| 2023-05 | 150 |
| 2023-06 | 140 |
| 2023-07 | 160 |
| 2023-08 | 170 |
| 2023-09 | 155 |
| 2023-10 | 180 |
| 2023-11 | 190 |
| 2023-12 | 200 |
| 2024-01 | 210 |
| 2024-02 | 220 |

Expected Output

| month_start | revenue | rolling_12month_revenue |
| --- | --- | --- |
| 2023-01 | 100 | 100 |
| 2023-02 | 120 | 220 |
| 2023-03 | 110 | 330 |
| 2023-04 | 130 | 460 |
| 2023-05 | 150 | 610 |
| 2023-06 | 140 | 750 |
| 2023-07 | 160 | 910 |
| 2023-08 | 170 | 1080 |
| 2023-09 | 155 | 1235 |
| 2023-10 | 180 | 1415 |
| 2023-11 | 190 | 1605 |
| 2023-12 | 200 | 1805 |
| 2024-01 | 210 | 1915 |
| 2024-02 | 220 | 2015 |

Explanation

The first eleven months do not have twelve months of history behind them, so they sum however many exist — month 1 sums 1 value, month 2 sums 2. Only from the twelfth month onward is it a true 12-month total, and from month 13 the window starts dropping the oldest month as it gains a new one.

Notes

What this PYSPARK challenge teaches you

“12-Month Rolling Revenue (PySpark)” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window, sum, rowsBetween, Rolling Window — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. rowsBetween(-11, 0) covers this row and the eleven before it.
  2. Without rowsBetween you get a running total, not a rolling one.
  3. F.sum('revenue').over(w).

How to practise it on PySpark.in

Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related PYSPARK challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated medium and covers Window Functions.

Solve this challenge free on PySpark.in