How can we calculate year-over-year revenue growth percentages across monthly time series data?
lag('monthly_revenue', 12) retrieves the exact revenue from the corresponding month in the previous year.
Financial reporting, executive dashboards, and macroeconomic trend tracking.
w = Window.partitionBy("business_unit").orderBy("month_id")
df_yoy = df.withColumn("prev_year_revenue", lag("monthly_revenue", 12).over(w)).withColumn("yoy_growth_pct", ((col("monthly_revenue") - col("prev_year_revenue")) / col("prev_year_revenue")) * 100)Practice typing production-grade PySpark code for Year-over-Year Growth with lag(12).