Skip to main content
PYSPARK • LESSON 113

Cumulative Running Totals with Window Frames

How do we calculate a running cumulative spend total for each customer from their first order to the current order?

Advanced2 Minutes600 XP
🤔 THE QUESTION

How do we calculate a running cumulative spend total for each customer from their first order to the current order?

💡 WHAT IS IT?

Using rowsBetween(Window.unboundedPreceding, Window.currentRow) to bound calculations from partition start to current row.

🎯 WHAT IS IT USED FOR?

Lifetime customer value tracking, cumulative revenue burn rates, and financial ledger running balances.

💻 EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import col, sum

window_spec = Window.partitionBy("customer_id") \
    .orderBy("order_date") \
    .rowsBetween(Window.unboundedPreceding, Window.currentRow)

df = df.withColumn("running_total", sum(col("amount")).over(window_spec))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Cumulative Running Totals with Window Frames.

  • Define unboundedPreceding frame specification
  • Calculate running cumulative sum
  • Track customer cumulative spending