Slowly changing dimensions: type 1 and type 2

What a slowly changing dimension is, when to overwrite a value (type 1) and when to keep its history in a new row (type 2), with the SQL for both.

Short answer: a slowly changing dimension is a dimension whose attributes change now and then: a customer moves, a product changes category. Type 1 overwrites the old value, so every report shows the new one. Type 2 closes the old row and adds a new one with dates, so last year's sales stay with last year's city. Choose per column, not per table.

The question is always the same: when a customer moves from Graz to Vienna, should last year's sales move with them? For a corrected typo, yes. For a sales region, usually not.

What does type 2 keep?

CustomerKeyCustomerIDCityValidFromValidToIsCurrent
881042Graz2023-05-142026-02-280
7311042Vienna2026-03-019999-12-311
Customer 1042 moves from Graz to Vienna on 1 March 2026

Two keys do two jobs. CustomerID is the business key from the source and stays the same. CustomerKey is the surrogate key: a new one per version, and the fact table stores it, so each sale points at the version that was true when it happened.

Type 0never changes: a date of birth
Type 1overwrite: corrections, and values nobody reports history on
Type 2new row with dates: values reports should see as they were
Type 3a column for the previous value: rare
The types you meet

How do you load type 2?

Two steps: close the rows whose tracked columns changed, then insert a new version for every changed or new customer.

UPDATE d
SET ValidTo = DATEADD(day, -1, CAST(GETDATE() AS date)), IsCurrent = 0
FROM dim.Customer AS d
JOIN stage.Customer AS s ON s.CustomerID = d.CustomerID
WHERE d.IsCurrent = 1 AND s.City <> d.City;

INSERT INTO dim.Customer (CustomerID, Name, City, ValidFrom, ValidTo, IsCurrent)
SELECT s.CustomerID, s.Name, s.City, CAST(GETDATE() AS date), '9999-12-31', 1
FROM stage.Customer AS s
LEFT JOIN dim.Customer AS d ON d.CustomerID = s.CustomerID AND d.IsCurrent = 1
WHERE d.CustomerID IS NULL;

After the update, a changed customer has no current row, so the insert adds its new version along with brand-new customers. Type 1 columns, like a corrected name, are a plain UPDATE on all rows of the customer.

What goes wrong?

  • Facts joined on the business key. Join facts to the dimension by CustomerKey when they load, or every sale shows the current city and type 2 did nothing.
  • Every column tracked. A type 2 change on a phone number makes a new row for nothing. Track only the columns reports group by.
  • Nulls in the comparison. s.City <> d.City is not true when one side is null. Compare with ISNULL or IS DISTINCT FROM.

Where can you check this?

Where do you start?

The open course Upserts into Azure SQL builds the staging table and the merge by key that type 1 needs; type 2 adds the two steps above. In Fabric, the gold layer of bronze, silver and gold is where these dimensions live.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.