The is_incremental() macro helps you know if your dbt model is running as an incremental update or a full refresh. This lets you write code that only adds new data, saving time and resources.
is_incremental() macro in dbt
{{ is_incremental() }}This macro returns true if the model is running incrementally, otherwise false.
Use it inside {% raw %}{% if %}{% endif %}{% endraw %} blocks to write conditional SQL.
{% raw %}{% if is_incremental() %}
-- SQL to add only new rows
{% else %}
-- SQL to build full table
{% endif %}{% endraw %}{% raw %}{% if is_incremental() %}
SELECT * FROM source_table WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% else %}
SELECT * FROM source_table
{% endif %}{% endraw %}This dbt model uses is_incremental() to load only new or updated rows when running incrementally, and loads all rows on full refresh.
{% raw %}
-- dbt model SQL file
{% if is_incremental() %}
SELECT id, name, updated_at
FROM source_table
WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% else %}
SELECT id, name, updated_at
FROM source_table
{% endif %}
{% endraw %}Remember to define a unique key or timestamp column to identify new or changed rows for incremental loads.
If you run a full refresh, is_incremental() returns false, so your model rebuilds completely.
Use is_incremental() only inside dbt model SQL files or macros.
is_incremental() tells if the model runs incrementally or fully.
Use it to write SQL that adds only new data when possible.
This saves time and computing resources on large datasets.
Practice
is_incremental() macro in dbt indicate when used inside a model?Solution
Step 1: Understand the purpose of
This macro returns true if the model is running incrementally, false if running a full-refresh.is_incremental()Step 2: Identify what it indicates in the model
It tells the model whether to add only new data or rebuild fully.Final Answer:
Whether the model is running in incremental mode or full-refresh mode -> Option BQuick Check:
is_incremental()= incremental mode check [OK]
is_incremental() checks incremental run status [OK]- Thinking it checks for errors
- Confusing it with row count
- Assuming it checks connection status
is_incremental() in a dbt model SQL file?Solution
Step 1: Recognize correct Jinja syntax for
It must be used inside a Jinja if statement with {% %} tags.is_incremental()Step 2: Check the options for correct usage
{% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% endif %} uses {% if is_incremental() %} correctly to conditionally add a WHERE clause.Final Answer:
{% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% endif %} -> Option CQuick Check:
Use Jinja {% if is_incremental() %} for conditional SQL [OK]
- Using is_incremental() directly in SQL WHERE clause
- Missing Jinja tags {% %}
- Using is_incremental as a column or variable
SELECT * FROM source_table
{% if is_incremental() %}
WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}Solution
Step 1: Understand the condition inside the incremental block
When running incrementally, the WHERE clause filters rows with updated_at greater than the max in the current model.Step 2: Determine which rows are selected
This means only new or updated rows after the last run are selected.Final Answer:
Only rows with updated_at greater than the max in the existing model -> Option AQuick Check:
Incremental filter selects newer rows [OK]
- Thinking all rows are selected every time
- Confusing greater than with less than
- Ignoring the existing model's max timestamp
SELECT * FROM source_table
{% if is_incremental %}
WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}What is the error?
Solution
Step 1: Check the usage of
The macro must be called with parentheses:is_incrementalis_incremental().Step 2: Identify the error in the if statement
The code usesis_incrementalwithout parentheses, causing a syntax error.Final Answer:
Missing parentheses afteris_incrementalmacro call -> Option DQuick Check:
Always callis_incremental()with parentheses [OK]
is_incremental() needs parentheses [OK]- Forgetting parentheses on macro calls
- Confusing {{ }} and {% %} tags
- Assuming macro is a variable
is_incremental() to achieve this?Solution
Step 1: Understand incremental and full-refresh logic
When running incrementally, filter new rows; on full-refresh, select all rows.Step 2: Check which option handles both cases correctly
SELECT * FROM source_table {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% else %} -- full refresh: select all rows {% endif %} usesis_incremental()with an else block for full-refresh selecting all rows.Final Answer:
SELECT * FROM source_table {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% else %} -- full refresh: select all rows {% endif %} -> Option AQuick Check:
Useis_incremental()with else for full-refresh [OK]
is_incremental() for full-refresh [OK]- Not handling full-refresh case
- Using wrong comparison operators
- Filtering incorrectly on first run
Ready to prove it?
Pass the test and this pattern moves unknown
