# Use 2 variable in a math expression

**URL:** <https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041>\
**Category:** Kibana\
**Tags:** canvas\
**Created:** [February 11, 2021, 6:21pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041 "2021-02-11T18:21:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Clarke](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_clarke/32/87948_2.png) [@Mike\_Clarke](https://discuss.elastic.co/u/Mike_Clarke)\
**Post date:** [February 11, 2021, 6:21pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/1 "2021-02-11T18:21:53Z")

</div>

I am running 7.9.1 and am trying to perform math with 2 variables like below. Is this possible?

```auto
filters
| timefilter field="@timestamp" from="now-1d" to="now"
| var_set name="completed" value={escount index="index_1" query="thisField:test AND status:completed"}
| var_set name="sent" value={escount index="index_1" query="thisField:test AND status:sent"}
| math "completed / sent"

```

---

<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:** [February 11, 2021, 8:19pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/2 "2021-02-11T20:19:53Z")

</div>

Hi, welcome back! It looks like you are trying to do what we would call a "filter ratio".

Honestly, I think string concatenation is the easiest way to do this.

```auto
var_set name="errors" value=10
| var_set name="total" value=100
| math {string value={var "errors"} value=" / " value={var "total"}}

```

This is because the `math` function typically works using data tables, but we don't have a nice function to _create_ tables in Canvas.

---

<div class="post-metadata">

**Author:** ![Mike\_Clarke](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_clarke/32/87948_2.png) [@Mike\_Clarke](https://discuss.elastic.co/u/Mike_Clarke)\
**Post date:** [February 11, 2021, 9:02pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/3 "2021-02-11T21:02:29Z")

</div>

Thanks for the welcome back and the response. I am able to get part of the way there now.

```auto
var_set name="completed" value={escount index="index_1" query="thisField:test AND status:completed"}
| var_set name="sent" value={escount index="index_1" query="thisField:test AND status:sent"}
| math {string value={var "completed"} value=" / " value={var "sent"}}

```

However, if I want to restrict the result from the queries to the last 24 hours it won't work. How can I apply the timefilter that i specified in my original post?

---

<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:** [February 11, 2021, 9:45pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/4 "2021-02-11T21:45:15Z")

</div>

One thing that is not obvious is the way that parameters are passed into datasources in sub-expressions. For example, the `filters` is implicitly sent into `escount` here:

```auto
filters
| var_set name="completed" {/* implicit filters | */ escount}

```

But then when you have two `var_set` calls in a row, the same behavior causes an error:

```auto
var_set name="completed" value=0
| var_set name="sent" value={escount}

```

Putting this together, you need to create the full chain of datafetching at each subexpression level.

In this case I also used a time filter control, but you can do it either way:

```auto
timefilterControl compact=true column="@timestamp" filterGroup="myGroup"
| render

```

```auto
var_set name="completed" value={filters | timefilter filterGroup="myGroup" | escount index="kibana_sample_data_logs" query="bytes:[100 TO *]"}
| var_set name="sent" value={filters | timefilter filterGroup="myGroup" | escount index="kibana_sample_data_logs" query="*"}
| math {string {var "completed"} " / " {var "sent"}}  

```

---

<div class="post-metadata">

**Author:** ![Mike\_Clarke](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_clarke/32/87948_2.png) [@Mike\_Clarke](https://discuss.elastic.co/u/Mike_Clarke)\
**Post date:** [February 12, 2021, 12:17pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/5 "2021-02-12T12:17:09Z")

</div>

Wylie, thank you for the help and I now have things working. I'll figure out the expression language at some point. 🙂

---

<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:** [March 12, 2021, 12:17pm UTC](https://discuss.elastic.co/t/use-2-variable-in-a-math-expression/264041/6 "2021-03-12T12:17:28Z")

</div>

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