Skip to main content
PYSPARK • LESSON 238

Percentage of Partition Total

How can we calculate what percentage of regional revenue each individual sales representative generated?

Advanced3 Minutes870 XP
🤔 THE QUESTION

How can we calculate what percentage of regional revenue each individual sales representative generated?

💡 WHAT IS IT?

Dividing individual sales by sum('sales') over an un-ordered partition computes percentage contribution in a single pass.

🎯 WHAT IS IT USED FOR?

Commission calculations, market share distribution, and budget allocation reporting.

💻 EXAMPLE
w = Window.partitionBy("region")
df_pct = df.withColumn("regional_total", sum("sales").over(w)).withColumn("pct_contribution", (col("sales") / col("regional_total")) * 100)

🎯 Mission Objectives

Practice typing production-grade PySpark code for Percentage of Partition Total.

  • sum() over partition without order
  • Percentage contribution calculation
  • Single-pass ratio analysis