transactions holds every purchase, with its created_at date (as text) and its value. For each month, find the total revenue and the percentage change from the month before, rounded to 2 decimal places. Return result with the columns year_month (text such as 2025-01), revenue and pct_change, in month order. The first month has no month before it, so its pct_change is empty (NaN).
The data
transactions16 rows
| idint64 | user_idint64 | created_atobject | valueint64 |
|---|
| 1 | 7 | 2025-03-09 | 310 |
| 2 | 3 | 2025-01-04 | 120 |
| 3 | 9 | 2025-02-14 | 230 |
| 4 | 3 | 2025-01-27 | 200 |
| 5 | 12 | 2025-03-21 | 170 |
| 6 | 7 | 2025-04-05 | 210 |
and 10 more rows
Target output
| year_month | revenue | pct_change |
|---|
| 2025-01 | 400 | NaN |
| 2025-02 | 380 | -5 |
| 2025-03 | 630 | 65.79 |
| 2025-04 | 350 | -44.44 |
| 2025-05 | 675 | 92.86 |
| 2025-06 | 590 | -12.59 |
What this question tests
Tests dates end to end: text to datetime, a month key that sorts correctly across a year boundary, a total per month and a change against the row before. Revenue movement is one of the most common analyst questions there is, and the month key is where most answers quietly go wrong.
More Python interview questions