# Apply variable values in filter with entitySelector

**URL:** <https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707>\
**Category:** DQL\
**Tags:** dql, entity-selector, dashboards\
**Created:** [July 14, 2023, 3:26pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707 "2023-07-14T15:26:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![elenaperez](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/elenaperez/32/38054_2.png) [@elenaperez](https://community.dynatrace.com/u/elenaperez)\
**Post date:** [July 14, 2023, 3:26pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/1 "2023-07-14T15:26:48Z")

</div>

I have the following query on a dashboard:

timeseries cpu=avg(dt.host.cpu.usage), filter:in(dt.entity.host,entitySelector(“type(host),tag(Environment:DEV)”)),by:dt.entity.host  
| sort arrayAvg(cpu), direction:“descending”  
| limit 20

And I have a variable called Environment with the values DEV, SANDBOX and PROD. I wold like to filter the tile based on the variable but substituting DEV by $Environment doesn’t work.

Any idea on how could I create the query?

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [July 17, 2023, 7:39am UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/2 "2023-07-17T07:39:52Z")

</div>

Hi&nbsp;[@elenaperez](https://community.dynatrace.com/user/58054)

As you might have noticed from the info message in your environment, the entitySelector will be soon depreciated for usage in DQL. Instead you can use just plain DQL. I guess this example should work for you

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{tags}
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| filter toString(tag) == "Environment" and toString(value) == $environment
| sort arrayAvg(cpu), direction:"descending"
| limit 20

```

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![elenaperez](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/elenaperez/32/38054_2.png) [@elenaperez](https://community.dynatrace.com/u/elenaperez)\
**Post date:** [July 19, 2023, 10:36am UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/3 "2023-07-19T10:36:33Z")

</div>

What if I also have another tag called Business\_Application that works the same way as Environment and I want to filter on the 2 values? I tried this but it doesn’t work (doesn’t show any data but it should)

| filter (toString(tag) == “Business Application” and in(toString(value), array($Business\_Application))) and (toString(tag) == “Environment” and in(toString(value), array($Environment)))

---

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [July 19, 2023, 11:12am UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/4 "2023-07-19T11:12:48Z")

</div>

because for every tag you have now one record. This is caused by the [expand command](https://www.dynatrace.com/support/help/shortlink/dql-commands#expand).

So you need to summarize again the data end extract the tags on which you want to filter. Something like this

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{tags}
| filter isNotNull(tags)
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| fieldsAdd businessApp = if(tag == "Business Application", value)
| fieldsAdd environment = if(tag == "Environment", value)
| summarize {cpu = takeAny(cpu), 
  businessApp=arrayMin(collectArray(businessApp)),
  environment = arrayMin(collectArray(environment))}
  , by:{dt.entity.host}
| filter in(businessApp, array($Business_Application)) and 
  in(environment, arary($Environment))

```

Best,  
Sini

---

<div class="post-metadata">

**Author:** ![dmezynski](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dmezynski](https://community.dynatrace.com/u/dmezynski)\
**Post date:** [March 12, 2025, 3:21pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/5 "2025-03-12T15:21:53Z")

</div>

I’m doing this same thing and am having issues with displaying as a time series on a dashboard.

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{tags}
| filter isNotNull(tags)
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| fieldsAdd client = if(tag == "client", value)
| fieldsAdd environment_type = if(tag == "environment_type", value)
| fieldsAdd function = if(tag == "function", value)
| summarize {cpu = takeAny(cpu), 
  client=arrayMin(collectArray(client)),
  environment_type = arrayMin(collectArray(environment_type)),
  function = arrayMin(collectArray(function))}
  , by:{dt.entity.host}
| filter in(client, array($Client)) and 
  in(environment_type, array($Environment))

```

The data set comes out like this (with the tag values not shown):

 ![dmezynski_0-1741792797748.png](https://global.discourse-cdn.com/dynatrace/original/2X/8/8dba01fa9a9c0b02a0a469402c95777750bd2eac.png)

The dashboard is telling my the data is not suitable for a graph:

 ![dmezynski_1-1741792898506.png](https://global.discourse-cdn.com/dynatrace/original/2X/e/ec6acf22837204f598db689ce208e5399e04c10f.png)

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![elenaperez](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/elenaperez/32/38054_2.png) [@elenaperez](https://community.dynatrace.com/u/elenaperez)\
**Post date:** [March 12, 2025, 3:51pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/6 "2025-03-12T15:51:21Z")

</div>

Try adding timeframe and interval in the by. You are missing that information when you do the summarize:

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{tags}
| filter isNotNull(tags)
| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| fieldsAdd client = if(tag == "client", value)
| fieldsAdd environment_type = if(tag == "environment_type", value)
| fieldsAdd function = if(tag == "function", value)
| summarize {cpu = takeAny(cpu), 
  client=arrayMin(collectArray(client)),
  environment_type = arrayMin(collectArray(environment_type)),
  function = arrayMin(collectArray(function))}
  , by:{dt.entity.host, timeframe, interval}
| filter in(client, array($Client)) and 
  in(environment_type, array($Environment))

```

---

<div class="post-metadata">

**Author:** ![dmezynski](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dmezynski](https://community.dynatrace.com/u/dmezynski)\
**Post date:** [March 12, 2025, 8:09pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/7 "2025-03-12T20:09:26Z")

</div>

Thank you very much!!! I am just getting into gen3 and DQL. Do you know what modification would be needed to report by entity name instead of the id?

---

<div class="post-metadata">

**Author:** ![dmezynski](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dmezynski](https://community.dynatrace.com/u/dmezynski)\
**Post date:** [March 12, 2025, 8:28pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/8 "2025-03-12T20:28:56Z")

</div>

Got it.

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
| lookup [fetch dt.entity.host], sourceField:dt.entity.host, lookupField:id, fields:{tags, entity.name}
| filter isNotNull(tags)| expand tags
| parse tags, """((LD:tag (!<<'\\' ':') LD:value)|LD:tag)"""
| fieldsAdd tag = replaceString(tag,"""\:""", ":"), value = replaceString(value, """\:""", ":")
| fieldsAdd client = if(tag == "client", value)
| fieldsAdd environment_type = if(tag == "environment_type", value)
| fieldsAdd function = if(tag == "server_function", value)
| summarize {cpu = takeAny(cpu), 
  client=arrayMin(collectArray(client)),
  environment_type = arrayMin(collectArray(environment_type)),
  function = arrayMin(collectArray(function))}
  , by:{entity.name, timeframe, interval}
| filter in(client, array($Client)) and 
  in(environment_type, array($Environment))
| fieldsRemove client
|fieldsREmove environment_type
|fieldsRemove function

```

---

<div class="post-metadata">

**Author:** ![mgome](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mgome](https://community.dynatrace.com/u/mgome)\
**Post date:** [March 27, 2025, 6:37pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/9 "2025-03-27T18:37:45Z")

</div>

I want to revisit the original question because filtering in the timeseries query is faster than filtering after the query. In order to use a variable as part of an entity selector filter in a timeseries query, you need to use classicEntitySelector, concat and toString.

```python
timeseries cpu=avg(dt.host.cpu.usage),
  filter:in(dt.entity.host,
            classicEntitySelector(concat("type(host),tag(Environment:",
                                          toString($Environment),
                                          ")")
                                 )
           ),
  by:{dt.entity.host}
| sort arrayAvg(cpu) desc
| limit 20

```

Breaking down the filter, there’s an embedded concat in the classicEntitySelector call with three arguments. &nbsp;The first argument is the beginning of the entity selector in quotes. &nbsp;The second argument is the variable converted to a string. &nbsp;The last argument is a quoted close&nbsp;parentheses to end the entity selector.

Anything you can to do reduce the amount of data returned in the timeseries query will make tiles in dashboards load faster.

---

<div class="post-metadata">

**Author:** ![elenaperez](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/elenaperez/32/38054_2.png) [@elenaperez](https://community.dynatrace.com/u/elenaperez)\
**Post date:** [March 28, 2025, 8:12am UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/10 "2025-03-28T08:12:53Z")

</div>

That makes sense! Thank you as well

---

<div class="post-metadata">

**Author:** ![dmezynski](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@dmezynski](https://community.dynatrace.com/u/dmezynski)\
**Post date:** [March 28, 2025, 1:27pm UTC](https://community.dynatrace.com/t/apply-variable-values-in-filter-with-entityselector/40707/11 "2025-03-28T13:27:39Z")

</div>

We flipped our methodology on this recently.  
We created a hidden variable ($app\_servers) to create a list of hosts:  
Note: that hidden variable needs to be multiselect and default to \*

```markup
fetch dt.entity.host
|fieldsAdd tags
|filter in(concat("client:",$Client),tags) and in(concat("environment_type:",$Environment),tags)
|fields id

```

Then our tiles became much simpler by providing a host list.

```markup
timeseries cpu=avg(dt.host.cpu.usage), by:{dt.entity.host}
|filter in({$app_servers}, dt.entity.host)
|fieldsadd hostname=entityName(dt.entity.host)

```
