# Calculate a delta between two time slices

**URL:** https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411
**Category:** Kibana
**Created:** [June 16, 2022, 1:14pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411 "2022-06-16T13:14:22Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 16, 2022, 1:14pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/1 "2022-06-16T13:14:22Z")

</div>

Is there a visualization that can calculate a delta between to two most recent records or any two time slices for that matter. Specifically we have metricbeat data from SQL server. Many of the metrics we extract are cumulative. So we'd like to create something where I can show the deltas between two values.

---

<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: [June 16, 2022, 1:32pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/2 "2022-06-16T13:32:01Z")

</div>

Hi @Kohlman,

it depends a bit on what you are looking for.  
You might use the `differences` operation for example to compute the delta between two consequent buckets, or be more explicit with two distinct offsets with a formula:

```auto
my_metric(my_field) - my_metric(my_field, shift='1w')

```

---

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 16, 2022, 2:06pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/3 "2022-06-16T14:06:29Z")

</div>

Hi @Marco_Liberati ,

Thanks for the quick response. I've spent a few minutes trying to find out where I would do such a thing . . . but to no avail.

I've created something maybe similar in a TSVB visualization where I defined two averages and then created a third with Bucket Script where I defined a function that did a simple calculation of the first/ the second. See attached screenshot. I suspect what you are suggesting must be here, but not sure where I find information about the options for the function my\_metric. And where all this goes.

Can you point me to where I might find out more about where exactly you put such a function?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/5/f55dd95cd3b54f3038e3644744a8358bfe31f339.png)

Thanks in advance for your time.

---

<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: [June 16, 2022, 2:23pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/4 "2022-06-16T14:23:39Z")

</div>

That is TSVB, while Formula is a feature available in the Lens editor.  
You can find some complete examples of formula in this blog post here: [10 common Kibana visualization questions answered with formulas and time travel | Elastic Blog](https://www.elastic.co/blog/kibana-10-common-questions-formulas-time-series-maps#performance-comparison-week)

---

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 17, 2022, 5:47pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/5 "2022-06-17T17:47:46Z")

</div>

@Marco_Liberati So this will actually help me with something else I need to create. But I don't think I can use this for my initial need. I realize I left something out of my initial description. The metric we pull out of SQL server and ingest to elastic is a value that constantly increase. For example:

- at T0, the cpu seconds used in say 500
- at T1, the cpu seconds used in say 550

From this I can calculate that the actual cpu seconds used between T0 and T1 is 50. We only import this metric once every 5 minutes. Hence why I'd like to be showing the deltas between the records, if possible.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [June 20, 2022, 9:47am UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/6 "2022-06-20T09:47:55Z")

</div>

If you can use date histogram aggregation to guarantee each bucket contains one document, [derivatige aggreagation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html) (or [serial differencing aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html)) could be an option to be considered.

---

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 22, 2022, 4:59pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/7 "2022-06-22T16:59:49Z")

</div>

In the process of trying to figure this out I also discovered differences(last\_value(sql.metrics.numeric.waits)) which also gives me what I want.

@Tomo_M I could not figure out how to plug those into a lens to create a line graph. How do I plug these into a lens? I'd still like to use these for other things.

---

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 22, 2022, 5:41pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/8 "2022-06-22T17:41:12Z")

</div>

The one downside to the differences is that you can only break it down by one field. Do the other functions support breaking it down by more then 1 field?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [June 22, 2022, 11:22pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/9 "2022-06-22T23:22:06Z")

</div>

Use Quick function of `Differences`:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/b/2beff262e6c58b589a02b35ab5ba762fa7f3da2b.png)

What do you mean by "break down by more then 1 field" ?

---

<div class="post-metadata">

### Author: ![Kohlman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohlman/32/87348_2.png) [@Kohlman](https://discuss.elastic.co/u/Kohlman)
#### Post date: [June 24, 2022, 3:50pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/10 "2022-06-24T15:50:26Z")

</div>

Hi @Tomo_M,

My previous post wasn't clear. I figured out how to use the Differences function. I did not figure out how to use [derivatige aggreagation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html) (or [serial differencing aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html)

Regarding the Differences break down by one field see the screen shots below.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/0/b09f060d4d97375b2852673dad60c9fed01fa5a4.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/1/91f2743b2363d2ef55f05f74d7447360ab693fe7.png)

There is no way to add a second field.

I tried the "Add layer" but that seems to allow you to create another line on the graph also only "grouped" by one field.

In my case I created a new run time filed where I concatenated the two fields I wanted to see my data by.

Thanks,  
Kohlman

---

<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: [July 22, 2022, 3:50pm UTC](https://discuss.elastic.co/t/calculate-a-delta-between-two-time-slices/307411/11 "2022-07-22T15:50:27Z")

</div>

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