# Can 2 queries be used in a Metric or Markdown element, with if logic in the display?

**URL:** <https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [September 10, 2020, 8:42pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237 "2020-09-10T20:42:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 10, 2020, 8:42pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/1 "2020-09-10T20:42:27Z")

</div>

I have a Metric element on Kibana Canvas with the following expression syntax:

```auto
    filters group="default"

    | essql

    query="SELECT COUNT(*) as count FROM \"index1*\" "

    | markdown "

    {{#each rows}}{{count}}{{/each}}

    "

    font={font size=36 align="center"}

    | render containerStyle={containerStyle}

```

Would it be possible to add a 2nd query and use if logic in the markdown syntax, for example use another query

```auto
"SELECT COUNT(*) as count FROM \"index2*\" "

```

and only display the markdown if count from index2 is greater than 0?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 10, 2020, 8:57pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/2 "2020-09-10T20:57:15Z")

</div>

Sure:

```auto
kibana 
| filters
| var_set name="index1" value={essql query="SELECT COUNT(*) as count FROM kibana_sample_data_logs" | getCell "count"}
| var_set name="index2" value={essql query="SELECT COUNT(*) as count FROM kibana_sample_data_ecommerce" | getCell "count"}
| if {var "index1" | lt {var "index2"}} then={markdown "Index 1 greater {{#each rows}}{{count}}{{/each}}" | render} else={var "index2" | metric label="Index 2 count" | render}

```

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 11, 2020, 4:54pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/3 "2020-09-11T16:54:39Z")

</div>

When I try this in a metric element I get the error "cannot read property 'query' of null"

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 11, 2020, 4:57pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/4 "2020-09-11T16:57:21Z")

</div>

Are you using Kibana 7.7 or higher? What exactly is your expression?

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 11, 2020, 5:07pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/5 "2020-09-11T17:07:02Z")

</div>

7.4 currently. And I used that exact expression that you posted (except for my index).

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 11, 2020, 6:38pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/6 "2020-09-11T18:38:13Z")

</div>

The `var_set` and `var` feature is not available until 7.7, so that's probably why you are seeing this error. You might be able to copy+paste the queries into multiple places, but it will cause multiple requests to get executed.

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 11, 2020, 9:16pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/7 "2020-09-11T21:16:56Z")

</div>

By multiple places do you mean different elements? Are there examples anywhere of a metric or markdown element referencing 2 queries before 7.7?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 11, 2020, 9:36pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/8 "2020-09-11T21:36:33Z")

</div>

You can copy paste the SQL query into multiple places in the expression, instead of using variables. Or you can upgrade

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 11, 2020, 9:49pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/9 "2020-09-11T21:49:45Z")

</div>

Sorry for being so dense, but what exactly would that look like if you were to do it with your example above and no variable usage? I'm really struggling with expression syntax.

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 14, 2020, 2:46pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/10 "2020-09-14T14:46:13Z")

</div>

```auto
kibana 
| filters
| essql query="SELECT COUNT(*) as count FROM kibana_sample_data_logs"
| getCell "count"
| if {context | lt {essql query="SELECT COUNT(*) as count FROM kibana_sample_data_ecommerce" | getCell "count"}} then={markdown "Index 1 greater {{#each rows}}{{count}}{{/each}}" | render} else={essql query="SELECT COUNT(*) as count FROM kibana_sample_data_ecommerce" | getCell "count" | metric label="Index 2 count" | render}

```

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 21, 2020, 9:20pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/11 "2020-09-21T21:20:23Z")

</div>

Thanks Wylie. I've gained a better understanding of expression syntax. When I try that query or others like it where I'm putting the `getcell` result of a query inside sub expression like the one for `lt`above, I'm getting "Can not cast 'number' to any of 'filter'"

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 21, 2020, 9:32pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/12 "2020-09-21T21:32:13Z")

</div>

I think you need to change `lt {essql` to `lt {filters | essql` to fix that erro

---

<div class="post-metadata">

**Author:** ![devbot](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@devbot](https://discuss.elastic.co/u/devbot)\
**Post date:** [September 21, 2020, 9:42pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/13 "2020-09-21T21:42:36Z")

</div>

Worked like a charm. Thanks so much!!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 19, 2020, 9:42pm UTC](https://discuss.elastic.co/t/can-2-queries-be-used-in-a-metric-or-markdown-element-with-if-logic-in-the-display/248237/14 "2020-10-19T21:42:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
