DT7: dbt: transformations as tested code

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Materialisations: view, table, incremental

🎯You'll be able to

Choose how each model is stored (view, table, incremental), configure an incremental model with a unique key, and know its limits.

🏠The everyday picture

Some dishes are cooked to order (a view: computed when asked), some are cooked in the morning for the day (a table), and a big pot of dal gets topped up as the day goes on instead of being remade (incremental).

📘What it really is

sql {{ config(materialized='incremental', unique_key='order_line_id', incremental_strategy='delete+insert') }} select ... {% if is_incremental() %} where o.order_date >= (select max(order_date) - interval 3 day from {{ this }}) {% endif %} The unique_key makes re-processing safe (DT3 Lesson 2); the 3-day look-back picks up late rows (DT3 Lesson 3).

📝A worked example

Input: in the solution, reload one day with python -m pipeline.run --date 2026-10-30, then dbt build --profiles-dir . again, and count marts.fct_order_lines.

Expected output: still 10,181 rows and ₹19,65,764 net, because the incremental model deleted and re-inserted the last 3 days' lines by order_line_id instead of appending them. Project 2 test 9 does exactly this.

🌍Where it's used

📝Case scenario

A team made every model incremental "for speed". When they changed how refunds were counted, only new rows used the new rule; the old ones kept the old rule, and month-on-month comparisons became wrong. A full refresh after the change, and tables for the small models, fixed it.

🖱️Try it

In the starter's fct_order_lines, add the incremental config and a where clause for new rows, without a unique_key. Run dbt build twice after reloading a day, and count rows. Then add the key. Success: you saw rows multiply without the key and stay stable with it.

⚠️Common mistakes

🛠️For your project

Project 2 test 9 checks fct_order_lines is incremental with a unique key and gives the same totals after a reload and rebuild.

✅Quick check

When is a table better than a view?

Show the answer

When the model is read often and is cheap enough to rebuild in full.

Why does an incremental model need a unique key?

Show the answer

So re-processed rows replace their old versions instead of being added again.

← PreviousDocumentation and lineage
ProLearners Assist