Skip to content

Instantly share code, notes, and snippets.

@jeremyyeo
Last active October 1, 2021 09:44
Show Gist options
  • Select an option

  • Save jeremyyeo/699eca6aeb341ec196821a629d3d65e4 to your computer and use it in GitHub Desktop.

Select an option

Save jeremyyeo/699eca6aeb341ec196821a629d3d65e4 to your computer and use it in GitHub Desktop.
Conditionally adding things to a list in a dbt macro
{% macro test_macro() %}
{% set contains_1 = [] %}
{% set contains_2 = [] %}
{% set my_dict = [{'a': [1]}, {'b': [1, 2]}, {'c': [2]}] %}
{% for item in my_dict %}
{{ log(item, info=True) }}
{% for key, value in item.items() %}
{% if 1 in value %}
{% do contains_1.append(key) %}
{% endif %}
{% if 2 in value %}
{% do contains_2.append(key) %}
{% endif %}
{% endfor %}
{% endfor %}
{{ log(contains_1, info=True) }}
{{ log(contains_2, info=True) }}
{% endmacro %}
> dbt run-operation test_macro
Running with dbt=0.20.2
{'a': [1]}
{'b': [1, 2]}
{'c': [2]}
['a', 'b']
['b', 'c']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment