DataEng Hub
dbtintermediateArticle · 2 min read

Slowly changing dimensions with dbt snapshots

dbt snapshots give you SCD Type 2 history in about ten lines of config — if you pick the right strategy and understand what they can't do.

Balaji S.
Lead Data Engineer

TL;DR

  • A dbt snapshot tracks changes to a source table and writes SCD Type 2 rows with dbt_valid_from / dbt_valid_to.
  • Use the timestamp strategy when the source has a reliable updated_at; fall back to check when it doesn't.
  • Snapshots only see changes that exist when they run — two changes between runs collapse into one. Know this before you promise auditors "full history".

The problem

NorthBridge Retail's customers table holds only the current address. Finance wants revenue by the customer's region at the time of the order, which needs history the source never kept.

The snapshot

-- snapshots/customers_snapshot.sql
{% snapshot customers_snapshot %}
{{
  config(
    target_schema='snapshots',
    unique_key='customer_id',
    strategy='timestamp',
    updated_at='updated_at'
  )
}}
SELECT customer_id, name, address, region, updated_at
FROM {{ source('crm', 'customers') }}
{% endsnapshot %}

On each dbt snapshot run, changed rows get their open record closed (dbt_valid_to set) and a new open record inserted.

Timestamp vs check strategy

Strategy When to use Cost
timestamp Source has a trustworthy updated_at Cheapest — one column compared
check No reliable timestamp; compare chosen columns Compares every listed column each run

With check, list the columns explicitly. check_cols='all' is convenient and a performance trap on wide tables.

Joining history correctly

Point-in-time joins use a range condition, not equality:

SELECT o.order_id, o.amount, c.region
FROM orders o
JOIN snapshots.customers_snapshot c
  ON  o.customer_id = c.customer_id
  AND o.ordered_at >= c.dbt_valid_from
  AND o.ordered_at <  COALESCE(c.dbt_valid_to, '9999-12-31');

From production: gotchas and lessons

  1. Symptom: history "missing" for some customers. Cause: snapshot ran daily; two same-day changes collapsed into one row. Fix: run snapshots on the ingestion schedule, or accept and document the granularity.
  2. Symptom: every row rewritten on one run. Cause: an upstream type change made check columns differ for all rows. Fix: pin column types in the snapshot query itself with explicit casts.
  3. Symptom: deleted customers stay "current" forever. Cause: snapshots don't see deletes by default. Fix: enable invalidate_hard_deletes: true.

When not to use this

If the source already emits CDC events with before/after images, build SCD2 from the CDC feed with MERGE instead — the snapshot would throw away intra-day history your source actually has.

Practice

Further reading

#scd2#snapshots#dimensions

Keep going

ProblemSilver

Build an SCD Type 2 with MERGE

Apply a day's customer address changes to a dimension table, closing old versions and inserting new ones, in a single MERGE.

PDF guide9 pages · v1.0

dbt Project Structure Guide

How to lay out a dbt project that scales past 200 models: staging/intermediate/marts layers, naming, tests, and the refactors to do early.

One practical note a week

Get new articles, problems and guides in your inbox.