# Test if a dashboard variable is set before using it

**URL:** <https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335>\
**Category:** DQL\
**Tags:** log-analytics, dql, dashboards\
**Created:** [April 29, 2024, 7:12am UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335 "2024-04-29T07:12:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GerardJ](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/gerardj/32/154_2.png) [@GerardJ](https://community.dynatrace.com/u/GerardJ)\
**Post date:** [April 29, 2024, 7:12am UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/1 "2024-04-29T07:12:46Z")

</div>

Hello

I’m using variables on dashboard to filter in DQL.  
I would like the filter command to be applied only if the variable has a selected value : if no value is selected for the variable, the variable isn’t used to filter the request as if the filter command is skipped.

I didn’t find a way to do it, like testing the value of a variable before using it.

Is it something feasible ?

Regards

---

<div class="post-metadata">

**Author:** ![PierreGutierrez](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pierregutierrez/32/5934_2.png) [@PierreGutierrez](https://community.dynatrace.com/u/PierreGutierrez)\
**Post date:** [June 9, 2024, 1:48am UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/2 "2024-06-09T01:48:42Z")

</div>

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

If you are using Dashboard Gen 3, I recommend using Javascript&colon;  
1.- Capture filter information  
2.- Create a Method that evaluates whether the filter variables have data (depending on it, apply one logic or another).  
3.- Start working with that new final value

Something like this :

```javascript
let array_tags = $GeneralTags
addTags(query_requestcountfilter, array_tags, array_environmentVariablesTags );

function addTags(query, array_tags, array_environmentVariablesTags){
  var new_query_1 = ""
  var new_query_2 = ""
  var tags = "tag(~\""+ array_tags[0]+"~\")";
  let i = 1;

  while (i < array_tags.length ) {
    if (array_tags[i]==null) break;
    tags = tags + ",tag(~\""+array_tags[i]+"~\")";
    i++;
  }

  if ( array_tags.length > 0){
    new_query_1 = query.replace("CHANGE_TAG_GENERAL",tags) 
  }else{
    new_query_1 = query.replace("CHANGE_TAG_GENERAL", "")
  }

  tags = "not(tag(~\""+array_environmentVariablesTags[0]+"~\"))";

  i = 1;
  while (i < array_environmentVariablesTags.length ) {
    if (array_environmentVariablesTags[i]==null) break;
    tags = tags + ",not(tag(~\""+array_environmentVariablesTags[i]+"~\"))";
    i++;
  }
  if ( array_environmentVariablesTags.length > 0){
    new_query_2 = new_query_1.replace("CHANGE_TAG",tags)   
  }else{
    new_query_2 = new_query_1.replace("CHANGE_TAG","")
  }  

  return new_query_2;
}

```

Of course, you can still optimize this code  
I hope it’s helpful 💪

---

<div class="post-metadata">

**Author:** ![mark\_bley](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mark_bley/32/20954_2.png) [@mark\_bley](https://community.dynatrace.com/u/mark_bley)\
**Post date:** [June 9, 2024, 10:00am UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/3 "2024-06-09T10:00:16Z")

</div>

You can check for variable values with a code snippet, if the value you expect is set then continue with the DQL

```markup
export default async function () {
  if ($important_filter != "expected value") {
    return null
  }
  // execute DQL...
  ...
  return data;
}

```

> **[Grail DQL query API | Dynatrace Developer](https://developer.dynatrace.com/develop/sdks/client-query/)**
>
> Exposes an API to fetch records stored in Grail.

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [June 10, 2024, 6:41pm UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/4 "2024-06-10T18:41:23Z")

</div>

Maybe this is not exactly what you asked for for but I hope it is close and usable. Let’s define variable as a list of values you want (in my example I used 2 popular keys of log lines) to filter by and one additional special value. In my case it is “\*”.

 ![krzysztof_hoja_0-1718044380064.png](https://global.discourse-cdn.com/dynatrace/original/2X/5/50e09dfba727c7c3741220720caba97a037c27a2.png)

If the condition is the query is build this way:

```markup
fetch logs
| filter $Traffic=="*" or contains(content,$Traffic)
| limit 10

```

second part of the filter is simply ignored when value of $Traffic variable is \*

btw: I do not like “Define a value in the referenced variable” message displayed in case when nothing is selected. Or dashboard should not allow this state or variable should have w value (i.e. null)

Kris

---

<div class="post-metadata">

**Author:** ![mark\_bley](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mark_bley/32/20954_2.png) [@mark\_bley](https://community.dynatrace.com/u/mark_bley)\
**Post date:** [June 10, 2024, 6:51pm UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/5 "2024-06-10T18:51:42Z")

</div>

That is going to be an issue if nothing is found you will potentially still scan a couple of GB.

If you go this way I would at least suggest reading through this guide in order to reduce the amount of data scanned if nothing is found: [DQL best practices — Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/dql-best-practices#dql-best-practices)

 ![mark_bley_1-1718045449627.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/f/6f49334680d134ffc3a4f566ffbc1e8d851e3247.png)

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [June 10, 2024, 7:15pm UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/6 "2024-06-10T19:15:07Z")

</div>

Thanks for pointing this out. Presence of “always false conditions” in theory should not have any influence on query execution as they can be eliminated up front. This is place where we can optimize 🙂

---

<div class="post-metadata">

**Author:** ![GerardJ](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/gerardj/32/154_2.png) [@GerardJ](https://community.dynatrace.com/u/GerardJ)\
**Post date:** [June 21, 2024, 12:33pm UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/7 "2024-06-21T12:33:31Z")

</div>

Hi All

thank you for all your suggestions.  
As our aim is often to provide end-users with dashboards that they can evolve or adapt, using javascript tiles is never our first option so&nbsp;I’ll probably go with the one suggested by&nbsp;[@krzysztof\_hoja](https://community.dynatrace.com/user/25373)&nbsp;  
In fact what&nbsp;I’d like is to be able to do an if before a filter in DQL to only filter if the variable is set like this :

```markup
fetch logs
| if (isNotNull($myvariable), filter matchesValue(examplefield,$myvariable))

```

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [June 21, 2024, 8:34pm UTC](https://community.dynatrace.com/t/test-if-a-dashboard-variable-is-set-before-using-it/45335/8 "2024-06-21T20:34:54Z")

</div>

Unfortunately when variable is not selected it does not get value “null” and such state causes query to fail (probably this is something which can be improved). That’s why I proposed special value for “all records”.

If my previous example looks confusing and not intuitive, maybe this is clearer:

```markup
| filter if( $Traffic=="*", true, else: contains(content,$Traffic) )

```

but internally does the same.

If variable value is “\*”, then condition is always true, so effectively filter disappears.
