Is there a way to set environment variable from macro so that I can access that in dbt_project.yml

The problem I’m having

I need to set a dynamic named based on a query result which is returned by a macro. It seems I cannot call macro and use its output because of the execution sequence. But I can use the start-up option to execute a macro. If I can set an env variable, then I can access that in the dbt_project.yml.

What I’ve already tried

I have tried to call the macros in various way. Even if its called, it does not return anything.

Any help would be appreciated. If its not possible, please let me know that as well. If you any better way that this, throw at me. Thanks !

Your approach seemed correct to begin with - obtaining the result of the query within the macro-. You might want to consider wrapping it in execute .

This will only help in knowing if any SQL statement is run and run an “if-else” condition based on that. I still need to access that value in dbt_project.yml.

I think that the suggestion is something like:

# dbt_project.yaml
...
vars:
  foo: {{ bar() }}
...

And you can define your query in the bar macro with the execute variable