# Variable based on Tags in Segments

**URL:** <https://community.dynatrace.com/t/variable-based-on-tags-in-segments/51360>\
**Category:** DQL\
**Tags:** dql, segments\
**Created:** [April 24, 2025, 7:43pm UTC](https://community.dynatrace.com/t/variable-based-on-tags-in-segments/51360 "2025-04-24T19:43:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![StephenHughes](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@StephenHughes](https://community.dynatrace.com/u/StephenHughes)\
**Post date:** [April 24, 2025, 7:43pm UTC](https://community.dynatrace.com/t/variable-based-on-tags-in-segments/51360/1 "2025-04-24T19:43:54Z")

</div>

I would like to create a variable to be used in a segment that lets me filter based on tags. The idea is the tag is [AWS]ID and the values are X, Y, Z, I want to be able to filter by X,Y,Z and get their related hosts. I attempted to use the solution in&nbsp;[https://community.dynatrace.com/t5/DQL/Create-a-variable-with-the-values-of-a-specific-tag/m-p/218061/thread-id/135](https://community.dynatrace.com/post/218061)&nbsp;but it’s repeating the [AWS]IDs so it looks like we have multiples of one ID rather than individual IDs.

My original DQL was:

```javascript
fetch dt.entity.host
| fieldsAdd tags
| expand tags
| fields tags

```

And the solution from the above link that I tried was:

```markup
fetch dt.entity.host
| expand tags
| summarize tags = collectDistinct(tags)
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fields tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| filter toString(tag) == "[AWS]ID"
| fields value

```

---

<div class="post-metadata">

**Author:** ![marco\_irmer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marco_irmer/32/3038_2.png) [@marco\_irmer](https://community.dynatrace.com/u/marco_irmer)\
**Post date:** [April 24, 2025, 7:50pm UTC](https://community.dynatrace.com/t/variable-based-on-tags-in-segments/51360/2 "2025-04-24T19:50:03Z")

</div>

Adding the dedup command at the end of your query might help.

```javascript
fetch dt.entity.host
| expand tags
| summarize tags = collectDistinct(tags)
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fields tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| filter toString(tag) == "[AWS]ID"
| fields value
| dedup value

```

---

<div class="post-metadata">

**Author:** ![StephenHughes](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@StephenHughes](https://community.dynatrace.com/u/StephenHughes)\
**Post date:** [April 24, 2025, 7:59pm UTC](https://community.dynatrace.com/t/variable-based-on-tags-in-segments/51360/3 "2025-04-24T19:59:48Z")

</div>

Thank you, that helped!
