How can I use {{this}} as a parameter in a dbt jinja macro?

Viewed 1452

How can I pass the name of the current model using {{this}} as a parameter for a macro in a config function?

I have tried a couple of options and none of them works. model/Table1.sql

{{ config(post_hook= calculate_test("{{this}}") ) }}

macro/calculate_test.sql

{% macro calculate_test(tableN) %}
   
    {%- set tableName = tableN -%}

    {% set sql %}

        SELECT 
            COUNT(*) as cnt
        FROM {{ tableName }}

    {% endset %}

    {% set results = run_query(sql) %}

{% endmacro %}

The error is: enter image description here

1 Answers

You’re almost there! Only thing is that you’ve got extra curly brackets and misplaced quotes. The entire config block is Jinja so you don’t need the curly brackets around ‘this’.

{{ config(post_hook= “calculate_test(this)” ) }}
Related