# How to dynamically skip a model run

**URL:** <https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968>\
**Category:** Help\
**Tags:** modeling\
**Created:** [April 22, 2023, 6:05pm UTC](https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968 "2023-04-22T18:05:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Process](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@Process](https://discourse.getdbt.com/u/Process)\
**Post date:** [April 22, 2023, 6:05pm UTC](https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968/1 "2023-04-22T18:05:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Surya](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/surya/32/3763_2.png) [@Surya](https://discourse.getdbt.com/u/Surya)\
**Post date:** [April 23, 2023, 2:24am UTC](https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968/2 "2023-04-23T02:24:20Z")

</div>

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 | dbt Developer Hub](https://docs.getdbt.com/reference/resource-configs/enabled)**
>
> \<Tabs

```auto
dbt_project.yml
models:
  model_folder:
      model_name
           enabled: "{{ enable_model() }}"

```

```auto
enable_model.sql

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

```

---

<div class="post-metadata">

**Author:** ![Process](https://avatars.discourse-cdn.com/v4/letter/p/cc9497/32.png) [@Process](https://discourse.getdbt.com/u/Process)\
**Post date:** [April 23, 2023, 9:15pm UTC](https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968/3 "2023-04-23T21:15:44Z")

</div>

I tried:

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

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

```

Seems once enable\_model() returns false, next time if I run `dbt run`, it always said

```auto
The selection criterion 't2' does not match any nodes

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![jeremyyeo](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/jeremyyeo/32/4121_2.png) [@jeremyyeo](https://discourse.getdbt.com/u/jeremyyeo)\
**Post date:** [April 24, 2023, 2:06am UTC](https://discourse.getdbt.com/t/how-to-dynamically-skip-a-model-run/7968/4 "2023-04-24T02:06:33Z")

</div>

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:

> **[How to - dynamically enable or disable a model from running based on query...](https://github.com/dbt-labs/docs.getdbt.com/discussions/1310)**
>
> From time to time, we may want to skip the run of a model based on some dynamic condition instead of statically setting the enabled flag: -- my\_table.sql {{ config(enabled = false) }} select 1 as u...
