# Is an optional filter using variable possible?

**URL:** <https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539>\
**Category:** DQL\
**Tags:** dql, logs\
**Created:** [May 13, 2024, 5:05am UTC](https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539 "2024-05-13T05:05:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hfdku](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hfdku](https://community.dynatrace.com/u/hfdku)\
**Post date:** [May 13, 2024, 5:05am UTC](https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539/1 "2024-05-13T05:05:36Z")

</div>

Imagine I have logs coming from a Kubernetes cluster which I can retrieve successfully with this:

```markup
fetch logs
| filter k8s.namespace.name == "some-namespace-here"
| filter matchesPhrase(content, "someValue")

```

I can also run this app locally (or outside kubernetes), in which case, the DQL needs to be:

```markup
fetch logs
| filter matchesPhrase(content, "someValue")

```

Rather than create two tiles, is is possible to dynamically achieve this? So that a single tile works in both environments?

I imagined creating a dashboard level variable: “Kubernetes Mode: on / off” and toggling that would (somehow) reconfigure the DQL.

---

<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:** [May 13, 2024, 5:48am UTC](https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539/2 "2024-05-13T05:48:04Z")

</div>

Let’s define variable this way:

 ![krzysztof_hoja_0-1715578372074.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/d/9dac8f0daf2970df18b740c1fe3f30feee12626a.png)

Then the condition can look like this:

```markup
| filter ($KubernetesMode=="on" and k8s.namespace.name == "some-namespace-here")
  or ($KubernetesMode=="off" and isNull(k8s.namespace.name))

```

The reason I used isNull for “off” case is that without it the query would take all log lines: from Kubernetes as well as from local case.

You can also approach the problem in more dynamic way using variable based on DQL query defined this way:

 ![krzysztof_hoja_1-1715579095752.png](https://global.discourse-cdn.com/dynatrace/original/3X/2/f/2f75e3b843cd35afc43ab8cdbe3715f1cd191cdb.png)

```markup
fetch dt.entity.cloud_application_namespace
| fields entity.name
| dedup entity.name
| append [data record(entity.name="<local>")]

```

Then the filter reacting to any namespace selected would look like this:

```markup
| filter ($KubernetesMode!="<local>" and k8s.namespace.name == $KubernetesMode)
  or ($KubernetesMode=="<local>" and isNull(k8s.namespace.name))

```

Now you can switch between different namespaces and log lined without namespace set (local)

Kris

---

<div class="post-metadata">

**Author:** ![hfdku](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hfdku](https://community.dynatrace.com/u/hfdku)\
**Post date:** [May 13, 2024, 6:30am UTC](https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539/3 "2024-05-13T06:30:35Z")

</div>

Thanks Krzysztof, I’ll experiment with this tomorrow

---

<div class="post-metadata">

**Author:** ![hfdku](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hfdku](https://community.dynatrace.com/u/hfdku)\
**Post date:** [May 15, 2024, 5:37am UTC](https://community.dynatrace.com/t/is-an-optional-filter-using-variable-possible/45539/4 "2024-05-15T05:37:14Z")

</div>

![hfdku_0-1715751419484.png](https://global.discourse-cdn.com/dynatrace/original/2X/c/ce6757617453e5c5cb3be8b67f383ccb323b3828.png)

Works perfectly, thanks!
