Passing a source table to a macro

Maybe I am missing the obvious, but I have a macro which can do a select out of multiple source tables, so I need to pass the table name. I am confused on what type parameter to pass to the macro so I can use it in my FROM statement. The source table has been defined in a .yml by name and identifier only.

The from statement would look like this if a source statement is used in the macro.

FROM {{ source( 'source_name_in_yml', {{table_name}} ) }}

This doesn’t work.

Is there a way to set the source table in the yml to make this easier? Or do I need to define a variable? Or both?


Hey @cjremley :wave:

So, in Jinja, once you’re already inside curly brackets {{ }}, you don’t need to use them again.

Try something like:

FROM {{ source( 'source_name_in_yml', table_name ) }}

If that doesn’t fix it, let me know! If you can also share the full macro code (i.e. everything from {% macro my_macro(table_name) %} to {% endmacro %}, and a snippet of how you’re calling the macro (i.e. {{ my_macro('my_table') }} ), I’ll likely be able to help out more easily here.

Thanks for the quick answer Claire. That worked. I hadn’t figured that nuance in Jinja out yet.

1 Like