Keys and slowly changing dimensions
CustomerChanges EarlyOrders NWCustomers- Why it matters
- Customers change segment, tier and region. Without history, last year's sales are silently re-attributed to this year's segment, and reports can't be reproduced.
- Typical production failure
- A customer moves from Bronze to Gold and every historical sale jumps into the Gold row: Gold looks like it grew 40% last year when nothing happened.
- When to use it
- SCD type 2 (new row per change, valid-from/valid-to) for attributes people analyse over time; type 1 (overwrite) for corrections; surrogate keys so versions can coexist.
- When not to
- Type 2 on everything. Tracking history on phone numbers or typos bloats the dimension and confuses users.
Assignments
Build an SCD type 2 customer dimension
The CRM sends a change feed: one row per customer per effective change. Build dim_customer with full history so sales can be analysed by the segment and tier the customer had at the time.
- A surrogate key, the natural key (CustomerID), the tracked attributes, ValidFrom, ValidTo and IsCurrent.
- Remove exact duplicate feed rows.
- Don't create a new version when the CRM sends a "change" in which nothing tracked actually changed.
- Report the counts.
Which version was true on that day?
Attach each order to the customer version that was valid on the order date, so sales by tier reflect the tier at the time.
- Point-in-time join: OrderDate >= ValidFrom AND OrderDate < ValidTo.
- Show the versions of customer C10005.
- Decide, per attribute, whether it should be type 1 or type 2.
Orders for customers who don't exist yet
Four orders arrived for customers the CRM hasn't sent yet. Last night's load dropped them because the join to dim_customer failed. Fix the design so no fact row is ever lost, and say what happens when the customer arrives later.
- Never drop a fact row because a dimension row is missing.
- Distinguish "unknown" from "not yet arrived".
- Show which of the four orders can be resolved once the CRM catches up.
Interview questions
- Surrogate key vs natural key?
- SCD type 1 vs type 2?
- What is an unknown member and why add one?
- Early-arriving fact vs late-arriving dimension?
Assessment
A customer changes from Bronze to Gold on 1 March. With SCD type 2, a February order is reported under:
- Gold
- Bronze
- Both
- Unknown
Why use a surrogate key instead of CustomerID in a type 2 dimension?
- It's shorter
- CustomerID isn't unique once a customer has several versions
- Power BI requires integers
- It hides personal data
In Power BI, relate FactSales to the type 2 dim_customer and build a matrix of sales by LoyaltyTier for 2025 and 2026. Then build the same with only the current version. Explain the difference to a sales manager.
Join facts on the surrogate key for history; use a separate "current customer" view (or the IsCurrent filter on a type 1 copy) for "as is today" analysis.