Last active
October 1, 2021 09:44
-
-
Save jeremyyeo/699eca6aeb341ec196821a629d3d65e4 to your computer and use it in GitHub Desktop.
Conditionally adding things to a list in a dbt macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| > 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