Skip to content
Silver 5 min read · Snapshots 3 practice problems ↓

Snapshots (SCD Type 2)

Many source tables overwrite their rows: when a customer changes plan, the old plan is gone. Snapshots keep the history, so reports can see what was true at the time. This lesson shows how they work and the two ways to detect changes.

Pipelines in production · dbt · Silver · Change over time, lesson 2 of 2

Read first: Sources and staging models (skip if you know it)

In plain words first

A height chart on a door frame keeps every mark with its date. Overwriting the mark each year would tell you only today's height.

A snapshot is that door frame for a table: each time a row changes, the old version is kept with the dates it was valid.

The problem

Many sourcesource: A raw table loaded by another tool, declared in YAML so models can select from it with source() and dbt can test it and check its freshness. Learn more → tables are overwritten in place: when an account changes plan, the accounts row is updated and the old plan is gone. Revenue by plan last quarter then uses today's plans, which is wrong. A snapshot records every version of each row over time, a slowly changing dimension of Type 2.

Defining a snapshot

YAML · snapshots/accounts_snapshot.yml · YAML snapshots, dbt 1.9 and later
snapshots:
  - name: accounts_snapshot
    relation: source('app', 'accounts')
    config:
      schema: snapshots
      unique_key: account_id
      strategy: timestamp
      updated_at: updated_at
      hard_deletes: invalidate   # close the row when it disappears from the source
dbt · snapshots/accounts_snapshot.sql · The older SQL form, still common in existing projects
{% snapshot accounts_snapshot %}
{{ config(
    target_schema='snapshots',
    unique_key='account_id',
    strategy='timestamp',
    updated_at='updated_at'
) }}
select * from {{ source('app', 'accounts') }}
{% endsnapshot %}

Run it with dbt snapshot (or dbt build, which includes snapshots).

What the snapshot table looks like

Source on three days

account_idplanupdated_at
42freeJan 1
42premiumFeb 10
42familyMar 5

accounts_snapshot

account_idplandbt_valid_fromdbt_valid_to
42freeJan 1Feb 10
42premiumFeb 10Mar 5
42familyMar 5null
  • dbt_valid_from / dbt_valid_to: when this version was current. The current version has dbt_valid_to null (or a far-future date with dbt_valid_to_current).
  • dbt_scd_id: a unique id per version; dbt_updated_at: the timestamp dbt used.
  • Each run, dbt compares the source with the current snapshot rows: changed rows are closed (valid_to set) and a new version is inserted; new keys are inserted.

timestamp or check

StrategyDetects a change whenUse when
timestampThe row's updated_at is newer than the stored oneThe source has a reliable updated-at column. Preferred: cheap and precise.
checkAny of check_cols (or all columns) differNo trustworthy timestamp. Slower, and misses changes that revert between runs.
Watch out: A snapshot only sees the source as it is when the snapshot runs. If an account changes plan twice between two runs, the middle version is never recorded. Run snapshots at least as often as the changes you care about, or snapshot a change log instead.

Deleted rows

By default a row that disappears from the source stays current in the snapshot forever. hard_deletes: invalidate closes it (sets valid_to); new_record also inserts a row marking the deletion with dbt_is_deleted. (Before 1.9 this was invalidate_hard_deletes: true.)

Using a snapshot

Snapshots are raw history: build a modelmodel: A SELECT statement in a .sql file. dbt runs it and saves the result in the warehouse as a table or view named after the file. Learn more → on top of them, like any source. To find the plan an account had when each song was played, join on the key and on the time range:

dbt · models/marts/fct_plays_with_plan.sql
select
    p.play_id,
    p.played_at,
    s.plan as plan_at_play_time
from {{ ref('stg_plays') }} as p
left join {{ ref('accounts_snapshot') }} as s
    on  s.account_id = p.account_id
    and p.played_at >= s.dbt_valid_from
    and (p.played_at < s.dbt_valid_to or s.dbt_valid_to is null)

The half-open range (>= from, < to) matters: a closed range on both ends would match two versions at the exact moment of a change.

Note: Snapshots are not reproducible: if the snapshot table is dropped, history cannot be rebuilt from the source. Keep them in their own schema, never full-refresh them, and back them up like source data.

Common mistakes

  • Snapshotting a transformed model with complex logic — Changing the logic later changes what counts as a change. Snapshot sources, or a light staging modelstaging model: The first layer of models: one per source table, renaming and casting columns, with no joins or business logic. Learn more →.
  • Running snapshots once a week — Changes that happen and revert between runs are lost.
  • Closed date ranges in point-in-time joins — A row at the change moment matches two versions.

Key takeaways

  • Snapshots record each version of a mutable row with dbt_valid_from and dbt_valid_to.
  • timestamp strategy is preferred; check is the fallback without a reliable timestamp.
  • Changes between two runs are invisible: schedule accordingly.
  • Join on key and half-open validity range for point-in-time lookups.

Interview prep

THE QUESTION

"How do you keep history for a source table that is updated in place?"

A dbt snapshot (SCD Type 2). Configure unique_key and a strategy: timestamp if the source has a reliable updated_at, check with check_cols if not. Each dbt snapshot run compares the source to the current versions, closes changed rows by setting dbt_valid_to and inserts the new version with dbt_valid_from. Deleted rows are handled with hard_deletes. Snapshots only catch changes visible at run time, so schedule them often enough, and protect them because history cannot be rebuilt.

Avoid: "a snapshot is a copy of a table". It is a history: one row per version with validity dates.

Follow-ups · answer out loud, then open

Follow-up"When would you use the check strategy over timestamp?"
When the source has no reliable updated_at. check compares the chosen columns (or all) between runs; it costs more and still misses changes that revert between runs.
Scenario"Finance needs the plan each account had when each invoice was issued. How?"
Snapshot the accounts source, then join invoices on account_id and issued_at >= dbt_valid_from and (issued_at < dbt_valid_to or dbt_valid_to is null).
Trap"If the snapshot table gets corrupted, we can rebuild it with a full refresh."
No: the history only exists in the snapshot. The source has only the current state. Protect and back up snapshot tables.

Check yourself

3 questions

How do you find the current version of each row in a snapshot?

Show the answer

dbt_valid_to is null. The current version has no end date (unless dbt_valid_to_current sets one).

A source has no updated_at column. Which strategy works?

Show the answer

check. check compares column values between runs.

A row changes from A to B and back to A between two snapshot runs. What does the snapshot record?

Show the answer

Nothing: it still sees A. The snapshot only sees the state at run time.

Practise this

Problems that use this, graded on hidden tests

See all 4 problems →

Keep going

Up next · new module: Testing logic · 5 min read
Unit tests
Test a model's logic on fixed input rows before it builds, and how unit tests differ from data tests.

Related lessons

Previous: Incremental models

Primary sources: Snapshots · Snapshot configs