# Need to calculate a single value event rate and visualize it

**URL:** <https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076>\
**Category:** Kibana\
**Created:** [November 26, 2024, 10:27am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076 "2024-11-26T10:27:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [November 26, 2024, 10:27am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/1 "2024-11-26T10:27:41Z")

</div>

I need to calculate the event rate and show it as a single value (metric).

E.g. I've the following documents:

| Column 1 | Column 2 |
| --- | --- |
| Timestamp | Data |
| 1.2.2024 15:30 | Event 1 |
| 1.2.2024 15:45 | Event 2 |
| 2.2.2024 10:30 | Event 3 |
| 2.2.2024 14:30 | Event 4 |
| 2.2.2024 15:00 | Event 5 |
| 3.2.2024 10:30 | Event 6 |
| ... | |

If the user selects e.g. 2.2.2024 10:00-2.2.2024 15:00  
 ![Screenshot 2024-11-26 at 11.25.52](https://us1.discourse-cdn.com/elastic/original/3X/7/f/7ff0913cbb0b92f5acdc77abbffc960780c3e9de.png)  
the calculation should be:  
`3/(2.2.2024 15:00-2.2.2024 10:30)`

3=number of events in the selected timespan  
(2.2.2024 15:00-2.2.2024 10:30)=timespan in seconds

Is this possible to add this as a visualization in Kibana?

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [November 28, 2024, 9:57am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/2 "2024-11-28T09:57:42Z")

</div>

Currently the `rate` function considers only the passed time range without checking the exact timestamp of the event.  
So in your case the final formula would be:

```auto
3/(2.2.2024 15:00-2.2.2024 10:00)

```

In your case it looks like you're instead interested in the exact timing of the first event (`10:30`) but will like to stretch the final timespan until the end of the provided time range rather than the latest event (`15:00` vs `14:30`): did I get it correctly?

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [November 28, 2024, 11:30am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/3 "2024-11-28T11:30:17Z")

</div>

My example was a little bit misleading.

It would be best if I could get the timespan between the first and last event within the selected time range.

So if I select as time range: 2.2. 10:00 - 2.2. 15:15 the calculation should be done like this:  
3/(2.2. 15:00 - 2.2. 10:30)

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [November 28, 2024, 12:02pm UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/4 "2024-11-28T12:02:13Z")

</div>

Ok, I see what you mean.  
That is not possible yet. The `rate` function in ES currently has not that kind of precision to calculate the timespan: it will take into account the "date bucket" vs the specific event timing.

With this feature it would be possible to perform a precise `rate` as the one you're describing, so make sure to track it: [[ES|QL] add rate agg · Issue #187753 · elastic/kibana · GitHub](https://github.com/elastic/kibana/issues/187753)

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [November 28, 2024, 12:16pm UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/5 "2024-11-28T12:16:53Z")

</div>

Thanks for your answer

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [November 28, 2024, 12:42pm UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/6 "2024-11-28T12:42:55Z")

</div>

Where could I find the rate function in Kibana? I tried to insert it into a dashboard (e.g. as a metric) but can't find it.

---

<div class="post-metadata">

**Author:** ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)\
**Post date:** [November 29, 2024, 8:50am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/7 "2024-11-29T08:50:23Z")

</div>

Without ES|QL support it is really hard to compute right now in Kibana as a rate function requires a date histogram to be associated with it, and a Metric doesn't allow to define a rate + a date histogram without some tricks.  
The only one I can think of is the compute of an average rate for a given time range:

- select a Metric chart in Lens
- Define a `Date histogram` break down and configure with a `Collapse by Average`

 ![Screenshot 2024-11-29 at 09.48.07](https://us1.discourse-cdn.com/elastic/original/3X/d/9/d9067be66e3ac8bc094ff07aa031e422b926ec23.png)

- Now define a `Counter Rate` function as primary metric

 ![Screenshot 2024-11-29 at 09.48.24](https://us1.discourse-cdn.com/elastic/original/3X/9/a/9a211b9610c94028c473fac49683131862d71941.png)

Remember, this is just an average of all the rates for each time unit selected, so not the latest one.

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [November 29, 2024, 10:29am UTC](https://discuss.elastic.co/t/need-to-calculate-a-single-value-event-rate-and-visualize-it/371076/8 "2024-11-29T10:29:52Z")

</div>

Thank you very much!
