if condition in PostHook in ConfigBlock

Hello

I have a macro that i call in the posthook in the config block of my model. I want to have this macro called in posthook only if a particular query returns a ‘N’.

CURRENT Code Block
{{
config(
post_hook=macro1(“‘header’=‘true’”)
)
}}

WHAT I WANT is that this macro be called in the posthook only if a value of ‘N’ is returned by the condition query.

post_hook=[ if ((select a from b)=‘N’) then Macro1 Else end]

Thanks in advance! I am new to dbt and am stuck here!

-RS

I’m replying in Slack so I’m not sure how well the formatting will be preserved, but:

Inside macro1, or in a “wrapper” macro that calls macro1, use a conditional block similar to this:


    {%- set result = run_query('show streams like \'' ~ stream_name ~ '\' in schema ' ~ stream_schema) -%}
    {%- set stream_exists = (result is not none) and (result.rows | length > 0) -%}
    {%- if stream_exists -%}
        {%- set stream_info = dict(zip(result.columns | map(attribute='name'), result.rows[0])) -%}
        {%- if (stream_info['stale'] | string | lower) == 'true' -%}
            {{ macro1() }}
        {%- endif -%}
    {%- endif -%}

{%- endif -%}```
That example runs a query and then checks if that query returned any rows. If it did return any rows, it checks whether the value of the "stable" column is true in the first row. If both of those conditions are true, it does something (in this case calls macro1), otherwise it does nothing. You can adapt this to check whether your query returns "N".

<sub>Note: `@William` originally [posted this reply in Slack](https://getdbt.slack.com/archives/CBSQTAPLG/p1747866040912929?thread_ts=1747853122.458569&cid=CBSQTAPLG). It might not have transferred perfectly.</sub>

I can see it didn’t transfer well, let me try without the codeblock formatting:

Inside macro1, or in a “wrapper” macro that calls macro1, use a conditional block similar to this:

{%- if execute -%}

{%- set result = run_query('show streams like \'' ~ stream_name ~ '\' in schema ' ~ stream_schema) -%}
{%- set stream_exists = (result is not none) and (result.rows | length &gt; 0) -%}
{%- if stream_exists -%}
    {%- set stream_info = dict(zip(result.columns | map(attribute='name'), result.rows[0])) -%}
    {%- if (stream_info['stale'] | string | lower) == 'true' -%}
        {{ macro1() }}
    {%- endif -%}
{%- endif -%}

{%- endif -%}

That example runs a query and then checks if that query returned any rows. If it did return any rows, it checks whether the value of the “stable” column is true in the first row. If both of those conditions are true, it does something (in this case calls macro1), otherwise it does nothing. You can adapt this to check whether your query returns “N”.

Note: @William originally posted this reply in Slack. It might not have transferred perfectly.

this worked out for me! many thanks for taking the time and explaining it.

for posterity this is my wrapper macro that calls the main one based on condition

{% macro wrappermacro() %}

{% if execute %}
    {% set column1 %}
        select column1 from table1 where table1.column2 = something
    {% endset %}
    {% set sendfile = run_query(column1) %}
    {% if (sendfile.columns[0].values()[0]) == 'Y' %}
        {{ mainmacro() }}
    {% endif %}
{% endif %}

{% endmacro %}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.