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?

Advancedโฑ 2 Minutesโญ 690 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