From the course: Complete Guide to Analytics Engineering
Unlock this course with a free trial
Join today to access over 25,600 courses taught by industry experts.
dbt macros
From the course: Complete Guide to Analytics Engineering
dbt macros
- [Instructor] DBT and other data modeling tools have some great advantages over just writing queries in the database. One of these is called a macro. Macros are stored in the DBT instance and can house a snippet of SQL code or Python code that can be referenced and used in multiple data models. Here's an example. In a query from an earlier video, we needed to find the difference in days between two date values. It looked a bit like this, where we have max, Julian day order date minus minimum Julian order date. It's a fairly long function just to find the difference between the earliest and latest dates in a column. If we created a DBT macro, we could reuse this logic again and again and not have to type the whole thing out each time. Let's say we name our macro min_max_date_macro. And then when we use this function, we'll pass in a field name like order date, then we close a syntax like this. Then we write out the SQL code snippet. So in this case it was max, Julian day, and we used…