
#066: Start Using Jinja in dbt Macros (3 Examples)
May 22, 2024If 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:
- Using SET variables
- Running SQL queries in Macros
- Conditional IF statements
Enjoy!
Michael
When you're ready, here are 2 other ways I can help you:
1. Simple Stack Academy: Level-up your data career by learning more than just SQL. Design, build & automate an end-to-end data architecture using modern tools like dbt⢠& Github. (Free 7 Day Preview)
2. Consulting Services: Get personalized support to properly implement a well-structured, scalable, and maintainable data architecture at your company.