18 / 35 · Analytical Warehouses
History in a warehouse
Keep a versioned dimension, match late facts by event time and distinguish a historical change from a correction.
Tea moves from Drinks to Gifts on 10 January. A sale made on 9 January reaches the warehouse on 12 January. A report of categories at the time of sale must assign that line to Drinks, even though the current catalogue says Gifts.
Keep the version the event needs#
A slowly changing dimension describes a business object whose attributes can change. A type 1 update replaces the attributes on an existing row. It makes old facts display the current category. This can be useful for correcting a misspelling when no report needs the old spelling. A type 2 change inserts a new row with a new surrogate key. The durable product key ties its versions together, while the surrogate key identifies the version linked to a fact. (Kimball, 2008; Oracle, §2.4.2)
Use version 7 for Tea in Drinks and version 8 for Tea in Gifts. The first is valid from 1 January up to 10 January, and the second starts on 10 January. Treat intervals as inclusive at the start and exclusive at the end. A sale exactly on 10 January then matches the new version once. With two inclusive intervals it could match both.
The sale uses the earlier version
Look up by the sale date#
The warehouse receives sale 101 on 12 January. Its event date is 9 January. The type 2 lookup uses the event date and the durable product key. After finding the version, the fact stores that version’s key. Here dates are sufficient because the change occurs at a day boundary in the reporting calendar. An intraday rule needs instants and a time-zone convention. (Microsoft, Slowly changing dimensions)
WITH product_versions(version_id, product, category, valid_from, valid_to) AS (
VALUES
(7, 'tea', 'Drinks', DATE '2026-01-01', DATE '2026-01-10'),
(8, 'tea', 'Gifts', DATE '2026-01-10', NULL::date)
), sales(order_id, product, sold_on, arrived_on, cents) AS (
VALUES
(101, 'tea', DATE '2026-01-09', DATE '2026-01-12', 700),
(102, 'tea', DATE '2026-01-10', DATE '2026-01-10', 350)
)
SELECT s.order_id, d.version_id, d.category, s.cents
FROM sales AS s
JOIN product_versions AS d
ON d.product = s.product
AND d.valid_from <= s.sold_on
AND (s.sold_on < d.valid_to OR d.valid_to IS NULL)
ORDER BY s.order_id;
The result is 101 with version 7 and 700 cents under Drinks, then 102 with version 8 and 350 under Gifts. Using arrived_on instead would put both under Gifts. Overlapping intervals can duplicate a sale. A gap can make an inner join drop it. Before loading the foreign keys, check that each sale has exactly one match, for example with a left join and a count of matching version IDs. A missing version may require deferring the fact or representing it explicitly as unresolved.
Preserve effective time and recorded time#
A category change has a valid time when it takes effect in the shop. The warehouse also has a time when it learned that information. A correction received on 13 January might say that the change was effective on 8 January. A report that shows corrected history can revise the intervals and reassign affected facts. A report that must reproduce yesterday’s published answer also needs the earlier recorded version or a retained published snapshot. One effective-time interval per version alone cannot answer both questions.
Next#
Time-aware lookup determines which version belongs to each sale. Full and incremental loads take up the source boundary and decide which new, changed or deleted rows to carry into the warehouse.
References#
- Ralph Kimball. Slowly Changing Dimensions, Part 2. Kimball Group, 2008, on effective dates and new surrogate keys for type 2.
- Oracle. Data Warehousing Logical Design. Oracle Database 19c Data Warehousing Guide, §2.4.2 on slowly changing dimension types.
- Microsoft. Understand Star Schema and the Importance for Power BI. Product guidance on preserving versions and time-based dimension lookups.