Skip to main content
PYSPARK โ€ข LESSON 285

Scenario: Slowly Changing Dimension (SCD Type 2) Ingestion

How can we maintain historical attribute changes with effective_date, end_date, and is_current flags (SCD Type 2)?

Productionโฑ 3 Minutesโญ 840 XP
๐Ÿค” THE QUESTION

How can we maintain historical attribute changes with effective_date, end_date, and is_current flags (SCD Type 2)?

๐Ÿ’ก WHAT IS IT?

Comparing incoming dimension updates with current records to close out old versions (end_date) and insert new versions.

๐ŸŽฏ WHAT IS IT USED FOR?

Tracking customer address changes, sales rep territory reassignments, and product price history in data warehouses.

๐Ÿ’ป EXAMPLE
df_updates = df_incoming.withColumn("effective_date", current_date()).withColumn("end_date", to_date(lit("9999-12-31"))).withColumn("is_current", lit(True))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Scenario: Slowly Changing Dimension (SCD Type 2) Ingestion.

  • โœ“ SCD Type 2 pattern
  • โœ“ effective_date and end_date handling
  • โœ“ Historical dimension tracking