Skip to content

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_idsegmentcountrylast_seen_at
1smbIN2024-02-29 18:00:00
2enterpriseSG2024-02-29 20:00:00

Expected table after run 1

customer_idsegmentcountrylast_seen_atdbt_updated_atdbt_valid_fromdbt_valid_to
1smbIN2024-02-29 18:00:002024-03-01 02:00:002024-03-01 02:00:00null
2enterpriseSG2024-02-29 20:00:002024-03-01 02:00:002024-03-01 02:00:00null

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

{{ source('crm', 'customers') }}

customer_idsegmentcountrylast_seen_at
1smbIN2024-03-01 19:00:00
2enterpriseSG2024-03-01 21:00:00

Expected table after run 2

customer_idsegmentcountrylast_seen_atdbt_updated_atdbt_valid_fromdbt_valid_to
1smbIN2024-02-29 18:00:002024-03-01 02:00:002024-03-01 02:00:00null
2enterpriseSG2024-02-29 20:00:002024-03-01 02:00:002024-03-01 02:00:00null

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

{{ source('crm', 'customers') }}

customer_idsegmentcountrylast_seen_at
1mid-marketIN2024-03-02 09:00:00
2enterpriseSG2024-03-02 22:00:00

Expected table after run 3

customer_idsegmentcountrylast_seen_atdbt_updated_atdbt_valid_fromdbt_valid_to
1smbIN2024-02-29 18:00:002024-03-01 02:00:002024-03-01 02:00:002024-03-03 02:00:00
2enterpriseSG2024-02-29 20:00:002024-03-01 02:00:002024-03-01 02:00:00null
1mid-marketIN2024-03-02 09:00:002024-03-03 02:00:002024-03-03 02:00:00null

Hints

Hint 1With no column that tracks changes, use the check strategy: dbt compares column values between the source and the current version.
Hint 2check_cols='all' would compare last_seen_at too, and create a new version on almost every run.
Hint 3List only the columns whose changes matter: check_cols=['segment', 'country'].

Learn the concepts

More dbt problems

All dbt problems