25. Close Rows That Disappear from the Source
Difficulty: Hard · Topic: Snapshots
The billing system deletes a subscription's row from {{ source('billing', 'subscriptions') }} (subscription_id, customer_id, plan, updated_at) when it is cancelled. Sometimes a cancelled subscription is reactivated and its row comes back.
Write the snapshot subscriptions_snapshot (all four columns), with versions dated by updated_at. When a subscription disappears from the source, its current version must be closed (dbt_valid_to = the time of the snapshot run that noticed), so that "active subscriptions" (dbt_valid_to is null) stops counting it. If it comes back, it starts a new version.
Graded over several snapshot runs against the same table. 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-01-10 00:00:00)
{{ source('billing', 'subscriptions') }}
| subscription_id | customer_id | plan | updated_at |
|---|---|---|---|
| 100 | 1 | monthly | 2024-01-01 00:00:00 |
| 101 | 2 | annual | 2024-01-05 00:00:00 |
| 102 | 3 | monthly | 2024-01-08 00:00:00 |
Expected table after run 1
| subscription_id | customer_id | plan | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 100 | 1 | monthly | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | null |
| 101 | 2 | annual | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | null |
| 102 | 3 | monthly | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | null |
Run 2 (at 2024-02-10 00:00:00)
{{ source('billing', 'subscriptions') }}
| subscription_id | customer_id | plan | updated_at |
|---|---|---|---|
| 100 | 1 | annual | 2024-02-01 00:00:00 |
| 102 | 3 | monthly | 2024-01-08 00:00:00 |
Expected table after run 2
| subscription_id | customer_id | plan | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 100 | 1 | monthly | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | 2024-02-01 00:00:00 |
| 101 | 2 | annual | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | 2024-02-10 00:00:00 |
| 102 | 3 | monthly | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | null |
| 100 | 1 | annual | 2024-02-01 00:00:00 | 2024-02-01 00:00:00 | 2024-02-01 00:00:00 | null |
Run 3 (at 2024-03-10 00:00:00)
{{ source('billing', 'subscriptions') }}
| subscription_id | customer_id | plan | updated_at |
|---|---|---|---|
| 100 | 1 | annual | 2024-02-01 00:00:00 |
| 101 | 2 | monthly | 2024-03-04 00:00:00 |
| 102 | 3 | monthly | 2024-01-08 00:00:00 |
Expected table after run 3
| subscription_id | customer_id | plan | updated_at | dbt_updated_at | dbt_valid_from | dbt_valid_to |
|---|---|---|---|---|---|---|
| 100 | 1 | monthly | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | 2024-01-01 00:00:00 | 2024-02-01 00:00:00 |
| 101 | 2 | annual | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | 2024-01-05 00:00:00 | 2024-02-10 00:00:00 |
| 102 | 3 | monthly | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | 2024-01-08 00:00:00 | null |
| 100 | 1 | annual | 2024-02-01 00:00:00 | 2024-02-01 00:00:00 | 2024-02-01 00:00:00 | null |
| 101 | 2 | monthly | 2024-03-04 00:00:00 | 2024-03-04 00:00:00 | 2024-03-04 00:00:00 | null |
Hints
Hint 1
By default a snapshot ignores rows that disappear: their last version stays current forever.Hint 2
dbt 1.9+ hashard_deletes='invalidate' (older projects use invalidate_hard_deletes=True): the current version of a missing key is closed at the snapshot's run time.Hint 3
The rest is a normal timestamp snapshot:unique_key='subscription_id', strategy='timestamp', updated_at='updated_at'.Learn the concepts
- Snapshots (SCD Type 2) · 5 min read. Record how mutable source rows change over time with the timestamp and check strategies.