Skip to content
Advertisement

dbt macro – using *args/**kwargs

Occasionally, our tables need to append multiple columns together to make a unique key. This can be done by doing something like:

This works but lends to a lack of uniformity amongst multiple analysts.

I would like to utilize pythons *args (i think jinja2 uses varargs) feature to make a macro that can take an arbitrary amount of arguments and create a unique key between all of them.

Ideal outcome:

Advertisement

Answer

There is currently a macro in the dbt_utils package that does something similar, called surrogate key. It used to only use varargs and now also allows a list.

For the varargs portion, it does the following:

You can then join() or iterate through that list to do whatever you’d like. In the case of the macro, it does the following:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement