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.
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)!
effective_from, effective_to, is_current, and Surrogate Keys.| customer_sk (Surrogate PK) | customer_id (Natural Key) | city | effective_from | effective_to | is_current |
|---|---|---|---|---|---|
| 1001 | C-42 | London | 2025-01-01 | 2026-05-31 | false |
| 1002 | C-42 | Manchester | 2026-06-01 | 9999-12-31 | true |
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!
In interviews and production banking/retail architectures, you will encounter all 8 official Kimball SCD patterns:
signup_date, birth_date, original_credit_score). Changes are ignored or rejected.UPDATE). Zero historical lineage preserved. Best for correcting data-entry typos or phone numbers where history has zero analytical value.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!previous_city column alongside current_city. Only tracks 1 step back (useful for quick "Before vs. After territory reorganization" comparisons).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!current_profile_fk pointer inside the base Type 1 dimension for fast current-state slicing without extra joins.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!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!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);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.NULL <> 'New York' evaluates to UNKNOWN, silently skipping attribute changes! Always compare using `IS DISTINCT FROM` (WHERE s.city IS DISTINCT FROM d.city).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.customer_sk) for Type 2 dimensions because Natural IDs repeat.Test your understanding of the concepts covered in this lesson.
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.