shuffleet
Home
Practice
Problems
Lessons
Plans
Compete
Challenges
Interviews
Leaderboard
Progress
Skills
Analytics
Submissions
Account
Settings

Slowly changing dimensions

Intermediate · 4 min read

In two sentences

Customers move, products change category, prices change: a slowly changing dimension decides what a table keeps when that happens. Type 1 overwrites the old value; type 2 keeps every version with the dates it was valid, so you can ask what was true at any point in time.

See it

The dimension on 01-01, then a snapshot on 04-01 where c1 moved to Germany and c3 is new:

customer_idcountryvalid_fromvalid_tois_current
c1FR01-0104-01false
c1DE04-01NULLtrue
c2US01-01NULLtrue
c3ES04-01NULLtrue

c1's French version was closed, a German version opened. c2 didn't change, so nothing happened to it.

type 1type 2
on a changeoverwriteclose the current row, add a new one
rows per customer11 per version
"where did c1 live in February?"can't tellFR
sales by country, as of the salewrong for moversright

c1's history on a timeline: the FR version closes on 04-01 and the DE version opens the same day

In SQL

Type 2 in two idempotent steps. First, close the current version of every customer whose attributes changed:

SQL
MERGE INTO dim_customer t
USING snapshot s
ON t.customer_id = s.customer_id AND t.is_current
WHEN MATCHED AND t.country IS DISTINCT FROM s.country THEN
  UPDATE SET valid_to = s.snapshot_date, is_current = false;

Then, open a version for every customer without a current one, the changed ones and the new ones:

SQL
INSERT INTO dim_customer
SELECT s.customer_id, s.country, s.snapshot_date, NULL, true
FROM snapshot s
WHERE NOT EXISTS (
  SELECT 1 FROM dim_customer d WHERE d.customer_id = s.customer_id AND d.is_current
);

Run both again with the same snapshot and nothing changes. If the job dies between the two, the next run opens the missing versions.

Where did c1 live on 02-15? valid_to is exclusive:

SQL
SELECT country FROM dim_customer
WHERE customer_id = 'c1'
  AND valid_from <= DATE '2026-02-15'
  AND (valid_to > DATE '2026-02-15' OR valid_to IS NULL);    -- FR

When to use it

attributetype
corrections (a typo in a name)1: the old value was never true
attributes reports group by over time (country, segment, plan, category)2
attributes nobody reports on historically (phone number)1
prices, contracts, anything with legal or money consequences2, sometimes bitemporal (see below)

Best practices

  • Decide per column, not per table. A customer table is often type 2 on country and segment and type 1 on email.
  • Use a surrogate key for each version (see Grain, facts and dimensions): facts point at the version that was current when they happened.
  • Half-open intervals: valid_from inclusive, valid_to exclusive. A date then falls in exactly one version.
  • Compare with IS DISTINCT FROM, so a change from or to NULL counts as a change.
  • Decide what a deleted customer means: close the current version, or keep it with a deleted flag. Don't just leave it current.

Common mistakes

  • Closing and opening with the same condition in one MERGE. A MERGE takes one action per target row: it can't close c1's row and insert its new version at the same time. Two steps, or a source that lists c1 twice with different keys.
  • Comparing with <>. A country going from NULL to FR is not "different" to <>. Use IS DISTINCT FROM.
  • Closed intervals. valid_to = 04-01 and valid_from = 04-01 with BETWEEN matches both versions on 04-01.
  • Using the load date as valid_from when the source says when it changed. A change loaded a week late gets the wrong history. Use the source's effective date if it has one; that's the start of bitemporal modelling.

What an interviewer asks next

  • "Type 1 or type 2 for customer address?" Depends on whether reports need the address at the time of the order; usually type 2 for country or region, type 1 for the street.
  • "How do you join a fact to a type 2 dimension?" On the surrogate key stored in the fact, or on the natural key plus the fact's date between valid_from and valid_to (see Range joins and as-of joins).

Go further

Kimball's pages on type 1 and type 2 are the original definitions, one screen each.

Practise it

Upsert Daily Customer Changes (SCD Type 1), then Customer History as a Type 2 Slowly Changing Dimension, then Backdated Price Corrections in an SCD2 History (hard).