dbt Macros & Jinja
When to Use
Activate this skill when the task involves:
- Writing dbt macros from scratch or improving existing ones
- Using Jinja control flow, filters, and variables in dbt SQL
- Implementing cross-database macros with
adapter.dispatch - Dynamic SQL generation via
run_query / execute - Overriding built-in dbt macros (
generate_schema_name, generate_alias_name) - Writing macros that call
adapter methods (schema introspection, DDL) - Organizing macros across files and packages
Jinja Fundamentals
Three Delimiters
jinja
{{ expression }} {# outputs a value into SQL #}
{% statement %} {# control flow: for, if, set, do — produces no output #}
{# comment #} {# stripped at compile time #}
Variables
jinja
{% set payment_methods = ["credit_card", "bank_transfer", "gift_card"] %}
{% set threshold = 1000 %}
{% set label = "high_value" if threshold > 500 else "low_value" %}
Whitespace Control
Jinja preserves whitespace by default. Use - to strip it:
jinja
{%- set x = 1 -%} {# strip left AND right #}
{{- column_name -}} {# strip around expression #}
Filters
jinja
{{ column | upper }} -- COLUMN
{{ column | lower }} -- column
{{ column | replace(" ", "_") }} -- col_name
{{ items | join(", ") }} -- a, b, c
{{ items | list | length }} -- 3
{{ value | default("unknown") }} -- 'unknown' if value is falsy
{{ items | reject("equalto", "x") | list }} -- removes "x" from list
{{ text | trim }} -- strips leading/trailing whitespace
{{ value | int }} -- cast to Python int
{{ value | string }} -- cast to string
Control Flow
jinja
{# if/elif/else #}
{% if target.name == "prod" %}
limit 1000000
{% elif target.name == "dev" %}
limit 1000
{% else %}
limit 100
{% endif %}
{# for loop with loop variable #}
{% for col in columns %}
{{ col }}{%- if not loop.last -%},{%- endif %}
{% endfor %}
{# loop.index (1-based), loop.index0 (0-based), loop.first, loop.last #}
{% for col in columns %}
{{ loop.index }}: {{ col }}
{% endfor %}
Macro Basics
sql
-- macros/cents_to_dollars.sql
{% macro cents_to_dollars(column_name, scale=2) %}
({{ column_name }} / 100.0)::numeric(16, {{ scale }})
{% endmacro %}
Call in a model:
sql
-- models/stg_payments.sql
select
payment_id,
{{ cents_to_dollars('amount') }} as amount_usd,
{{ cents_to_dollars('refund_amount', scale=4) }} as refund_usd
from {{ ref('raw_payments') }}
Compiled output:
sql
select
payment_id,
(amount / 100.0)::numeric(16, 2) as amount_usd,
(refund_amount / 100.0)::numeric(16, 4) as refund_usd
from analytics.raw_payments
Macro Returning a Value
Use return() to return a value for use in {% set %}:
jinja
{% macro is_incremental_model() %}
{{ return(model.config.materialized == 'incremental') }}
{% endmacro %}
{% if is_incremental_model() %}
-- incremental-only logic
{% endif %}
do Tag — Side-Effect Calls
jinja
{% do log("Starting transform for " ~ model.name, info=true) %}
{% do adapter.create_schema(api.Relation.create(database=target.database, schema=target.schema ~ "_tmp")) %}
Context Variables
this — Current Relation
jinja
-- The current model's fully-qualified relation:
{{ this }} -- "analytics"."dbt_prod"."orders"
{{ this.database }} -- "analytics"
{{ this.schema }} -- "dbt_prod"
{{ this.identifier }} -- "orders"
{{ this.include(database=false) }} -- "dbt_prod"."orders"
Use this to reference the current table in incremental models:
sql
{% if is_incremental() %}
where event_at > (select max(event_at) from {{ this }})
{% endif %}
target — Warehouse Connection
jinja
{{ target.name }} -- "dev" | "prod" | "ci"
{{ target.schema }} -- "dbt_ivan"
{{ target.database }} -- "analytics"
{{ target.type }} -- "postgres" | "snowflake" | "bigquery" | "spark"
{{ target.threads }} -- 4
Use to branch logic by environment:
sql
{% if target.name == "prod" %}
{{ config(materialized="table") }}
{% else %}
{{ config(materialized="view") }}
{% endif %}
model — Node Metadata
jinja
{{ model.name }} -- "stg_orders"
{{ model.config.schema }} -- "staging"
{{ model.config.materialized }} -- "incremental"
{{ model.unique_id }} -- "model.my_project.stg_orders"
{{ model.fqn }} -- ["my_project", "staging", "stg_orders"]
{{ model.tags }} -- ["daily", "finance"]
{{ model.refs }} -- list of ref() calls in this model
execute — Compile vs. Execute Guard
execute is True only when dbt is running the model (not during parsing or dbt compile). Always wrap run_query and DDL calls in {% if execute %}:
jinja
{% if execute %}
{% set results = run_query("select distinct region from dim_geo") %}
{% set regions = results.columns[0].values() %}
{% else %}
{% set regions = [] %}
{% endif %}
select
order_id,
{% for region in regions %}
sum(case when region = '{{ region }}' then revenue end) as {{ region | lower | replace(' ', '_') }}_revenue
{%- if not loop.last %},{% endif %}
{% endfor %}
from {{ ref('fct_orders') }}
group by 1
var() — Project Variables
jinja
{# dbt_project.yml:
vars:
lookback_days: 90
start_date: "2023-01-01"
#}
{{ var('lookback_days') }} -- 90
{{ var('lookback_days', default=30) }} -- 30 if not defined
-- Override at runtime:
-- dbt run --vars '{"lookback_days": 30}'
env_var() — Environment Variables
jinja
{{ env_var('DBT_SCHEMA_PREFIX', 'dbt_') }} -- value or default
{{ env_var('DATABASE_PASSWORD') }} -- required; raises if absent
run_query — Dynamic SQL Execution
run_query sends SQL to the warehouse and returns an agate Table result. Always guard with `{% if execute %}`.
agate Table Access
jinja
{% set query %}
select distinct
payment_method,
count(*) as cnt
from {{ ref('stg_payments') }}
group by 1
order by 2 desc
{% endset %}
{% if execute %}
{% set results = run_query(query) %}
{% set methods = results.columns['payment_method'].values() %}
{% set counts = results.columns['cnt'].values() %}
{% else %}
{% set methods = [] %}
{% set counts = [] %}
{% endif %}
| Access Pattern | Code | |
|---|
| Column by index | results.columns[0].values() | |
| Column by name | results.columns['col_name'].values() | |
| Single value | results.rows[0][0] | |
| Row count | `results \ | length` | |
| Print table | results.print_table() | |
DDL / DML Macros (non-SELECT)
jinja
{% macro truncate_staging(table_name) %}
{%- set schema = target.schema ~ "_staging" -%}
{% if execute %}
{% do run_query("truncate table " ~ schema ~ "." ~ table_name) %}
{% do log("Truncated " ~ schema ~ "." ~ table_name, info=true) %}
{% endif %}
{% endmacro %}
Only run during dbt run / dbt build, skip on dbt compile and dbt docs generate:
jinja
{% if execute and flags.WHICH in ('run', 'build') %}
{% do run_query("delete from " ~ this ~ " where created_at < current_date - 90") %}
{% endif %}
adapter Methods
Schema Introspection
jinja
{# Get all columns in a relation #}
{%- set cols = adapter.get_columns_in_relation(this) -%}
{% for col in cols %}
{{ col.name }} ({{ col.data_type }}, nullable={{ col.is_nullable }})
{% endfor %}
{# Check if a relation exists before referencing it #}
{%- set rel = adapter.get_relation(
database=target.database,
schema=target.schema,
identifier='my_table') -%}
{% if rel is not none %}
-- table exists, safe to select from it
select * from {{ rel }}
{% endif %}
DDL Operations
jinja
{# Create a schema #}
{% do adapter.create_schema(
api.Relation.create(database=target.database, schema=target.schema ~ "_audit")
) %}
{# Drop a relation #}
{%- set stale = adapter.get_relation(database=target.database, schema=target.schema, identifier='old_table') -%}
{% if stale is not none %}
{% do adapter.drop_relation(stale) %}
{% endif %}
{# Rename a relation #}
{% do adapter.rename_relation(from_relation, to_relation) %}
adapter.dispatch — Cross-Database Macros
The core pattern for writing macros that behave differently per SQL dialect:
jinja
-- macros/safe_divide.sql
{% macro safe_divide(numerator, denominator) -%}
{{ return(adapter.dispatch('safe_divide', 'my_project')(numerator, denominator)) }}
{%- endmacro %}
-- Default: works on PostgreSQL, Trino, DuckDB
{% macro default__safe_divide(numerator, denominator) %}
case when {{ denominator }} = 0 then null
else {{ numerator }}::float / {{ denominator }}
end
{% endmacro %}
-- BigQuery uses SAFE_DIVIDE
{% macro bigquery__safe_divide(numerator, denominator) %}
SAFE_DIVIDE({{ numerator }}, {{ denominator }})
{% endmacro %}
-- Spark SQL
{% macro spark__safe_divide(numerator, denominator) %}
case when {{ denominator }} = 0 then null
else {{ numerator }} / {{ denominator }}
end
{% endmacro %}
Dispatch resolution order for postgres:
my_project.postgres__safe_dividemy_project.default__safe_divide
Override a Package Macro via dbt_project.yml
yaml
# dbt_project.yml
dispatch:
- macro_namespace: dbt_utils
search_order: ['my_project', 'dbt_utils']
- macro_namespace: dbt
search_order: ['my_project', 'my_org_macros', 'dbt']
With this config, dbt_utils.generate_surrogate_key will first look for my_project.postgres__generate_surrogate_key before falling back to dbt_utils.
Cross-Database Built-in Macros (dbt.*)
These are the dbt namespace macros that abstract SQL dialect differences:
sql
-- Date / time
{{ dbt.date_trunc("month", "created_at") }} -- date_trunc('month', created_at)
{{ dbt.dateadd("day", 7, "created_at") }} -- created_at + interval '7 day'
{{ dbt.datediff("start_date", "end_date", "day") }} -- end_date::date - start_date::date
{{ dbt.current_timestamp() }} -- now() / current_timestamp / getdate()
{{ dbt.last_day("created_at", "month") }} -- last day of month
-- Types
{{ dbt.type_string() }} -- TEXT (PG) / VARCHAR (Snowflake) / STRING (BQ)
{{ dbt.type_timestamp() }} -- TIMESTAMP
{{ dbt.type_bigint() }} -- BIGINT
{{ dbt.type_int() }} -- INT
{{ dbt.type_float() }} -- FLOAT
{{ dbt.type_numeric() }} -- NUMERIC(28,6)
{{ dbt.type_boolean() }} -- BOOLEAN
-- Casting
{{ dbt.safe_cast("user_id", dbt.type_bigint()) }} -- cast(user_id as BIGINT)
{{ dbt.cast("amount", dbt.type_numeric()) }} -- cast(amount as NUMERIC(28,6))
-- Strings
{{ dbt.concat(["first_name", "' '", "last_name"]) }} -- first_name || ' ' || last_name
{{ dbt.hash("order_id") }} -- md5(cast(order_id as varchar))
{{ dbt.length("email") }} -- length(email)
{{ dbt.replace("status", "'_'", "' '") }} -- replace(status, '_', ' ')
{{ dbt.split_part("full_name", "' '", 1) }} -- split_part(full_name, ' ', 1)
-- Aggregates
{{ dbt.listagg("tag", "','", "order by tag") }} -- array_to_string(array_agg(tag order by tag), ',')
{{ dbt.any_value("description") }} -- any(description)
{{ dbt.bool_or("is_active") }} -- bool_or(is_active)
Built-in Override Macros
generate_schema_name — Custom Schema Logic
Override to prevent dbt from prefixing custom schemas with the target schema: