# Esql - Applying if condition to output of two esql queries

**URL:** <https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [November 4, 2019, 10:38am UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371 "2019-11-04T10:38:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![atamarnath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atamarnath/32/57083_2.png) [@atamarnath](https://discuss.elastic.co/u/atamarnath)\
**Post date:** [November 4, 2019, 10:38am UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/1 "2019-11-04T10:38:09Z")

</div>

Hi,

I have the below code in expression edit that seems to be working fine.

filters

| essql "SELECT RAG.keyword AS RAG FROM "logstash-bam\*" where sourcetype.keyword = 'current\_status\_A' AND Step = 'A' AND time\_stamp \> TODAY() order by time\_stamp DESC limit 1"

| shape "circle" fill={if {rowCount | lt 1} then="Green" else={getCell column=RAG}} border="rgba(255,255,255,0)" borderWidth=0 maintainAspect=true

| render containerStyle={containerStyle} css=".canvasRenderEl { }"

Now I have another query that need to be added to the above code

essql "SELECT RAG.keyword AS RAG FROM "logstash-bam\*" where sourcetype.keyword = 'current\_status\_B' AND Step = 'A' AND time\_stamp \> TODAY() order by time\_stamp DESC limit 1"

Set the color of shape to GREEN if both the output of both the sqls return a RAG of GREEN. Even if one the query returns RED the color of the shape need to be set to RED

---

<div class="post-metadata">

**Author:** ![tims](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tims/32/46750_2.png) [@tims](https://discuss.elastic.co/u/tims)\
**Post date:** [November 14, 2019, 10:10pm UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/2 "2019-11-14T22:10:21Z")

</div>

Hi @atamarnath, the problem you are going to encounter is that you can't pass 2 query contexts into an if statement. Is there a way to make 1 query return the count of sourcetype.keyword where it equals 'current\_status\_A' or 'current\_status\_B'?

---

<div class="post-metadata">

**Author:** ![atamarnath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atamarnath/32/57083_2.png) [@atamarnath](https://discuss.elastic.co/u/atamarnath)\
**Post date:** [November 14, 2019, 10:48pm UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/3 "2019-11-14T22:48:24Z")

</div>

Thank you for your response. Is there a way to store the result of the query to a temporary variable and then use it in the if statement.

---

<div class="post-metadata">

**Author:** ![tims](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tims/32/46750_2.png) [@tims](https://discuss.elastic.co/u/tims)\
**Post date:** [November 15, 2019, 5:56pm UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/4 "2019-11-15T17:56:57Z")

</div>

There is no way to create a variable in the expression language but you could maybe store a value to the datatable using [mapColumn](https://www.elastic.co/guide/en/kibana/current/canvas-function-reference.html#mapColumn_fn) and then reference it using [getCell](https://www.elastic.co/guide/en/kibana/current/canvas-function-reference.html#getCell_fn).

---

<div class="post-metadata">

**Author:** ![atamarnath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atamarnath/32/57083_2.png) [@atamarnath](https://discuss.elastic.co/u/atamarnath)\
**Post date:** [November 20, 2019, 3:43pm UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/5 "2019-11-20T15:43:49Z")

</div>

Thanks Tim. We ended up converting the RAG to a number instead of string and sorting the rows added in last 1 minute based RAG and pick up the top record. We then used if condition to identify the color. Basically RAG was made to store 1, 2 or 3 where 1 = RED, 2 = Orange (Amber is not supported by Canvas), 3 = GREEN.

---

<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:** [December 18, 2019, 3:43pm UTC](https://discuss.elastic.co/t/esql-applying-if-condition-to-output-of-two-esql-queries/206371/6 "2019-12-18T15:43:52Z")

</div>

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