Advanced dbt: Incremental Models, Snapshots and Macros

Scale dbt projects with incremental models, snapshots for SCD Type 2, Jinja macros, packages, model contracts and CI with slim builds.

Advanced · about 8 hours · difficulty 4/5

Learn first: dbt Fundamentals, Slowly Changing Dimensions

Once a dbt project grows beyond a few models, full rebuilds get slow and repeated SQL creeps in. These features keep larger projects fast and maintainable.

Incremental models

  • Materialize as incremental and filter new rows with {% if is_incremental() %}.
  • Strategies: append, merge (with a unique_key), delete+insert, insert_overwrite for partitions. Support varies by warehouse.
  • Include a lookback window for late data, and run a periodic --full-refresh if logic changes.

Snapshots

dbt snapshots implement SCD Type 2 for you, using a timestamp or check strategy, and add dbt_valid_from and dbt_valid_to columns. Use them to capture history of mutable source tables.

Jinja and macros

  • dbt SQL is templated with Jinja: loops, conditionals and variables.
  • Macros are reusable SQL functions (for example converting paise to rupees or generating a date spine).
  • Packages such as dbt-utils and dbt-expectations add tested macros; install with packages.yml and dbt deps.

Governance features

  • Model contracts enforce column names and types on important models.
  • Model versions and access levels (public, protected, private) help larger teams.
  • Exposures document dashboards and apps that depend on models.

CI and deployment

Run dbt build --select state:modified+ (slim CI) against a temporary schema on each pull request, so only changed models and their children are built.

Pitfalls

  • Incremental models with no unique key that duplicate rows on reruns.
  • Macro-heavy projects nobody can read. Use Jinja to remove repetition, not to be clever.

You are ready to move on when your dbt project has an incremental fact table, a snapshot-based customer history, one custom macro, a package, and a GitHub Actions workflow running slim CI.

Key concepts

  • Incremental models and strategies
  • is_incremental and lookback windows
  • Snapshots
  • Jinja templating
  • Macros
  • Packages like dbt-utils
  • Model contracts and versions
  • Exposures
  • Slim CI with state selection

Mini project

Extend your dbt project: convert fct_orders to an incremental merge model with a 3-day lookback, add a snapshot for customers, write a macro for currency conversion, and set up slim CI in GitHub Actions.

Check yourself

An incremental model filters on updated_at > max(updated_at) in the target. What happens to a record that arrives two days late, and how do you fix it?

Free resources