Saltar al contenido

Lección 13 · History and Slowly Changing Dimensions

Slowly Changing Dimension Types

Compare slowly changing dimension types 0 to 4 and 6, see how overwrite and add-row choices change report results, and learn how to choose per attribute.

Dimension values change. A customer moves, a product is moved to another category, a sales team is reorganized. Your warehouse has to decide what happens to the old value. The standard techniques are called slowly changing dimension (SCD) types.

The word "slowly" means attributes that change now and then, not on every transaction.

The question that decides everything

When a customer moves from Quito to Bogota, which city should last year's purchases show?

  • The city at the time of purchase ("as it was").
  • The city now ("as it is").

Both are legitimate questions. Your design determines which ones you can answer.

The types

Type What happens on change History kept
0 Nothing; the original value is retained Original only
1 Overwrite the value None
2 Add a new row with a new surrogate key Full history
3 Add a column for the previous value Limited
4 Move history or fast-changing data to a separate table In that table
6 Combine types 1, 2 and 3 Full history plus current value

Descriptions of types 4 and above differ between authors. Kimball uses type 4 for a mini-dimension; many other texts use it for a separate history table. When you discuss them, describe the behavior, not only the number.

Type 0: keep the original

Use it for values that must reflect the start, such as a customer's original signup channel.

Type 1: overwrite

Use it to correct mistakes, such as a misspelled name, or when nobody needs the old value. Past reports will change after the overwrite. If you have stored summaries, they must be rebuilt too.

Type 2: add a row

The old row is closed and a new one is added. Facts keep the surrogate key that was valid when they happened, so history stays true. This is the main technique when history matters, and the next lesson builds it.

Type 3: keep a previous-value column

Useful when you need to compare "old and new" after a planned change, such as a sales territory realignment. It holds only as many earlier values as you have columns.

See types 1 and 2 give different answers

SQL Same sales, two histories

Press Run (or Ctrl + Enter) to see the result here.

Type 1 puts all 160 in Bogota. Type 2 puts 100 in Quito and 60 in Bogota. Neither is a bug: they answer different questions.

Type 3 and type 6

SQL Type 6: history plus current value

Press Run (or Ctrl + Enter) to see the result here.

One design now serves both questions. The cost is that every change must update current_city on all versions of that customer.

Choose per attribute

SCD type is chosen per attribute, not per table. A customer dimension might treat name corrections as type 1, city as type 2, and original signup channel as type 0. Record the choice:

Attribute Type Reason
customer_name 1 Corrections should apply everywhere
city 2 Regional history matters
signup_channel 0 Describes how the relationship began

Privacy and history

History can store personal data for a long time. Laws in many places give people rights to correction or deletion, and require limits on retention. Keep only history you have a reason to keep, restrict access, and plan how to erase or anonymize old versions. Ask your privacy or legal specialists about your obligations.

Questions to ask for each attribute

Business users rarely say "I want Type 2". Ask in their own words:

  • "If this value changes, should last year's report change as well?"
  • "Have you ever needed to know what the value was at a particular time?"
  • "Is a change usually a real event, or the correction of a mistake?"
  • "How often does it change?"

A tricky case is that one column can see both kinds of change. A city may be corrected because of a typing error, or updated because the customer moved. Applying Type 2 to the correction would invent a move that never happened. If the source records the reason, use it. If it does not, you need a manual correction process.

What version rows do to counts

With Type 2, one customer can have several rows. COUNT(*) on the dimension counts versions, not customers. To count customers, count distinct natural keys, or count only current rows:

SQL
-- Read-only: two different questions
SELECT COUNT(DISTINCT customer_id) AS customers_ever FROM dim_customer;
SELECT COUNT(*) AS current_customers FROM dim_customer WHERE is_current = 1;

The same applies to facts. Group by the natural key when you want one line per customer across all versions. Grouping by surrogate key would split a person who moved into two lines.

"As it is" without type 6

You can report current values from a pure Type 2 dimension: join the fact to its historical version, then join again on the natural key to the row where is_current = 1. This needs no extra updates during loading. A view can hide the second join and expose clear columns such as city_at_sale and city_now. Clear names matter, because a column simply called city leaves readers guessing which one they are seeing.

Changing your mind later

Moving an attribute from Type 1 to Type 2 starts history from the day of the change. Values that were already overwritten cannot be recovered from the dimension, although retained raw snapshots may help you rebuild them.

Common mistakes

Using type 2 for every column creates many versions that nobody needs, especially for volatile values such as last login time. Using type 1 everywhere quietly rewrites history. Ask users how they expect past reports to behave.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A misspelled customer name is fixed, and all reports should show the correct spelling. Which type fits?

    An overwrite applies the correction everywhere.
  2. 2. What does a Type 2 change add?

    Each version is its own row, and facts point to the version valid at the time.
  3. 3. Which type keeps only a limited amount of history in extra columns?

    Type 3 stores prior values in a fixed number of columns.
  4. 4. At what level is the SCD type usually decided?

    Different attributes in one dimension can need different behavior.

Summary

  • SCD types describe how a dimension handles change.
  • Type 1 overwrites; Type 2 adds versions; Type 3 keeps limited earlier values.
  • Type 6 combines techniques so you can report both "as it was" and "as it is".
  • Decide per attribute, and weigh privacy and retention.

Next: SCD Type 2 in SQL

Tu progreso se guarda en este navegador. No necesitas una cuenta.