# Keep differences from going negative

**URL:** <https://discuss.elastic.co/t/keep-differences-from-going-negative/297300>\
**Category:** Kibana\
**Created:** [February 15, 2022, 10:29pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300 "2022-02-15T22:29:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![agiddens](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@agiddens](https://discuss.elastic.co/u/agiddens)\
**Post date:** [February 15, 2022, 10:29pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300/1 "2022-02-15T22:29:01Z")

</div>

I have a couple of questions and I think they are related but I could be wrong.

Currently using Kibana 7.15.0

My data comes in every 5 minutes and looks like this.  
[{Server:"id", requests\_since\_server\_started: , timestamp: }, ...]

I want to display the total number of requests processed in a time increment broken down by server. Seems easy enough, in the dashboard the horizontal axis is a date histogram and the vertical axis is a difference of a sum of the requests\_since\_server\_started. It looks fine during normal operations but every now and then there are issues:

1. If the dashboard refresh is less than the time increment on the horizontal axis my numbers are incorrect. Is there a way to tie the refresh rate of the dashboard to the selected data range? Or is there a better way to handle displaying the difference of a sum?

2. The server resets the value back to 0 when it restarts. That causes the difference to go negative. Is there a commonly accepted way to calculate the difference for values that occasionally reset?

---

<div class="post-metadata">

**Author:** ![agiddens](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@agiddens](https://discuss.elastic.co/u/agiddens)\
**Post date:** [February 16, 2022, 4:01pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300/2 "2022-02-16T16:01:01Z")

</div>

After reading more it seems I should be able to use the following formula to prevent negative values from being displayed. However the formula screen tells me it can't be parsed because it is missing a max value. The documentation says the max value is optional.

```auto
clamp(differences(sum(bytes)), 0)

```

---

<div class="post-metadata">

**Author:** ![ghudgins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ghudgins/32/138532_2.png) [@ghudgins](https://discuss.elastic.co/u/ghudgins)\
**Post date:** [February 17, 2022, 1:30pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300/3 "2022-02-17T13:30:16Z")

</div>

Nice catch. I logged an [enhancement](https://github.com/elastic/kibana/issues/125930) for this. As a workaround just set that max value to something arbitrarily high.

---

<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:** [February 17, 2022, 3:14pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300/4 "2022-02-17T15:14:05Z")

</div>

We should definitely allow omitting the max value of clamp - thanks @ghudgins for logging the enhancement. The arbitrarily high number should work fine, however if it's not feasible for your use case you can also do this: `clamp(differences(sum(bytes)), 0, differences(sum(bytes)))` which will effectively do the same thing without a magic constant.

---

<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 17, 2022, 3:14pm UTC](https://discuss.elastic.co/t/keep-differences-from-going-negative/297300/5 "2022-03-17T15:14:39Z")

</div>

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