Type 1 turned Q2 East revenue into $2,500. Type 2 kept the real $6,700.
python3 --version.pip install "duckdb>=1.0"git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
pip install -r requirements.txt # or just this lesson: pip install "duckdb>=1.0"
cd data-engineering/18-scd-type-2 python3 src/scd.py
from shop import setup, by_region # same data
db = setup() # t1: customers; t2: + valid dates
db.sql("UPDATE t1 SET region='West' WHERE id=1")
db.sql("UPDATE t2 SET valid_to='2026-07-01', "
"is_current=false WHERE id=1")
db.sql("INSERT INTO t2 VALUES (1, 'Ana', 'West', "
"'2026-07-01', '9999-12-31', true)")
print("Q2 type 1:", by_region(db, "t1"))
print("Q2 type 2:", by_region(db, "t2"))Your customer moved, and your report rewrote history. Ana lived East. On July 1, she moved West. Same customers, same sales, two dimension tables.
Type 1 overwrites Ana's row. Type 2 closes the old row with a valid-to date, and inserts a new one: West, from July 1. Run it. Type 1 says East made $2,500 in Q2.
Ana's spring sales moved West with her. Type 2 says $6,700. History kept. Overwriting rewrites the past.
Add a row instead.
Read the lesson on GitHub →