Skip to main content
PYSPARK โ€ข LESSON 88

Business Dimension Summary Pipeline

How do we aggregate headcount and annualized budget per department and filter for established teams?

Intermediateโฑ 3 Minutesโญ 490 XP
๐Ÿค” THE QUESTION

How do we aggregate headcount and annualized budget per department and filter for established teams?

๐Ÿ’ก WHAT IS IT?

A complete dimensional aggregation pipeline grouping by department, computing annual budgets, and filtering.

๐ŸŽฏ WHAT IS IT USED FOR?

Enterprise financial planning, departmental cost allocation, and resource governance.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, countDistinct, round, sum

dept_metrics_df = df.groupBy("department_id") \
    .agg(
        countDistinct(col("employee_id")).alias("headcount"),
        round(sum(col("monthly_salary")) * 12, 2).alias("annual_budget")
    ) \
    .filter(col("headcount") >= 5)
dept_metrics_df.show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Business Dimension Summary Pipeline.

  • โœ“ Group by department ID
  • โœ“ Calculate distinct headcount and annual budget
  • โœ“ Filter departments with at least 5 staff