Skip to main content

Slowly changing dimensions (SCD) cheatsheet

The types​

TypeWhat happens on changeHistory?Use when
0Nothing. The original value is kept foreverOriginal onlyValues that must never change (signup date, original channel)
1OverwriteNoneCorrections, or attributes where only the current value matters
2Close the old row, insert a new rowFullReporting by the value at the time (region at order time)
3Keep a previous_value columnOne stepRare; a single "before and after" comparison, like a reorg
4Current table plus a separate history tableFull, separateVery large or fast-changing dimensions
6Type 2 rows plus a Type 1 "current" column (1 + 2 + 3)Full, plus current on every rowBoth "then" and "now" reporting without a self-join

You can mix types in one dimension: Type 2 for region, Type 1 for email.

SCD Type 2 columns​

customer_key -- surrogate key, one per version
customer_id -- natural/business key, same across versions
region, ... -- tracked attributes
valid_from -- inclusive
valid_to -- exclusive; '9999-12-31' for the current version
is_current -- convenience flag

Join a fact to the version valid at the event time:

JOIN dim_customer d
ON d.customer_id = f.customer_id
AND f.order_date >= d.valid_from
AND f.order_date < d.valid_to

Or store customer_key on the fact at load time, which is faster to query but fixes the version when the row loads.

Load pattern​

  1. Compute a hash of the tracked columns for incoming rows.
  2. Compare with the current version (is_current = true) by natural key.
  3. Changed: set valid_to = change time, is_current = false on the old row, and insert a new row.
  4. New key: insert.
  5. Unchanged: do nothing. This matters, because no-op updates create fake versions.
  6. Do it in one MERGE or one transaction.

dbt snapshots implement this pattern for you (timestamp or check strategy).

Gotchas​

  • No-op changes (same values re-sent) must not create versions. Compare hashes.
  • Late-arriving changes (a change dated before the current version started) need the timeline re-cut, and possibly facts re-keyed. Decide the policy up front.
  • Deletes: close the version and mark is_deleted, or remove history for GDPR. Know which one the law requires.
  • Use a far-future valid_to, not NULL. It keeps range joins simple and index-friendly.
  • Don't track noisy columns (last_login_at) as Type 2, or the table explodes.

Practice​