23. Track Plan Changes with a Snapshot
Difficulty: Medium · Topic: Snapshots
{{ source('app', 'accounts') }} holds only the current state of each account (account_id, plan, seats, updated_at): when an account upgrades, its row is overwritten and updated_at moves forward. Finance needs the history: which plan each account was on, and when.
Write the snapshot accounts_snapshot (all four columns) so that dbt snapshot keeps one row per version of each account, dated by the account's own updated_at: dbt_valid_from when the version started, dbt_valid_to when it was replaced (null for the current one).
Graded over several snapshot runs (a month apart) against the same table. dbt also adds dbt_scd_id (a hash), which is left out of the comparison.
Graded over several runs against the same table, like a real schedule: 3 tests, including hidden edge cases. The table after every run must match.
Run 1 (at 2024-01-02 06:00:00)
{{ source('app', 'accounts') }}
| account_id | plan | seats | updated_at |
|---|---|---|---|
| 1 | free | 1 | 2024-01-01 09:00:00 |
| 2 | pro | 5 | 2024-01-01 10:00:00 |
Expected table after run 1
| account_id | plan | seats | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | free | 1 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | null |
| 2 | pro | 5 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | null |
Run 2 (at 2024-02-02 06:00:00)
{{ source('app', 'accounts') }}
| account_id | plan | seats | updated_at |
|---|---|---|---|
| 1 | pro | 3 | 2024-02-01 08:00:00 |
| 2 | pro | 5 | 2024-01-01 10:00:00 |
| 3 | free | 1 | 2024-02-01 12:00:00 |
Expected table after run 2
| account_id | plan | seats | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | free | 1 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | 2024-02-01 08:00:00 |
| 2 | pro | 5 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | null |
| 1 | pro | 3 | 2024-02-01 08:00:00 | 2024-02-01 08:00:00 | 2024-02-01 08:00:00 | null |
| 3 | free | 1 | 2024-02-01 12:00:00 | 2024-02-01 12:00:00 | 2024-02-01 12:00:00 | null |
Run 3 (at 2024-03-02 06:00:00)
{{ source('app', 'accounts') }}
| account_id | plan | seats | updated_at |
|---|---|---|---|
| 1 | pro | 10 | 2024-03-01 08:00:00 |
| 2 | team | 8 | 2024-03-01 09:00:00 |
| 3 | free | 1 | 2024-02-01 12:00:00 |
Expected table after run 3
| account_id | plan | seats | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | free | 1 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | 2024-01-01 09:00:00 | 2024-02-01 08:00:00 |
| 2 | pro | 5 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 2024-01-01 10:00:00 | 2024-03-01 09:00:00 |
| 1 | pro | 3 | 2024-02-01 08:00:00 | 2024-02-01 08:00:00 | 2024-02-01 08:00:00 | 2024-03-01 08:00:00 |
| 3 | free | 1 | 2024-02-01 12:00:00 | 2024-02-01 12:00:00 | 2024-02-01 12:00:00 | null |
| 1 | pro | 10 | 2024-03-01 08:00:00 | 2024-03-01 08:00:00 | 2024-03-01 08:00:00 | null |
| 2 | team | 8 | 2024-03-01 09:00:00 | 2024-03-01 09:00:00 | 2024-03-01 09:00:00 | null |
Hints
Hint 1
A snapshot file wraps one SELECT in{% snapshot accounts_snapshot %} ... {% endsnapshot %}, with a config() call inside.Hint 2
The source has a column that changes whenever the row changes: that is the timestamp strategy,strategy='timestamp', updated_at='updated_at'.Hint 3
dbt needs to know what one row is:unique_key='account_id'.Learn the concepts
- Snapshots (SCD Type 2) · 5 min read. Record how mutable source rows change over time with the timestamp and check strategies.