Skip to main content
Back to Academy🔄 2. Enterprise Dimensional Modeling
6 min read
Try Hands-on Lab
VISUAL LESSON2. Enterprise Dimensional Modeling

Slowly Changing Dimensions (SCD Types 0 through 7) in Delta Lake

A complete beginner-to-architect guide to Slowly Changing Dimensions (SCD Types 0 through 7) with real timeline tables, Delta Lake MERGE SQL patterns, and how to report historical facts honestly.

Slowly Changing Dimensions SCDSCD Type 1 vs SCD Type 2SCD Types 0 through 7Delta Lake MERGE INTO SCD2Surrogate Key HistoryTemporal Table effective_from effective_to

Explain Like I'm New: Moving Houses & Honest Tax Records

Suppose customer Maya lived in London throughout 2025 and spent $1,000. Then on June 1, 2026, she moved to Manchester.

Look at what happens depending on how your Dimension table handles her change: - SCD Type 1 (Erase & Overwrite): You simply replace 'London' with 'Manchester'. Suddenly, your 2025 regional report shifts $1,000 into Manchester and makes London look smaller retroactively! Great for fixing spelling typos—terrible for historical truth. - SCD Type 2 (Add a New Versioned Row): You close Maya's London row (effective_to = 2026-05-31, is_current = false) and insert a brand new row with a fresh Surrogate Key (customer_sk = 2002, city = 'Manchester', effective_from = 2026-06-01, is_current = true). Her 2025 orders still join to customer_sk = 2001 (London), while her 2026+ orders join to customer_sk = 2002 (Manchester)!

Key Beginner Takeaway: SCD Type 1 forgets history cleanly when only the latest value matters; SCD Type 2 preserves full historical truth using effective_from, effective_to, is_current, and Surrogate Keys.

Visual Comparison Table: Maya's `dim_customer` in SCD Type 2

customer_sk (Surrogate PK)customer_id (Natural Key)cityeffective_fromeffective_tois_current
1001C-42London2025-01-012026-05-31false
1002C-42Manchester2026-06-019999-12-31true

Notice two crucial engineering details beginners miss: 1. Both rows share customer_id = 'C-42', so customer_id is NO LONGER unique! That is why Surrogate Keys (`customer_sk`) are mandatory in SCD Type 2 tables. 2. Notice the end date 9999-12-31 (instead of NULL) on the active row—this makes time-travel joins effortless with clean BETWEEN: ON fact.order_date BETWEEN dim.effective_from AND dim.effective_to!


Complete Taxonomy: The Standard SCD Types (0 through 7)

In interviews and production banking/retail architectures, you will encounter all 8 official Kimball SCD patterns:

  • SCD Type 0 (Fixed / Retain Original): Attribute is strictly immutable after creation (e.g., signup_date, birth_date, original_credit_score). Changes are ignored or rejected.
  • SCD Type 1 (In-Place Overwrite): Overwrites the old attribute cleanly in place (UPDATE). Zero historical lineage preserved. Best for correcting data-entry typos or phone numbers where history has zero analytical value.
  • SCD Type 2 (Historical Row Versioning): Inserts a new versioned row with a fresh Surrogate Key, setting effective_from, effective_to = '9999-12-31', and is_current = true while retiring the previous active row (is_current = false). The gold standard for auditability!
  • SCD Type 3 (Previous Value Column): Keeps just 1 row per customer, adding a previous_city column alongside current_city. Only tracks 1 step back (useful for quick "Before vs. After territory reorganization" comparisons).
  • SCD Type 4 (Historical Minidimension): Splits fast-changing attributes (e.g., monthly_income_bracket, credit_tier, loyalty_score that change every week) out into a slim separate minidimension table so a 50M-row customer table doesn't explode to 2B rows!
  • SCD Type 5 (Type 4 + Type 1 Current-Value Overlay): Combines a Type 4 Minidimension *plus* embeds a current_profile_fk pointer inside the base Type 1 dimension for fast current-state slicing without extra joins.
  • SCD Type 6 (Hybrid Type 1 + Type 2 + Type 3): Combines all three: tracks full Type 2 historical rows *while also* updating a current_city column across *all* historical versions so analysts can query either "as-was" (historical_city) or "as-is" (current_city) from the same row!
  • SCD Type 7 (Dual Keys / Hybrid Type 1 & Type 2): Fact tables store both the versioned Surrogate Key (customer_sk for historical Type 2 joins) *and* the durable Natural/Durable Key (customer_durable_id joined to a is_current = true Type 1 view). Maximum flexibility for BI tools!

