Quick revision
The short answer
We model the product as a slowly changing dimension of type 2: every time a tracked attribute such as category or price tier changes, we close the current row by setting its valid_to and insert a new row with a new surrogate key, valid_from and is_current. Each sale stores the surrogate key that was current at the time of the sale, so reports grouped by category automatically show the category the product had then. Attributes that do not need history can be overwritten as type 1 instead.
What to remember
In this answer, we designed a type 2 slowly changing product dimension. Each change to a tracked attribute closes the current row and adds a new version with its own surrogate key and validity period, and each fact stores the key that was valid at the event time, so reports keep their history. We chose type 1 or type 3 for attributes that need no or limited history, built a load that detects changes with a hash of tracked attributes, and documented how to ask as of now questions through the natural key and is_current.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We model the product as a slowly changing dimension of type 2: every time a tracked attribute such as category or price tier changes, we close the current row by setting its valid_to and insert a new row with a new surrogate key, valid_from and is_current. Each sale stores the surrogate key that was current at the time of the sale, so reports grouped by category automatically show the category the product had then. Attributes that do not need history can be overwritten as type 1 instead.
Why this question is asked
Products, customers and stores change over time. A mug moves from Kitchen to Gifts, a laptop drops from the premium to the standard price tier. If the warehouse simply overwrites the product row, last year's reports change overnight, and nobody can explain why Gifts suddenly sold well in 2024. Interviewers ask this to see whether we know how to keep history in a dimension and what that costs the loading process and the analysts. In this answer, we will recap the problem, design a type 2 dimension, see how facts pick the right key, compare type 1 and type 3, build the loading logic with change detection, and finish with as of now queries.
1The Problem With Overwriting
In a star schema, sales facts point at a product dimension row through a surrogate key, an integer generated by the warehouse. If dim_product has one row per product and we update its category when the shop recategorises it, every past sale of that product now appears under the new category. The history in the facts is still there, but its context has been rewritten.
That is sometimes what the business wants, for example when a category was simply misspelled. But for this question, reports must show the category at the time of sale, so the dimension itself has to remember what each product looked like in each period.
2Designing a Type 2 Dimension
A type 2 dimension keeps one row per version of a product. The natural key, the product id from the shop's system, stays the same across versions; the surrogate key is new for every version. Three extra columns describe the version's life: valid_from, valid_to and is_current.
Notice that valid_to of one version equals valid_from of the next, so periods never overlap or leave gaps, and that the open version uses a far-future date rather than NULL, which keeps range comparisons simple. We can add a unique constraint so that each product_id has exactly one current row.
3How Facts Keep History
The history only works if each fact row points to the version that was current when the event happened. The fact loader looks up the product's natural key and the sale time in the dimension and stores that version's surrogate key. After that, the fact never changes: a mug sold in February 2025 points at key 101 forever, with category Kitchen, even though the product is now in Gifts.
So the design moves the hard work to load time. Analysts writing the usual star query get historically correct answers without thinking about dates at all. For late-arriving facts, such as a sale reported a week after it happened, the loader must look up the version by the event time, not by is_current, which is why the lookup above uses the date range.
4Choosing a Type per Attribute
Not every attribute deserves history. Each change in a type 2 attribute creates a new row, so tracking a frequently edited text description would multiply the dimension without any reporting value. We decide attribute by attribute.
| Type | What a change does | History | Use for |
|---|---|---|---|
| Type 1 | overwrite the value in place | none | corrections, descriptions |
| Type 2 | close the row, insert a new version | full, with periods | category, price tier, region |
| Type 3 | keep a previous_value column | one step back | comparing old and new during a reorganisation |
For our product, category and price tier are type 2, because the question requires sales by the value at the time. Description and image URL are type 1, overwritten in every version, because reports never group by them and a fixed typo should be fixed everywhere. Type 3 is useful during a planned reorganisation, when the business wants to compare this year's sales under both the old and the new category for a while.
5Loading: Change Detection and Key Lookup
Every night, or every few minutes with CDC, the loader receives the current state of each product from the source. For each one it must decide: is it new, unchanged, or changed in a tracked attribute? Comparing every tracked column one by one is error prone, so we compute a hash of the tracked attributes and store it in attr_hash. If the incoming hash differs from the current row's hash, a tracked attribute changed.
- Compute hash of tracked attributescategory, price tier
- Current row exists?YesCompare hashesNoInsert first version
- Hashes differ?YesClose current row, insert new versionNoOnly update type 1 columns
3 steps in order. Play them, or step through them one at a time.
Notice the order. Step 1 closes changed rows, so in step 2 those products no longer have a current row and get a new version, together with brand new products. Running the same batch twice changes nothing the second time, because the hashes now match, which makes the load safe to retry. In dbt, the snapshot feature implements this same pattern for us.
6As of Now Queries Need Documentation
Sometimes the business asks the opposite question: show all past sales under today's categories, for example to plan next year's ranges. Then we join the fact to its version only to find the natural key, and join again to the current version of that product.
Both questions are valid, and they give different numbers. Analysts must know which one their dashboard answers, so the model's documentation states it plainly: joining on product_key gives the value at the time of sale, joining on product_id with is_current gives today's value. Many teams also publish a ready-made view, such as dim_product_current, so analysts do not have to write the double join themselves.
7Follow-up questions
Why use a far-future valid_to instead of NULL for the current row?
Because range conditions such as event_time >= valid_from AND event_time < valid_to then work for every row without a special case for NULL, and indexes on the range stay simple. is_current is kept as well because filtering on a boolean is clearer and faster for the common current-row lookups.
What happens if a product changes twice in one day?
It depends on the grain of change we capture. A nightly snapshot only sees the end-of-day state, so the intermediate version is lost. If intraday changes matter, we load from CDC events with their real timestamps, and each change produces its own version.
Could we skip type 2 and store category directly in the fact table?
For one or two attributes that is a valid shortcut, because the fact then records the value at the time of sale. It becomes awkward as attributes grow, it bloats the fact table, and it cannot answer as of now questions without a separate lookup, so a type 2 dimension is the general solution.
8Conclusion
In this answer, we designed a type 2 slowly changing product dimension. Each change to a tracked attribute closes the current row and adds a new version with its own surrogate key and validity period, and each fact stores the key that was valid at the event time, so reports keep their history. We chose type 1 or type 3 for attributes that need no or limited history, built a load that detects changes with a hash of tracked attributes, and documented how to ask as of now questions through the natural key and is_current.
Open with
I'd use a type 2 slowly changing dimension for product. Each row is one version of a product: a new surrogate key, the natural product id from the source, the attributes, and valid_from, valid_to and is_current.
Then, if they want more
When category or price tier changes, the load closes the current row by setting valid_to and is_current false, and inserts a new version with a new key.
The fact loader looks up the version that was valid at the sale time and stores that surrogate key, so a mug sold while it was in Kitchen stays in Kitchen in every report, even after it moves to Gifts. That means analysts just join on product_key and get history for free.
I'd choose the type per attribute: category and price tier are type 2, things like description are type 1 and simply overwritten, and type 3 with a previous value column is handy during a planned reorganisation.
For loading, I detect changes with a hash of the tracked attributes, so the comparison is one column and reruns are idempotent; dbt snapshots do the same thing. Finally, I'd document how to get today's view, joining through product id to the is_current row, and probably publish a current view, because the two questions give different numbers.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Product attributes such as category and price tier change over time, and reports must...": https://scrivio-interview-library.netlify.app/databases/analytics-and-cdc/99c3962075/