📘 Introduction

In this hands-on dbt tutorial, you'll learn how to use project variables to make your SQL models more dynamic, flexible, and reusable. Instead of hardcoding values directly into your SQL logic, dbt allows you to define variables in your dbt_project.yml file. These variables can be adjusted at runtime or reused across multiple models, making your transformations cleaner and easier to maintain.

✅ Prerequisites

☑️ A dbt project set up
☑️ Set up Medallion Architecture
☑️ Source data loaded into your data warehouse
☑️ Source configurations defined in sources.yml

💡What Are Project Variables in dbt?

Project variables in dbt are values defined in your dbt_project.yml file under the vars configuration. These variables can be scoped globally or to a specific package imported into your project. They are accessible throughout your dbt models, macros, and tests using the {{ var('variable_name') }} function.

🧠 Practical Example

Let’s walk through an example. In our dbt project, the table dim_course contains the following data:

We want to create a new dbt model that filters courses from the table dim_course based on their number of credits. The filter threshold should be flexible, allowing us to change it easily without rewriting SQL code — simply by modifying the value of the variable.

In a previous post, we solved this requirement by using a Jinja variable defined directly in a dbt model:

How to use variables in your dbt models with Jinja
📘 Introduction In this hands-on dbt tutorial, you’ll learn how to use Jinja variables inside your dbt models to make your SQL code more dynamic, flexible, and reusable. Instead of hardcoding values directly into your SQL logic, Jinja allows you to define variables that can be adjusted at runtime or

Now, we want to define a variable globally in the entire dbt project so it can be used by multiple models.

✍️1️⃣ Define the project variable in dbt_project.yml

In your dbt_project.yml file, define a variable under the vars configuration:

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In