7-Day Moving Average of Sales (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: Window Functions · +100 XP
Problem
Daily sales bounce around too much to read a trend from. Smooth them with a 7-day moving average: each row averages itself and the six days before it.
Schema — `daily_traffic`
| Column | | --- | | order_date | | sales |
Example Input — `daily_traffic`
| order_date | sales | | --- | --- | | 2024-06-01 | 100 | | 2024-06-02 | 150 | | 2024-06-03 | 120 | | 2024-06-04 | 180 | | 2024-06-05 | 200 | | 2024-06-06 | 160 | | 2024-06-07 | 140 | | 2024-06-08 | 220 | | 2024-06-09 | 190 | | 2024-06-10 | 210 |
Expected Output
| order_date | sales | moving_avg_7day | | --- | --- | --- | | 2024-06-01 | 100 | 100.0 | | 2024-06-02 | 150 | 125.0 | | 2024-06-03 | 120 | 123.33 | | 2024-06-04 | 180 | 137.5 | | 2024-06-05 | 200 | 150.0 | | 2024-06-06 | 160 | 151.67 | | 2024-06-07 | 140 | 150.0 | | 2024-06-08 | 220 | 167.14 | | 2024-06-09 | 190 | 172.86 | | 2024-06-10 | 210 | 185.71 |
Explanation
The first six rows have fewer than seven days behind them, so they average however many exist — day 1 averages 1 value, day 2 averages 2, and only from day 7 onward is it a true 7-day average.
Notes
- The DataFrame is created for you — do not recreate it
- Build a DataFrame called
df_resultand finish withdf_result.show() - Same problem in SQL: [7-Day Moving Average of Sales](/challenges/7-day-moving-average-of-sales)
What this PYSPARK challenge teaches you
“7-Day Moving Average of Sales (PySpark)” is a medium-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with Window, avg, rowsBetween, Moving Average — 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
- Window
- avg
- rowsBetween
- Moving Average
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:
- rowsBetween(-6, 0) covers the current row and the six before it.
- Without rowsBetween you get a running average over all previous rows.
- F.round(F.avg('sales').over(w), 2).
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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
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.