# Dividing two values from same field and same index

**URL:** <https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643>\
**Category:** Kibana\
**Created:** [April 24, 2020, 12:11pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643 "2020-04-24T12:11:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saif3r](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@saif3r](https://discuss.elastic.co/u/saif3r)\
**Post date:** [April 24, 2020, 12:11pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643/1 "2020-04-24T12:11:09Z")

</div>

Hello,

My index has a field: **my\_field** that can have two possible values: **yes** and **no**. The following query:

```
{
  "size": 0,
  "aggs": {
    "count": {
      "terms": {
        "field": "my_field"
      }
    }
  }
}

```

returns:

```
{
  ...
  },
  "aggregations" : {
    "count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "yes",
          "doc_count" : 20000
        },
        {
          "key" : "no",
          "doc_count" : 54000
        }
      ]
    }
  }
}

```

I would like to create a visualization in Kibana (Metric or Data Table) that would return several values:  
"yes" / total which would be 20000 / 74000, so value = **0,2702**  
"no" / total which would be 54000 / 74000, so value = **0,7298**

How could I achieve that?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [April 24, 2020, 1:36pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643/2 "2020-04-24T13:36:30Z")

</div>

You can do this with the TSVB visualization type. Select the "metric" tab on top, then use the "filter ratio" aggregation type. It allows you to specify a query for the numerator and denominator - in your case the numerator would be `my_field:yes` and the denominator `*`. By adding a second series, you can do the same thing for the `no` value (the plus button in the top right of the series panel).

---

<div class="post-metadata">

**Author:** ![saif3r](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@saif3r](https://discuss.elastic.co/u/saif3r)\
**Post date:** [April 24, 2020, 1:42pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643/3 "2020-04-24T13:42:28Z")

</div>

Hello @flash1293,

That worked, thank you! Could you tell me how the same could be achieved using elasticsearch query? I'd like to get both those results in the same query call.  
And also, I'd like to have a possibility of dividing those two results, so something like **0,2702** / **0,7298**.  
Is that possible?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [April 24, 2020, 3:53pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643/4 "2020-04-24T15:53:09Z")

</div>

TSVB does that in a post-processing step, not as part of the query. But you should be able do it with the bucket script aggregation: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html)

---

<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:** [May 22, 2020, 3:53pm UTC](https://discuss.elastic.co/t/dividing-two-values-from-same-field-and-same-index/229643/5 "2020-05-22T15:53:16Z")

</div>

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