# tags in dbt to be applied on databricks tables/views etc

**URL:** <https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485>\
**Category:** Help\
**Created:** [October 8, 2024, 9:07am UTC](https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485 "2024-10-08T09:07:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThuanNg](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/thuanng/32/4558_2.png) [@ThuanNg](https://discourse.getdbt.com/u/ThuanNg)\
**Post date:** [October 8, 2024, 9:07am UTC](https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485/1 "2024-10-08T09:07:22Z")

</div>

We have

Tags in our models and want this to reflected in Databricks Tags for tables /views.  
Anyone manage to accomplish this?

---

<div class="post-metadata">

**Author:** ![JohnBrandborg](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JohnBrandborg](https://discourse.getdbt.com/u/JohnBrandborg)\
**Post date:** [January 27, 2025, 2:46pm UTC](https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485/2 "2025-01-27T14:46:52Z")

</div>

Hey @ThuanNg

I came up with a macro to it, that you can use a Post Hook. You can only apply either groups of `keys`, or `key=values` to Databricks as tags, (they can’t be mixed), so I made it that by default only `key=values` are applied. This means you can keep the normal key like tags for dbt use.

If you don’t want the KW tags, then set `key_values` to false, and `key` only tags will be applied.

**macros/apply\_table\_tags.sql**

```auto
{% macro apply_table_tags(key_values=true) %}
{%- set tags_list = [] -%}
{%- for tag in model.config.tags -%}
    {%- if key_values is true and '=' in tag -%}
        {% do tags_list.append(tag) %}
    {%- elif key_values is false and '=' not in tag -%}
        {% do tags_list.append(tag) %}
    {%- endif -%}
{%- endfor -%}

{%- set tags_string -%}
   {{ '"' + tags_list | join('","') | replace('=', '"="') + '"' }}
{%- endset -%}

{%- set comment_query -%}
    alter table {{ this | lower }} set tags ({{ tags_string }})
{%- endset -%}

{%- if tags_list | length > 0 -%}
    {{ return(comment_query) }}
{%- endif -%}

{% endmacro %}

```

**dbt\_project.yaml**

```yaml
model:
  <resource-path>:
    +post-hook:
      - "{{ apply_table_tags(key_values=true) }}"

```

---

<div class="post-metadata">

**Author:** ![JohnBrandborg](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@JohnBrandborg](https://discourse.getdbt.com/u/JohnBrandborg)\
**Post date:** [January 28, 2025, 10:05am UTC](https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485/3 "2025-01-28T10:05:47Z")

</div>

Today I also came up with a small variation of the Post-Hook macro, where the [Meta](https://docs.getdbt.com/reference/resource-configs/meta) config is used instead for Tags in Databricks because it’s inherently key value. This will allow the tags in dbt to be used for normal use like query selection.

**macros/apply\_table\_meta.sql**

```auto
{% macro apply_table_meta() %}
{%- set tags_list = [] -%}
{%- for item in model.config.meta.items() -%}
    {% do tags_list.append(item | join('"="')) %}
{%- endfor -%}

{%- set tags_string -%}
   {{ '"' + tags_list | join('","') + '"' }}
{%- endset -%}

{%- set comment_query -%}
    alter table {{ this | lower }} set tags ({{ tags_string }})
{%- endset -%}

{%- if tags_list | length > 0 -%}
    {{ return(comment_query) }}
{%- endif -%}

{% endmacro %}

```

**dbt\_project.yaml**

```auto
model:
  <resource-path>:
    +post-hook:
      - "{{ apply_table_meta() }}"
```

---

<div class="post-metadata">

**Author:** ![ThuanNg](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/thuanng/32/4558_2.png) [@ThuanNg](https://discourse.getdbt.com/u/ThuanNg)\
**Post date:** [February 14, 2025, 7:24pm UTC](https://discourse.getdbt.com/t/tags-in-dbt-to-be-applied-on-databricks-tables-views-etc/16485/4 "2025-02-14T19:24:23Z")

</div>

Great. do you have a sample of of a model showcasing the tags format?
