How to use tags as Macro parameters

Hello !

Do you know how to import tags in a macro ?
Here is an example of what I would like to do:

{% macro my_macro(tag) %}
           {% if var('tag') == 'hourly' %}
              Do stuff
          {% elif var('tag') == 'daily' %}
              Do other stuff
          {% endif %}
{% endmacro %}

Thank you for your help
Cheers

The code you’ve written is looking for a variable defined in your dbt_project.yml called tag. To access the tags on a model, you can use the {{ model }} object: model | dbt Developer Hub

Keep in mind that it could be an array, so you’d have to do an in check, something like this:

--models/some_model.sql
select *
from {{ ref('an_upstream_model') }}
{{ filter_based_on_tag(model.tags) }}
--macros/filter_based_on_tag.sql
{% macro filter_based_on_tag(tags) %}
where 
  {% if execute %} -- https://docs.getdbt.com/reference/dbt-jinja-functions/execute
    {% if 'hourly' in tags %}
      some_thing > dateadd('hour', -1, getdate())
    {% elif 'daily' in tags %}
      some_thing > dateadd('day', -1, getdate())
    {% else %}
      true
    {% endif %}
  {% endif %}
{% endmacro %}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.