Bird
Raised Fist0
dbtdata~5 mins

is_incremental() macro in dbt

Choose your learning style10 modes available
Introduction

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.

When you want to add only new rows to a large table instead of rebuilding it from scratch.
When your data source updates regularly and you want to keep your model up to date efficiently.
When you want to avoid processing all historical data every time you run your model.
When you want to write conditional logic in your model to handle incremental loads differently.
When you want to improve performance by processing only changed or new data.
Syntax
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.

Examples
This example shows how to run different SQL depending on whether the model is incremental or full refresh.
dbt
{% raw %}{% if is_incremental() %}
  -- SQL to add only new rows
{% else %}
  -- SQL to build full table
{% endif %}{% endraw %}
This example selects only new or updated rows when incremental, otherwise selects all rows.
dbt
{% raw %}{% if is_incremental() %}
  SELECT * FROM source_table WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% else %}
  SELECT * FROM source_table
{% endif %}{% endraw %}
Sample Program

This dbt model uses is_incremental() to load only new or updated rows when running incrementally, and loads all rows on full refresh.

dbt
{% 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 %}
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What does the is_incremental() macro in dbt indicate when used inside a model?
easy
A. If the model has any syntax errors
B. Whether the model is running in incremental mode or full-refresh mode
C. The total number of rows in the model
D. The database connection status

Solution

  1. Step 1: Understand the purpose of is_incremental()

    This macro returns true if the model is running incrementally, false if running a full-refresh.
  2. Step 2: Identify what it indicates in the model

    It tells the model whether to add only new data or rebuild fully.
  3. Final Answer:

    Whether the model is running in incremental mode or full-refresh mode -> Option B
  4. Quick Check:

    is_incremental() = incremental mode check [OK]
Hint: Remember: is_incremental() checks incremental run status [OK]
Common Mistakes:
  • Thinking it checks for errors
  • Confusing it with row count
  • Assuming it checks connection status
2. Which of the following is the correct way to use is_incremental() in a dbt model SQL file?
easy
A. WHERE is_incremental = true
B. SELECT * FROM table WHERE is_incremental() = true
C. {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% endif %}
D. SELECT * FROM table WHERE incremental = is_incremental()

Solution

  1. Step 1: Recognize correct Jinja syntax for is_incremental()

    It must be used inside a Jinja if statement with {% %} tags.
  2. 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.
  3. Final Answer:

    {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% endif %} -> Option C
  4. Quick Check:

    Use Jinja {% if is_incremental() %} for conditional SQL [OK]
Hint: Use {% if is_incremental() %} to add incremental filters [OK]
Common Mistakes:
  • Using is_incremental() directly in SQL WHERE clause
  • Missing Jinja tags {% %}
  • Using is_incremental as a column or variable
3. Given the following dbt model snippet, what rows will be selected when the model runs incrementally?
SELECT * FROM source_table
{% if is_incremental() %}
  WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}
medium
A. Only rows with updated_at greater than the max in the existing model
B. Only rows with updated_at less than the max in the existing model
C. No rows will be selected
D. All rows from source_table every time

Solution

  1. 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.
  2. Step 2: Determine which rows are selected

    This means only new or updated rows after the last run are selected.
  3. Final Answer:

    Only rows with updated_at greater than the max in the existing model -> Option A
  4. Quick Check:

    Incremental filter selects newer rows [OK]
Hint: Incremental runs add only newer rows based on max timestamp [OK]
Common Mistakes:
  • Thinking all rows are selected every time
  • Confusing greater than with less than
  • Ignoring the existing model's max timestamp
4. You wrote this dbt model code but it fails to run incrementally:
SELECT * FROM source_table
{% if is_incremental %}
  WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
{% endif %}

What is the error?
medium
A. Using {{ }} instead of {% %} for if statement
B. Incorrect SQL syntax in WHERE clause
C. No error, code is correct
D. Missing parentheses after is_incremental macro call

Solution

  1. Step 1: Check the usage of is_incremental

    The macro must be called with parentheses: is_incremental().
  2. Step 2: Identify the error in the if statement

    The code uses is_incremental without parentheses, causing a syntax error.
  3. Final Answer:

    Missing parentheses after is_incremental macro call -> Option D
  4. Quick Check:

    Always call is_incremental() with parentheses [OK]
Hint: Remember: is_incremental() needs parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on macro calls
  • Confusing {{ }} and {% %} tags
  • Assuming macro is a variable
5. You want to create a dbt model that updates only new rows incrementally but also handles the case when the model is run for the first time (empty table). Which code snippet correctly uses is_incremental() to achieve this?
hard
A. SELECT * FROM source_table {% if is_incremental() %} WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }}) {% else %} -- full refresh: select all rows {% endif %}
B. SELECT * FROM source_table WHERE updated_at > (SELECT MAX(updated_at) FROM {{ this }})
C. {% if is_incremental() %} SELECT * FROM source_table {% else %} SELECT * FROM source_table WHERE updated_at < NOW() {% endif %}
D. SELECT * FROM source_table {% if is_incremental() %} WHERE updated_at < (SELECT MAX(updated_at) FROM {{ this }}) {% endif %}

Solution

  1. Step 1: Understand incremental and full-refresh logic

    When running incrementally, filter new rows; on full-refresh, select all rows.
  2. 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 %} uses is_incremental() with an else block for full-refresh selecting all rows.
  3. 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 A
  4. Quick Check:

    Use is_incremental() with else for full-refresh [OK]
Hint: Use else block with is_incremental() for full-refresh [OK]
Common Mistakes:
  • Not handling full-refresh case
  • Using wrong comparison operators
  • Filtering incorrectly on first run
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard