# Graph a ratio between two counts

**URL:** <https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011>\
**Category:** Kibana\
**Created:** [September 10, 2015, 10:04am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011 "2015-09-10T10:04:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoann](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yoann](https://discuss.elastic.co/u/yoann)\
**Post date:** [September 10, 2015, 10:04am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/1 "2015-09-10T10:04:39Z")

</div>

Hello,

I would like to graph a ratio between two counts on the same index.

For example, if we take the books dataset, count the ratio of American book out of all the expensive books (more than 10$)  
count(books where country=US and price \> 10$) / count ( books where price \> 10$)

I want to be able to graph the evolution of this ratio aggregated per day of publication or any kind of bucketing (per country, per geohash, ...).

Do you have any idea on how to do that ? Considering that I can afford changing the data structure.

Thank you.

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [September 11, 2015, 10:10pm UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/2 "2015-09-11T22:10:02Z")

</div>

You mean [this issue](https://github.com/elastic/kibana/issues/2646)?

---

<div class="post-metadata">

**Author:** ![yoann](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yoann](https://discuss.elastic.co/u/yoann)\
**Post date:** [September 16, 2015, 8:23am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/3 "2015-09-16T08:23:43Z")

</div>

Yes indeed, it seems to be related to this limitation.  
Thank you.

---

<div class="post-metadata">

**Author:** ![tbragin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbragin/32/45166_2.png) [@tbragin](https://discuss.elastic.co/u/tbragin)\
**Post date:** [December 9, 2015, 5:43am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/4 "2015-12-09T05:43:14Z")

</div>

Btw, Timelion plugin for Kibana was just released, which makes doing ratios in Elasticsearch of two series trivial: [https://www.elastic.co/blog/timelion-timeline](https://www.elastic.co/blog/timelion-timeline)

---

<div class="post-metadata">

**Author:** ![yoann](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@yoann](https://discuss.elastic.co/u/yoann)\
**Post date:** [December 9, 2015, 9:12am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/5 "2015-12-09T09:12:02Z")

</div>

Thank you Tanya for pointing out Timelion.  
It seems very promising.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Colton](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jeremy\_Colton](https://discuss.elastic.co/u/Jeremy_Colton)\
**Post date:** [April 11, 2016, 3:39pm UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/6 "2016-04-11T15:39:48Z")

</div>

Hi Tanya,

Is TimeLion able to calculate a CTR , ie count(occurrence of text field A) / count(occurrence of text field B) \* 100?

The video [https://www.elastic.co/videos/time-series-analysis-using-timelion](https://www.elastic.co/videos/time-series-analysis-using-timelion) shows something the creator made but it doesn't describe the syntax of the query language and its vocabulary.

Many thanks.

---

<div class="post-metadata">

**Author:** ![bhatch](https://avatars.discourse-cdn.com/v4/letter/b/e95f7d/32.png) [@bhatch](https://discuss.elastic.co/u/bhatch)\
**Post date:** [April 19, 2016, 5:36pm UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/7 "2016-04-19T17:36:38Z")

</div>

Documentation seems very sparse for timelion. But you should be able to do that.

`.es(metric='cardinality:FieldA').divide(.es(metric='cardinality:FieldB')).multiply(100)`

---

<div class="post-metadata">

**Author:** ![Vinuja1](https://avatars.discourse-cdn.com/v4/letter/v/c57346/32.png) [@Vinuja1](https://discuss.elastic.co/u/Vinuja1)\
**Post date:** [June 21, 2017, 12:05pm UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/8 "2017-06-21T12:05:30Z")

</div>

With TimeLion we have the ratio over the Time, is it possible to have the ratio per term of a field?

---

<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:** [November 4, 2022, 4:05am UTC](https://discuss.elastic.co/t/graph-a-ratio-between-two-counts/29011/9 "2022-11-04T04:05:31Z")

</div>


