# Dividing document counts from 2 different indices

**URL:** <https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151>\
**Category:** Kibana\
**Created:** [February 5, 2025, 8:27pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151 "2025-02-05T20:27:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoD](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@HugoD](https://discuss.elastic.co/u/HugoD)\
**Post date:** [February 5, 2025, 8:27pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/1 "2025-02-05T20:27:51Z")

</div>

Hi. I've had an ask from one of our users. We are displaying multiple metrics coming form multiple indices in the same dashboard (NOT my design, by the way).

Now, they want to have a ratio calculated for the selected time period. For example, if selecting January, they want to have the number of Service Order Errors (SOEs) that happened in January, divided by the number of Service Orders (SOs) for the same period. Both indices have the same @timestamp field, and a few filters in common, so in theory this should work okay.

But I have no idea of doing this. I have checked in TSVB (Metric + math) and in Lens, but can't figure how to use data coming from 2 different indices in a ratio. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [February 5, 2025, 9:48pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/2 "2025-02-05T21:48:03Z")

</div>

You may find a new friend in Timelion

> **[Legacy editors | Kibana Guide \[8.17\] | Elastic](https://www.elastic.co/guide/en/kibana/current/legacy-editors.html#timelion)**

---

<div class="post-metadata">

**Author:** ![HugoD](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@HugoD](https://discuss.elastic.co/u/HugoD)\
**Post date:** [February 6, 2025, 4:02pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/3 "2025-02-06T16:02:56Z")

</div>

Can you elaborate? I have not seen a use case for Metrics in Timelion, only time series. Thanks.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [February 6, 2025, 4:35pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/4 "2025-02-06T16:35:26Z")

</div>

Please share some sample documents from the indices to which you refer.

> [@HugoD](#):
>
> Can you elaborate? I have not seen a use case for Metrics in Timelion, only time series. Thanks.

Well, if there is no timestamp or similar field I don't think Timeline can help. But you said both indices have the same @timestamp, so you do have a time series of sorts (I'm not getting into a semantic argument) ?

If you do have timestamps in both indices, then you can plot a graph of some metric (e.g. a count, a sum, ..) derived from data from index A / some other metric (e.g. a count or sum of something different) derived from the data index B.

Set the time span to what you want, and see daily/weekly/monthly/whatever.

Maybe I've misunderstood what your documents in the various indices contain, accept my apology if so.

Example, I plot ratio between number of docs (simplest example) in 2 different indices, per day, over last 30 days - should be 1.0, but ... life is not perfect.

Thread is titled "Dividing document counts from 2 different indices", so ...

```auto
.es(index=my-data1*,metric=count,timefield=timestamp).divide(.es(index=my-data2*,metric=count,timefield=timestamp)).label("Ratio of doc counts in 2 indices").yaxis(min=0.0,max=2)

```

which gives

 ![Screenshot 2025-02-06 at 17.48.44](https://us1.discourse-cdn.com/elastic/original/3X/4/6/468d930a66e340e20d0c16938c0217001fb573a4.jpeg)

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [February 12, 2025, 1:26pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/5 "2025-02-12T13:26:28Z")

</div>

@HugoD

You did not answer here. Did you find any solution to your issue?

---

<div class="post-metadata">

**Author:** ![HugoD](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@HugoD](https://discuss.elastic.co/u/HugoD)\
**Post date:** [February 24, 2025, 3:05pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/6 "2025-02-24T15:05:13Z")

</div>

Hi. Thanks for the explanations. I really need a calculated ratio (e.g. numerical metric, for example "61%") and not a time series. One of my team members suggested creating an Alias on top of both series, since they have a compatible timestamp field and primary key field. This will allow us to create a Metric using filtered data from the same Alias for numerator and denominator. We will see how it goes.

---

<div class="post-metadata">

**Author:** ![RainTown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raintown/32/140206_2.png) [@RainTown](https://discuss.elastic.co/u/RainTown)\
**Post date:** [February 24, 2025, 3:18pm UTC](https://discuss.elastic.co/t/dividing-document-counts-from-2-different-indices/374151/7 "2025-02-24T15:18:47Z")

</div>

That's a very clever idea, kudos to your colleague, should indeed work well, and efficiently, in the scenario you describe.

FYI I answered a few days ago with a different join-something-across-2-indices question using ES|QL, and an enrich index, with some modification the ratio could likely be calculated too, as well as various other stats.