Step-by-Step Delta Lake `MERGE INTO` Implementation (SCD Type 2)

Implementing SCD Type 2 cleanly requires handling two actions per changed record: (1) expire the active old row, and (2) insert the new active row.

The safest zero-bug pattern in Delta Lake / Spark SQL uses a staged_updates CTE with a merge_key:

WITH staged_updates AS (
  -- Action A: Rows to INSERT (brand new customers + new versions of changed customers)
  SELECT
    s.customer_id AS merge_key,
    s.customer_id,
    s.city,
    s.updated_date AS effective_from
  FROM silver.staging_customers s

  UNION ALL

  -- Action B: NULL merge_key forces Delta MERGE to NOT match on target PK,
  -- allowing the matched row to be expired while the NULL-key row inserts cleanly!
  SELECT
    NULL AS merge_key,
    s.customer_id,
    s.city,
    s.updated_date AS effective_from
  FROM silver.staging_customers s
  JOIN gold.dim_customer d
    ON s.customer_id = d.customer_id AND d.is_current = true
  WHERE s.city <> d.city
)
MERGE INTO gold.dim_customer AS target
USING staged_updates AS src
  ON target.customer_id = src.merge_key AND target.is_current = true
WHEN MATCHED AND target.city <> src.city THEN
  UPDATE SET
    target.is_current   = false,
    target.effective_to = DATE_SUB(src.effective_from, 1)
WHEN NOT MATCHED THEN
  INSERT (customer_id, city, effective_from, effective_to, is_current)
  VALUES (src.customer_id, src.city, src.effective_from, DATE '9999-12-31', true);

Common Beginner Mistakes (And How to Fix Them)

  1. Overlapping date intervals (`effective_to >= next effective_from`): If London ends 2026-06-01 and Manchester starts 2026-06-01, any BETWEEN join on 2026-06-01 matches both rows and doubles your revenue! Always use exclusive end dates (<) or DATE_SUB(new_start, 1) so intervals never overlap.
  2. Comparing nullable attributes with `<>`: Remember NULL <> 'New York' evaluates to UNKNOWN, silently skipping attribute changes! Always compare using `IS DISTINCT FROM` (WHERE s.city IS DISTINCT FROM d.city).
  3. Putting rapidly changing timestamps inside SCD Type 2: Putting last_login_timestamp into an SCD Type 2 table creates a brand new customer row on every login! Move volatile telemetry columns out of tracked dimensions.

Quick Cheat Sheet

  1. Use Type 1 for typo fixes & non-historical updates; Type 2 when historical truth matters.
  2. Always generate Surrogate Keys (customer_sk) for Type 2 dimensions because Natural IDs repeat.
  3. Prevent overlapping dates and use `IS DISTINCT FROM` to catch NULL -> non-NULL transitions cleanly.

Comprehension & Architecture Check (3 Questions)

Test your understanding of the concepts covered in this lesson.

1. In the Global DynaDatum Bank case study, joining product catalog without effective date bounds caused £8M to be misclassified. Which SCD type fixes this by enforcing date ranges?
2. Why should beginners check a table's grain before writing a JOIN?
3. Which Medallion layer is meant to store clean, deduplicated, standardized records?
Practice What You Learned

EX-2: SCD Type 2 Temporal Joins: Fix the £8M Cash ISA Discrepancy

The bank's monthly executive report was suddenly missing £8,000,000 in reported customer savings for May 31, 2026. The cause was traced to a join mistake: on July 1, the product team updated the product catalog with new category names (calling Cash ISAs "Investments" instead of "Savings"). Because the report joined balances to products using only the product code without checking dates, past May balances were matched to future July categories, duplicating rows and putting £8M into the wrong bucket.

Solve Challenge