How to dynamically skip a model run

I have many models and materialization=Table. Before the model runs, I want to check the source and the model table. If they have the same max(some_col), then I want to skip the model run and keep the model table as is. In fact, in the airflow DAG generated, I expect the corresponding steps can be skipped. Is there a way to do this? Thanks.

U can use enabled flag to enable aor disable a model.
It accepts true/flase but try calling a macro and return true/false based on ur logic.

           enabled: "{{ enable_model() }}"

{% macro enable_model()%}
return true
{%end macro %}

I tried:

{% macro enable_model() -%}
    {%- set bookmark = range(1, 10) | random -%}
    {{ return(bookmark>5) }}
{%- endmacro %}

-- model t2
{{ config(
  materialized = "table",
) }}
select random(10000) as a

Seems once enable_model() returns false, next time if I run dbt run, it always said

The selection criterion 't2' does not match any nodes

Am I missing something?

It’s not possible to do the following:

  1. Retrieve some value from a table with dbt.
  2. Then after (1) - set a model’s config to be enabled or not.

Because what dbt does is- it resolves a model’s enabled or not config BEFORE it even starts issuing queries to the database - thus it cannot do the reverse of it.

There is a great deal of exploration on what’s currently possible wrt “dynamically enable/disable models based on some result returned by a query” here: