# Conditional DQL statements based on value of input variable

**URL:** <https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852>\
**Category:** DQL\
**Tags:** dql, dashboards\
**Created:** [September 20, 2024, 7:45am UTC](https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852 "2024-09-20T07:45:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeadScratcher](https://avatars.discourse-cdn.com/v4/letter/h/85f322/32.png) [@HeadScratcher](https://community.dynatrace.com/u/HeadScratcher)\
**Post date:** [September 20, 2024, 7:45am UTC](https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852/1 "2024-09-20T07:45:36Z")

</div>

I have some DQL that presents a table of data of web service calls.&nbsp; The data can be examined in different ways e.g. by consumer\_id or application\_name etc.&nbsp; &nbsp;To achieve this I either have to edit query each time or create multiple tables on a dashboard.

How can I avoid this?&nbsp; Can I create an input variable called Choice (holding different DQL text) and then use that to visualise the data from different views?

```markup
// Summarise the output... 
| summarize hit_count = count(),by:{ $Choice, response_out_http_code }

```

When I used the above&nbsp; the $Choice resolves to the string “consumer\_id” but the DQL is treating it as a string literal and not actually using it in the query.

What&nbsp; am I doing wrong?

---

<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:** [September 20, 2024, 9:59am UTC](https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852/2 "2024-09-20T09:59:06Z")

</div>

variables are always strings and are treated as literals in DQL query. They cannot be used as a field names, commands or function names. The workaround is possible using this trick:

```markup
data record(a=1, b="a"),
record(a=1, b="a"),
record(a=2, b="b"),
record(a=2, b="b"),
record(a=2, b="b"),
record(a=3, b="c"),
record(a=3, b="d"),
record(a=3, b="e"),
record(a=3, b="e"),
record(a=4, b="e")
| fieldsAdd split=coalesce(
  if($splitField=="a", a),
  if($splitField=="b", b)
)
| summarize cnt=count(), by: { split }

```

depending on variable value, proper field value will be kept under added filed of the same name independent from the choice. Now it can be directly used in a query (in by: {}).

 ![krzysztof_hoja_0-1726826271904.png](https://global.discourse-cdn.com/dynatrace/original/2X/3/327a409a40ea6d79e41879bb31cda44286d34dde.png)

 ![krzysztof_hoja_1-1726826302237.png](https://global.discourse-cdn.com/dynatrace/original/3X/f/0/f0c4d4ecdd2d6e3d4b599d71c74e1a4b10a26bc5.png)

Kris

---

<div class="post-metadata">

**Author:** ![HeadScratcher](https://avatars.discourse-cdn.com/v4/letter/h/85f322/32.png) [@HeadScratcher](https://community.dynatrace.com/u/HeadScratcher)\
**Post date:** [September 20, 2024, 1:20pm UTC](https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852/3 "2024-09-20T13:20:06Z")

</div>

I thought there must be a way.&nbsp; &nbsp; &nbsp;That is a pretty neat solution and actually helps tidy up my DQL a little bit too.

---

<div class="post-metadata">

**Author:** ![almog42](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@almog42](https://community.dynatrace.com/u/almog42)\
**Post date:** [May 4, 2025, 3:31pm UTC](https://community.dynatrace.com/t/conditional-dql-statements-based-on-value-of-input-variable/47852/4 "2025-05-04T15:31:30Z")

</div>

Heyy

now you can use variables not as a string literal by adding the&nbsp;:noquote to the variable name for example:

```markup
// for refrence writing the variable value
$Problam_Columns = "event.kind"

fetch dt.davis.problems 
| limit 1
| fields $Problam_Columns:noquote

// the "code" that will be used 
fetch dt.davis.problems 
| limit 1
| fields event.kind

```

also here is the link for the docs for that:

> **[Add a variable to a dashboard](https://docs.dynatrace.com/docs/analyze-explore-automate/dashboards-and-notebooks/dashboards-new/components/dashboard-component-variable#dollarvarname-noquote)**
>
> Add variables to your Dynatrace dashboards.
