(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to Create a Slowly Changing Dimension Type 2 in Data Modeling (Kimball)

João Barros 06 de December de 2026 5 min read

This tutorial shows how to create a Slowly Changing Dimension Type 2 (SCD Type 2) in Data Modeling (Kimball) to preserve attribute history. It is useful when you need to analyze measures over time while respecting changes in dimensions such as customer, product or location.

Prerequisites

  • Basic knowledge of SQL and star schema design.
  • An environment with a relational database (e.g.: SQL Server, PostgreSQL) for testing.
  • Source data with a natural key (for example customer_id) and attributes that change.

Step 1: Understand the rationale and the SCD Type 2 pattern

An SCD Type 2 preserves each relevant change as a new row in the dimension, maintaining temporal validity (effective_from/effective_to) and a surrogate key. This way, the fact tables can point to the correct version of the dimension at the time of the transaction.

Step 2: Define the dimension table structure

Create a dimension table with a surrogate key, natural key, business attributes and validity columns plus a current row indicator. This facilitates historical queries and joins with the fact tables.

CREATE TABLE dim_customer_scd2 (
  customer_sk BIGINT IDENTITY(1,1) PRIMARY KEY,
  customer_id VARCHAR(50) NOT NULL,
  name VARCHAR(200),
  region VARCHAR(100),
  effective_from DATE NOT NULL,
  effective_to DATE NULL,
  is_current BIT NOT NULL DEFAULT 1
);

Step 3: Prepare source data and identify changes

Compare the current image from the source with the most recent version in dim_customer_scd2 using the natural key (customer_id). Identify three groups: new customers, unchanged customers and customers that changed.

-- Exemplo: obter versão actual da dimensão
WITH current_dim AS (
  SELECT customer_id, name, region
  FROM dim_customer_scd2
  WHERE is_current = 1
)
SELECT s.*
FROM src_customers s
LEFT JOIN current_dim d ON s.customer_id = d.customer_id;

Step 4: Insert new records (new customers)

For customers that do not exist in the dimension, insert a new row with effective_from = load date and is_current = 1.

INSERT INTO dim_customer_scd2 (customer_id, name, region, effective_from, effective_to, is_current)
SELECT s.customer_id, s.name, s.region, CAST(GETDATE() AS DATE), NULL, 1
FROM src_customers s
LEFT JOIN dim_customer_scd2 d ON s.customer_id = d.customer_id
WHERE d.customer_id IS NULL;

Step 5: Handle changes (close old version and insert new)

For customers that changed relevant attributes, update the existing row to set effective_to and is_current = 0, and insert a new row with the updated values and is_current = 1.

-- Marcar versões antigas como não correntes
UPDATE dim_customer_scd2
SET effective_to = CAST(GETDATE() AS DATE), is_current = 0
FROM dim_customer_scd2 d
JOIN src_customers s ON d.customer_id = s.customer_id
WHERE d.is_current = 1
  AND (d.name <> s.name OR d.region <> s.region);

-- Inserir novas versões
INSERT INTO dim_customer_scd2 (customer_id, name, region, effective_from, effective_to, is_current)
SELECT s.customer_id, s.name, s.region, CAST(GETDATE() AS DATE), NULL, 1
FROM src_customers s
JOIN dim_customer_scd2 d ON s.customer_id = d.customer_id
WHERE d.is_current = 0 -- após update, a versão antiga ficou 0
  AND (d.name <> s.name OR d.region <> s.region);

Step 6: Avoid duplicates and handle idempotent loads

Loads should be idempotent: use timestamps or batch_id and check if the same load has already been applied. A common practice is to compare the entire row before inserting or employ a staging table with a hash of the attributes to detect changes.

-- Criar hash para detectar mudanças
SELECT s.customer_id,
       HASHBYTES('SHA1', CONCAT(s.name,'|',s.region)) AS row_hash
FROM src_customers s;

Verify the result

Confirm that for each customer_id there is exactly one row with is_current = 1 and, if there were changes, prior versions with is_current = 0 and effective_to populated. Also verify that the fact tables point to the correct customer_sk according to transaction dates.

-- Verificações rápidas
-- 1. Uma linha corrente por customer_id
SELECT customer_id, COUNT(*) AS cnt_current
FROM dim_customer_scd2
WHERE is_current = 1
GROUP BY customer_id
HAVING COUNT(*) <> 1;

-- 2. Histórico ordenado
SELECT * FROM dim_customer_scd2
WHERE customer_id = 'C123'
ORDER BY effective_from;

Conclusion

Implementing a Slowly Changing Dimension Type 2 in Data Modeling (Kimball) enables correct historical analyses and integrates with the fact tables via the surrogate key. Next steps: automate the process (ETL/ELT), manage non-relevant attributes and integrate with the fact tables. Tip: start with a small dimension and test historical loads to validate the validity logic.