Skip to main content
PYSPARK • LESSON 231

Cumulative Running Totals with Unbounded Frames

How can we compute a running cumulative sum of revenue per customer across their entire purchase history?

Advanced3 Minutes800 XP
🤔 THE QUESTION

How can we compute a running cumulative sum of revenue per customer across their entire purchase history?

💡 WHAT IS IT?

Window.partitionBy().orderBy().rowsBetween(Window.unboundedPreceding, Window.currentRow) accumulates values up to the current row.

🎯 WHAT IS IT USED FOR?

Tracking lifetime customer spend, cumulative financial ledgers, and progressive budget utilization.

💻 EXAMPLE
w = Window.partitionBy("customer_id").orderBy("order_date").rowsBetween(Window.unboundedPreceding, Window.currentRow)
df_cumulative = df.withColumn("running_total", sum("amount").over(w))

🎯 Mission Objectives

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

  • unboundedPreceding frame
  • Cumulative sum calculation
  • Temporal progressive accumulation