The accounting department needs monthly sales numbers. You must extract the year and month from raw purchase timestamps and calculate total sales grouped by month.
The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):
| order_id | order_date | amount |
|---|---|---|
| 1 | 2026-01-15 | 150.0 |
| 2 | 2026-01-20 | 120.0 |
| 3 | 2026-02-05 | 300.0 |
| 4 | 2026-02-18 | 220.0 |
| 5 | 2026-03-12 | 450.0 |
order_id,order_date,amount 1,2026-01-15,150.0 2,2026-01-20,120.0 3,2026-02-05,300.0 4,2026-02-18,220.0 5,2026-03-12,450.0
Copy this starter template to write the data transformation logic:
# starter.py - Monthly Sales Report
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, to_date, date_format, sum
def monthly_sales():
spark = SparkSession.builder.appName("MonthlySales").getOrCreate()
# TODO: Read dataset.csv, cast dates, format to YYYY-MM, sum amount
pass
if __name__ == "__main__":
monthly_sales()order_date string column into a Date type using to_date(col("order_date"), "yyyy-MM-dd").
yyyy-MM format string using date_format().
year_month, calculate the sum of transaction amounts, and sort sequentially.