#066: Start Using Jinja in dbt Macros (3 Examples)

May 22, 2024

If you're using dbt, at some point you'll need to create a Macro.

If you're not familiar with Macros, essentially they are like "functions" in other programming languages.

They're used to create re-usable bits of code and make your project much more dynamic.

It's truly one of the most valuable parts of using dbt.


But the deeper you get into macros, the more you'll also run into Jinja.

And while powerful, Jinja itself has a bit of a learning curve.

In short, Jinja is a templating language that allows you to compile more complex queries.

Think of it like templating a website with reusable components (header, body, footer).

But instead of website parts, we're able to template SQL queries.


Like many things, this can get really complicated.

But if you know how to use a few key functions of Jinja, you'll be able to cover a wide variety of scenarios.


So with that said, in this week's video I want to walk you through 3 examples of how to use helpful Jinja functions within a custom dbt macro.

Whether you're brand new to Jinja or need a quick refresher, this will be a great working-session style video for you to check out.



The main things we'll cover include: 

  1. Using SET variables
  2. Running SQL queries in Macros
  3. Conditional IF statements




Get clarity on common tools & components of a modern data stack

Get started with The Starter Guide for Modern Data to help you cut through the noise & better understand common "modern" architectures.

You'll also get free weekly emails with helpful tips & tutorials.