24. Snapshot a Table Without a Reliable updated_at
Difficulty: Medium · Topic: Snapshots
The CRM export {{ source('crm', 'customers') }} (customer_id, segment, country, last_seen_at) has no updated_at column. last_seen_at changes every time the customer opens the app, which is almost every day, and says nothing about whether the customer's details changed.
Write the snapshot customers_snapshot (all four columns) that records a new version only when segment or country changes. Without a trustworthy timestamp, versions are dated by when the snapshot ran.
Graded over several snapshot runs against the same table (the run times are shown with each run). dbt_scd_id 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-03-01 02:00:00)
{{ source('crm', 'customers') }}
| customer_id | segment | country | last_seen_at |
|---|---|---|---|
| 1 | smb | IN | 2024-02-29 18:00:00 |
| 2 | enterprise | SG | 2024-02-29 20:00:00 |
Expected table after run 1
| customer_id | segment | country | last_seen_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | smb | IN | 2024-02-29 18:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | null |
| 2 | enterprise | SG | 2024-02-29 20:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | null |
Run 2 (at 2024-03-02 02:00:00)
{{ source('crm', 'customers') }}
| customer_id | segment | country | last_seen_at |
|---|---|---|---|
| 1 | smb | IN | 2024-03-01 19:00:00 |
| 2 | enterprise | SG | 2024-03-01 21:00:00 |
Expected table after run 2
| customer_id | segment | country | last_seen_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | smb | IN | 2024-02-29 18:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | null |
| 2 | enterprise | SG | 2024-02-29 20:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | null |
Run 3 (at 2024-03-03 02:00:00)
{{ source('crm', 'customers') }}
| customer_id | segment | country | last_seen_at |
|---|---|---|---|
| 1 | mid-market | IN | 2024-03-02 09:00:00 |
| 2 | enterprise | SG | 2024-03-02 22:00:00 |
Expected table after run 3
| customer_id | segment | country | last_seen_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 1 | smb | IN | 2024-02-29 18:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | 2024-03-03 02:00:00 |
| 2 | enterprise | SG | 2024-02-29 20:00:00 | 2024-03-01 02:00:00 | 2024-03-01 02:00:00 | null |
| 1 | mid-market | IN | 2024-03-02 09:00:00 | 2024-03-03 02:00:00 | 2024-03-03 02:00:00 | null |
Hints
Hint 1
With no column that tracks changes, use the check strategy: dbt compares column values between the source and the current version.Hint 2
check_cols='all' would compare last_seen_at too, and create a new version on almost every run.Hint 3
List only the columns whose changes matter:check_cols=['segment', 'country'].Learn the concepts
- Snapshots (SCD Type 2) · 5 min read. Record how mutable source rows change over time with the timestamp and check strategies.