hands-on lab

Understand and Use dbt Jinja Macros

Up to 1h
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.
Lab description

dbt is a powerful tool to leverage existing data, model and transform it, and finally materialize it in order to let business stakeholders access the data. dbt is also an easy-to-use and rich-in features tool. Macros are one of them.

Macros are functions written in Jinja, a great templating language, that enables you to use programming structures like if/else and loops, define logic, and change the way your project builds depending on conditions. You can also access environment variables. They are mainly used to represent and materialize a logic you want to implement in your dbt transformations.

By using dbt, you can use some native macros such as ref and source, but you can also define custom ones. dbt also offers you the possibility to implement Jinja code directly into your dbt models, without the need of defining a macro.

In this lab, you will understand what are macros, you will use native dbt macros, and you will define a custom macro.

Learning Objectives

Upon completion of this lab, you will be able to:

  • Understand what are Jinja macros and how to use them
  • Create custom Jinja macros

Intended Audience

  • Data engineers who need to use dbt macros
  • Developers who need to develop reusable logics in dbt


To get the most from this lab, you should have basic knowledge of dbt. To get ready, you can use the following labs:

About the author
Learning paths

Stefano studies Computer Science and is passionate about technology. He loves working with Cloud services and learning all the best practices for them. Google Cloud Platform and Amazon Web Services are the cloud providers he prefers. He is a Google Cloud Certified Associate Cloud Engineer. Node.js is the programming language he always uses to code. When he's not involved in studying or working, Stefano loves riding his motorbike and exploring new places.

Covered topics
Lab steps
Understanding What Are dbt Jinja Macros
Connecting to the dbt Web IDE
Defining a Custom dbt Macro
Creating a Model Leveraging dbt Macros
Executing the dbt Model