Skip to content

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_idcustomer_idplanupdated_at
1001monthly2024-01-01 00:00:00
1012annual2024-01-05 00:00:00
1023monthly2024-01-08 00:00:00

Expected table after run 1

subscription_idcustomer_idplanupdated_atdbt_updated_atdbt_valid_fromdbt_valid_to
1001monthly2024-01-01 00:00:002024-01-01 00:00:002024-01-01 00:00:00null
1012annual2024-01-05 00:00:002024-01-05 00:00:002024-01-05 00:00:00null
1023monthly2024-01-08 00:00:002024-01-08 00:00:002024-01-08 00:00:00null

Run 2 (at 2024-02-10 00:00:00)

{{ source('billing', 'subscriptions') }}

subscription_idcustomer_idplanupdated_at
1001annual2024-02-01 00:00:00
1023monthly2024-01-08 00:00:00

Expected table after run 2

subscription_idcustomer_idplanupdated_atdbt_updated_atdbt_valid_fromdbt_valid_to
1001monthly2024-01-01 00:00:002024-01-01 00:00:002024-01-01 00:00:002024-02-01 00:00:00
1012annual2024-01-05 00:00:002024-01-05 00:00:002024-01-05 00:00:002024-02-10 00:00:00
1023monthly2024-01-08 00:00:002024-01-08 00:00:002024-01-08 00:00:00null
1001annual2024-02-01 00:00:002024-02-01 00:00:002024-02-01 00:00:00null

Run 3 (at 2024-03-10 00:00:00)

{{ source('billing', 'subscriptions') }}

subscription_idcustomer_idplanupdated_at
1001annual2024-02-01 00:00:00
1012monthly2024-03-04 00:00:00
1023monthly2024-01-08 00:00:00

Expected table after run 3

subscription_idcustomer_idplanupdated_atdbt_updated_atdbt_valid_fromdbt_valid_to
1001monthly2024-01-01 00:00:002024-01-01 00:00:002024-01-01 00:00:002024-02-01 00:00:00
1012annual2024-01-05 00:00:002024-01-05 00:00:002024-01-05 00:00:002024-02-10 00:00:00
1023monthly2024-01-08 00:00:002024-01-08 00:00:002024-01-08 00:00:00null
1001annual2024-02-01 00:00:002024-02-01 00:00:002024-02-01 00:00:00null
1012monthly2024-03-04 00:00:002024-03-04 00:00:002024-03-04 00:00:00null

Hints

Hint 1By default a snapshot ignores rows that disappear: their last version stays current forever.
Hint 2dbt 1.9+ has hard_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 3The rest is a normal timestamp snapshot: unique_key='subscription_id', strategy='timestamp', updated_at='updated_at'.

Learn the concepts

More dbt problems

All dbt problems