Occasionally, our tables need to append multiple columns together to make a unique key. This can be done by doing something like:
select
*,
col1 || "_" || col2 as unique_key
from
my_table
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:
select
*,
unique_key(col1, col1, ..., colN)
from
my_table
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: