By the end of this lesson, you’ll understand:

  • Explain what variables are in dbt

  • Define variables in dbt_project.yml, the CLI, and via environment variables

  • Use variables inside SQL models with Jinja (var())

  • Apply a real example: filter high-value orders using a threshold

What are variables in dbt ?

In dbt, variables are named values (key: value) you can inject into your project at runtime. They help you parameterize your models so the same SQL can behave differently depending on the environment, scenario, or business rule (without rewriting code).

Why use variables?

  • ReusabilityDefine a value once and use it across models.

  • Flexibility: Change behavior without editing code.

Ways to define variables

1. Project variables:

The variable are set in dbt_project.yml file. Use this when you want project defaults variables. 

 
dbt_project.yml
vars:
  default_country: 'US'
  high_value_threshold: 500

2. Command-line variables:

The variable are passed when running dbt using --vars parameter

dbt run -v --vars '{"default_country": "FR"}'

3. Environment variables: 

The variables are defined using  Jinja macros. Use this when the value should come from the environment (CI, secrets manager, etc.).

profiles.yml
user: "{{ env_var('DB_USER') }}"
password: "{{ env_var('DB_PASSWORD') }}"

Example: Using Variables in the E-Commerce Project

Imagine you want to build a high-value orders model that keeps only orders above a certain amount. Instead of hardcoding 500 in your SQL, you’ll use a dbt variable so the threshold can be changed without editing the model.

models/staging/stg_high_value_orders.sql
select
  orderid,
  customerid,
  amount,
  status,
  order_date
from {{ source('ecommerce', 'orders') }}
where amount > {{ var('high_value_threshold', 500) }}

What’s happening here?

  • high_value_threshold is a dbt variable (usually defined in dbt_project.yml).

  • If it’s not provided, dbt uses the default value (500) from var('...', 500).

  • You can override it at runtime from the CLI to  400 (useful in CI/CD or quick tests).

dbt run -v --vars '{"high_value_threshold": 400}'