Skip to main content
PYSPARK โ€ข LESSON 237

First and Last Values with Frame Boundaries

How can we compare a customer's current order value against their very first order value?

Advancedโฑ 3 Minutesโญ 860 XP
๐Ÿค” THE QUESTION

How can we compare a customer's current order value against their very first order value?

๐Ÿ’ก WHAT IS IT?

first('amount') over an unbounded window extracts the earliest recorded transaction value for comparative analysis.

๐ŸŽฏ WHAT IS IT USED FOR?

Customer lifetime value progression, initial vs current pricing comparisons, and onboarding performance.

๐Ÿ’ป EXAMPLE
w = Window.partitionBy("customer_id").orderBy("order_date").rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)
df_baseline = df.withColumn("initial_order_amount", first("amount").over(w))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for First and Last Values with Frame Boundaries.

  • โœ“ first() over window
  • โœ“ Initial state retention
  • โœ“ Baseline comparison analytics