Type 2: Preserve Full History
Type 2 keeps the old row and adds a new one. Each change to a tracked attribute produces a fresh row with its own surrogate key, and the row it replaces is marked as closed. One row now means one version of a host, valid for a stated window of time.
- dim_host: host_key BIGINT PK, host_id BIGINT, host_name VARCHAR, tier VARCHAR, valid_from DATE, valid_to DATE, is_current BOOLEAN
- fact_booking: booking_id BIGINT PK, host_key BIGINT FK, booking_date DATE, amount DECIMAL(10,2)
- fact_booking.host_key references dim_host.host_key (N:1)
3 columns do the bookkeeping. valid_from is the first day a version applies. valid_to is the day the next version takes over, so a version covers valid_from up to but excluding valid_to. is_current flags the open version. The open row carries valid_to = 9999-12-31, a far-future date that lets one range condition match open and closed rows alike.
| host_key | host_id | tier | valid_from | valid_to | is_current |
|---|---|---|---|---|---|
| 101 | 4417 | Standard | 2023-06-12 | 2025-04-01 | false |
| 104 | 4417 | Superhost | 2025-04-01 | 9999-12-31 | true |
| 102 | 5230 | Superhost | 2022-09-05 | 9999-12-31 | true |
Marta now has 2 rows. host_id 4417 appears twice, which is why the natural key can no longer be the primary key. The surrogate host_key identifies a version, and host_id ties the versions of one host together.
What the load does on a change
When the nightly load finds that host 4417's incoming tier differs from her open row, it runs 2 statements. The first closes the open version.
UPDATE dim_host
SET
valid_to = DATE '2025-04-01',
is_current = FALSE
WHERE host_id = 4417
AND is_current = TRUEThe second statement inserts the new version under a new surrogate key, starting on the day the old one ended.
INSERT INTO dim_host (host_key, host_id, host_name, tier, valid_from, valid_to, is_current)
VALUES (104, 4417, 'Marta Reis', 'Superhost', DATE '2025-04-01', DATE '9999-12-31', TRUE)One version ends on the date the next begins, and the end date is excluded. The windows for a host therefore never overlap and never leave a gap, and any date maps to exactly one version.
How facts find the right version
The source system sends each booking with a host_id and a date. The fact load translates that pair into a host_key by finding the version whose window contains the booking date.
SELECT
s.booking_id,
h.host_key,
s.booking_date,
s.amount
FROM staged_booking s
JOIN dim_host h
ON s.host_id = h.host_id
AND s.booking_date >= h.valid_from
AND s.booking_date < h.valid_toBookings 9001 and 9002 happened in February and March, so they keep host_key = 101, the Standard version. Booking 9004 on April 10 receives 104. The date logic runs once, at load time. After that, reports use the same equality join on host_key as before, and the first-quarter report returns Standard 800 and Superhost 400 no matter when it runs.
Both views from one table
A Type 2 dimension still answers as-is questions. Join each fact to its version, then hop through host_id to the same host's current row and group by that row's tier.
SELECT
cur.tier AS tier_today,
SUM(b.amount) AS revenue
FROM fact_booking b
JOIN dim_host ver
ON b.host_key = ver.host_key
JOIN dim_host cur
ON ver.host_id = cur.host_id
AND cur.is_current = TRUE
GROUP BY cur.tierOn the 4 bookings from the opening example, this query returns a single line, Superhost 1800, because both hosts hold that tier today. The as-is view costs one extra join.
Host 4417 has version 101 (Standard, 2023-06-12 up to 2025-04-01) and version 104 (Superhost, from 2025-04-01). A booking dated March 28, 2025 is delayed in the source system and reaches the fact load on April 3. Which host_key belongs on its fact row?
- 101, because March 28 falls inside that version's window, whatever day the row is loaded.
- 104, because that row is flagged is_current on April 3, the day the booking is loaded.
- 104, because its valid_from of April 1 is the closest date before the April 3 load.
- Both 101 and 104, because the host had 2 versions on file when the booking arrived.
Type 2 answers what was true at the time for every fact, at the price of a longer load and a table where a host and a row are different things. Some requirements need less: a single earlier value, kept beside the current one.
About this lesson
This section is part of the Change Tracking lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.