0
0
dbtdata~10 mins

is_incremental() macro in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - is_incremental() macro
Start dbt model run
Check if run is incremental?
Run incremental
Append new data
End run
The is_incremental() macro checks if the current dbt model run is incremental or full refresh, guiding the model to append new data or replace all data.
Execution Sample
dbt
select * from source_table
{% if is_incremental() %}
where updated_at > (select max(updated_at) from {{ this }})
{% endif %}
This code selects all data from source_table, but if running incrementally, it only selects new or updated rows.
Execution Table
Stepis_incremental() ResultCondition CheckedAction TakenOutput Description
1TrueIs this an incremental run?Apply WHERE filter to select new rowsOnly rows with updated_at > max(updated_at) in target are selected
2FalseIs this an incremental run?No filter applied, select all rowsAll rows from source_table are selected
3TrueRun incremental appendAppend new rows to existing tableTable grows with new data
4FalseRun full refreshReplace entire table with new dataTable is fully replaced
5-Run endsProcess completeModel run finished
💡 Execution stops after data is either appended (incremental) or fully replaced (full refresh).
Variable Tracker
VariableStartAfter Step 1After Step 3Final
is_incremental()undefinedTrue or False depending on run typeTrue or FalseTrue or False
selected_rowsemptyFiltered rows if incremental, else all rowsRows ready to append or replaceFinal rows loaded into table
Key Moments - 2 Insights
Why does the model select fewer rows when is_incremental() returns True?
Because when is_incremental() is True, the model adds a WHERE clause to select only new or updated rows, as shown in execution_table row 1.
What happens if is_incremental() returns False during a run?
The model runs a full refresh, selecting all rows and replacing the existing table, as shown in execution_table rows 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what action is taken when is_incremental() returns True?
AReplace entire table with new data
BSkip data loading
CAppend new rows to existing table
DDelete existing data without loading
💡 Hint
See execution_table row 3 for the action when is_incremental() is True.
At which step does the model decide to run a full refresh?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check execution_table row 2 where is_incremental() is False.
If the WHERE clause inside the if block is removed, how does the output change when is_incremental() is True?
AAll rows are selected regardless of incremental run
BNo rows are selected
COnly new rows are selected
DModel run fails
💡 Hint
Refer to execution_sample code where the WHERE clause filters rows only if is_incremental() is True.
Concept Snapshot
is_incremental() macro in dbt:
- Returns True if the model run is incremental
- Use inside {% if is_incremental() %} to filter new data
- Incremental runs append data, full refresh replaces
- Helps optimize data loading by avoiding full reloads
- Commonly used with WHERE clauses to select new/updated rows
Full Transcript
The is_incremental() macro in dbt helps the model know if it is running incrementally or as a full refresh. When running incrementally, the macro returns True, so the model can add a filter to select only new or updated rows. This way, the model appends only new data instead of reloading everything. If the macro returns False, the model runs a full refresh and replaces all data. This process optimizes data loading and saves time. The execution table shows steps where the macro result guides the model's behavior, either filtering rows or selecting all. Variables like is_incremental() and selected_rows change accordingly during the run. Understanding this macro helps beginners write efficient dbt models.