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?

Advanced3 Minutes860 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