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?

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