Search⌘ K
AI Features

Coding Challenge: Pack Your Macro

Explore how to incorporate a macro into a dbt package to perform distinct counts. Learn to import the macro into a project and build a model using the macro to calculate daily distinct product counts, improving your dbt package management and macro usability.

We'll cover the following...

Problem

Here’s a macro that groups by a column and counts distinct values of a field:

MySQL
{% 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 %}

As this macro is ...