Skip to main content
PYSPARK • LESSON 118

Cumulative Percentage of Total Share

How do we compute rounded percentage of revenue contributed by each product within its department?

Advanced2 Minutes690 XP
🤔 THE QUESTION

How do we compute rounded percentage of revenue contributed by each product within its department?

💡 WHAT IS IT?

Combining partition window sums, multiplication by 100, and the round() function to calculate percentage shares.

🎯 WHAT IS IT USED FOR?

Merchandising revenue concentration analysis and identifying top revenue drivers.

💻 EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import col, round, sum

dept_window = Window.partitionBy("department")
df = df.withColumn("pct_dept_share", round(col("revenue") / sum(col("revenue")).over(dept_window) * 100, 2))

🎯 Mission Objectives

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

  • Calculate department total with window sum
  • Compute and round percentage share
  • Produce merchandising revenue breakdown