Search⌘ K
AI Features

Solution: Daily Aggregation Macro

Understand how to create and use a daily aggregation macro in dbt with Jinja templating. Explore passing arguments to macros to dynamically count values in different fields, and apply this in multiple models for efficient data aggregation.

We'll cover the following...

Solution

Let’s review the solution to the problem.

{% macro daily_count(field_to_count) %}
SELECT 
    order_date, 
    COUNT(DISTINCT {{field_to_count}} ) AS {{field_to_count}}
FROM {{ ref("orders") }}
GROUP BY order_date
{% endmacro %}
Solution code: Creating a daily_count macro

Code explanation

In the daily_count.sql file: ...