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?

Advancedโฑ 3 Minutesโญ 800 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