Skip to main content
PYSPARK โ€ข LESSON 79

Filtering Prior to Aggregation

How do we filter for shipped orders before calculating realized net revenue?

Intermediateโฑ 2 Minutesโญ 420 XP
๐Ÿค” THE QUESTION

How do we filter for shipped orders before calculating realized net revenue?

๐Ÿ’ก WHAT IS IT?

Chaining filter() before aggregate select() to restrict metric calculations to valid status subsets.

๐ŸŽฏ WHAT IS IT USED FOR?

Realized financial revenue accounting, excluding pending or cancelled orders from metrics.

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

df.filter(col("status") == "SHIPPED") \
  .select(sum(col("amount")).alias("realized_revenue")) \
  .show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Filtering Prior to Aggregation.

  • โœ“ Filter shipped status first
  • โœ“ Compute aggregate sum on filtered subset
  • โœ“ Display realized revenue