dbt variable
A dbt variable is a value you define once and can reuse
across many models and macros in your project.
Key points:
- Define it in dbt_project.yml under the vars section, or
override it at runtime with the --vars option.
- You can also set or read values via environment
variables (DBT_VAR_<name>).
How to use:
- Retrieve a variable with the Jinja function var():
- Example: where
created_at >= date '{{ var('start_date') }}'
- If the variable might be missing, provide a default:
- Example: {{
var('start_date', '2024-01-01') }}
Common uses:
- Cross-environment configuration (e.g., dev, staging,
prod) by changing variables.
- Configuring package behavior or feature flags within
the project.
Note:
- If a variable is a string, wrap it in quotes inside
your SQL where needed.
Youtube video
https://youtu.be/pwJwkOi_exk
0 Comments
Thanks for your message.