People move house, products are renamed and teams are reorganised. A warehouse has to decide whether a report from last year should show the old truth or the new one.
01 · The problem
Facts record events. Dimensions describe the things the events happened to.
A sale is a fact. The customer, the product and the region it belongs to are dimensions, and those descriptions change slowly over time.
When a customer moves from one region to another, a question follows: should their past sales count towards the old region or the new one? Both answers are reasonable, and they give different totals.
02 · The common answers
Overwrite, add a row, or add a column.
These are the names used in dimensional modelling. Most real warehouses use Type 1 for corrections and Type 2 where history matters.
- Type 1: overwriteChange the value in place. Simple, and history is lost. Right for corrections, such as fixing a typo.
- Type 2: add a rowKeep the old row, close its dates, and add a new current row. Full history is kept, and every fact joins to the version that was true at the time.
- Type 3: add a columnKeep the previous value beside the current one. Cheap, but it remembers only one step back.
03 · How Type 2 works
A surrogate key, two dates and a flag.
Each version of a customer gets its own surrogate key, a meaningless number the warehouse issues. The row also carries a valid-from date, a valid-to date and often a current flag.
A new sale joins to the version that is current on the sale date. Last year's sales still point at last year's row, so last year's report does not change when this year's customer moves.
The business key says which customer it is. The surrogate key says which version of them.
04 · Choosing
Decide per attribute, with the person who reads the report.
Not every attribute deserves history. Tracking every change in every column makes the table large and the joins harder, for little benefit. Ask which changes would alter a decision if the old value were lost.
Then write the choice down next to the model. The most common surprise in a warehouse is two reports that disagree because one used the old truth and the other the new.
05 · Worked example
A customer who moves.
A customer lives in Chennai until 28 February and in Perth from 1 March. With a Type 2 design the table holds two rows for the same customer: one with the city Chennai and dates ending on 28 February, and one with Perth, starting 1 March and still current.
A sale made in January joins to the Chennai row, so January's report still says Chennai. A sale made in April joins to the Perth row. Nothing was rewritten, and both reports are right for their time.
06 · Pitfalls
Where history tracking goes wrong.
- Joining on the business keyFacts must join on the version key, or every sale shows the current description and history is lost again.
- Forgetting to close the old rowIf the old row keeps no end date, a customer appears to be in two places at once.
- Tracking noisy columnsA column that changes daily, like a last-login time, creates a new row each day. Keep such values out of the dimension.
- Gaps and overlapsEach version should start the moment the previous one ends. Test for gaps and overlaps on every load.
- Unwritten rulesSay which columns keep history and which are overwritten, in the model's documentation.
What I took away
- Decide whether the past should follow the old description or the new one.
- Overwrite for corrections; keep versions where history changes decisions.
- Separate the business key from the version key.