Variables and Parameters
Variables allow you to make your dbt project more dynamic and configurable by passing values at runtime or setting them in configuration files. They enable you to create flexible data transformations that can adapt to different environments, use cases, and scenarios.
Understanding dbt Variables
Variables in dbt serve two primary purposes:
Make code reusable - Define values once and reference them throughout your project
Enable flexibility - Change behavior without modifying code
There are several ways to define and use variables in dbt:
Project variables - Defined in
dbt_project.yml
Command-line variables - Passed at runtime
Environment variables - Accessed via Jinja macros
Defining Variables in dbt_project.yml
The simplest way to define variables is in your dbt_project.yml
file:
These variables become available throughout your project via the var()
function.
Using Variables in Models
Once defined, you can reference variables in your models using the var()
function:
The var()
function has two parameters:
The variable name
An optional default value that's used if the variable isn't defined
Variable Behavior
When you use the var()
function:
It will use the variable from
dbt_project.yml
if definedCommand-line variables override values from
dbt_project.yml
If no variable is found and no default is specified, dbt will raise an error
Environment-specific variables (
dev
,prod
) are only used when running in that environment
Passing Variables at Runtime
For maximum flexibility, pass variables at runtime using the --vars
flag:
You can pass complex structures too:
Runtime variables override any variables defined in dbt_project.yml
.
Working with Environment Variables
You can access environment variables using the env_var
Jinja function:
This is particularly useful for sensitive information (like API keys) or values that vary by environment.
Security Note
Never use env_var()
for credentials that should remain secret. These values could be exposed in compiled SQL or logs. Instead, use your platform's secure environment variable handling for credentials.
Advanced Variable Techniques
Conditional Logic with Variables
Variables allow you to implement conditional logic in your models:
Dynamic Filtering
Create flexible filtering based on variable values:
Date/Time Variables
A common pattern for incremental models is using variables for date ranges:
Best Practices for Variables
Set meaningful defaults
Provide sensible default values to make your code more robust
Use descriptive names
Choose clear, explicit variable names that explain purpose
Document variables
Add comments in dbt_project.yml
to explain each variable's purpose
Consistent formatting
Maintain consistent casing and naming conventions
Avoid hardcoding
Use variables instead of hardcoding values that might change
Example: Well-Structured Variables
Common Use Cases
Environment-Specific Configuration
Define different behavior based on your deployment environment:
Parameterized Reporting
Create reports with customizable parameters:
Then run with different settings:
By effectively using variables in your dbt project, you create more flexible, maintainable, and reusable data transformations that can easily adapt to different needs and environments without code changes.
Last updated
Was this helpful?