# Perform calculation on metric aggregation in visualization

**URL:** <https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932>\
**Category:** Kibana\
**Created:** [April 29, 2019, 12:59pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932 "2019-04-29T12:59:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny.batra](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@sunny.batra](https://discuss.elastic.co/u/sunny.batra)\
**Post date:** [April 29, 2019, 12:59pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932/1 "2019-04-29T12:59:56Z")

</div>

We have a requirement to calculate the overall TAT(turnaround time) of the system. In our application a package is flown from many modules sequentially and each module do some operations on the package and move forward. So we have entry of in and out of each module in elastic.  
The need is to calculate time difference between "in time" of first module and "out time" of last module. that should give us the over all time taken by the system  
I have achieved to get the "in time" and "out time" for each package using min and max aggregation but not able to use this aggregation value to perform operation.  
I have attached the related stuff. I need to get the difference between In Time and Out Time column

---

<div class="post-metadata">

**Author:** ![sunny.batra](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@sunny.batra](https://discuss.elastic.co/u/sunny.batra)\
**Post date:** [April 29, 2019, 1:02pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932/2 "2019-04-29T13:02:37Z")

</div>

![Matric%20aggregation](https://us1.discourse-cdn.com/elastic/original/3X/3/5/3550e577829655a2100a607fb2a2431041c8cf98.png)

---

<div class="post-metadata">

**Author:** ![sunny.batra](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@sunny.batra](https://discuss.elastic.co/u/sunny.batra)\
**Post date:** [April 29, 2019, 1:03pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932/3 "2019-04-29T13:03:03Z")

</div>

My sample data is 🙂  
PUT package/doc/11  
{  
"package\_name": "Test",  
"in\_timestamp" : "2019-02-13T13:15:30Z",  
"app\_name" : "Test APP 1"  
}

PUT package/doc/12  
{  
"package\_name": "Test",  
"out\_timestamp" : "2019-02-16T13:16:30Z",  
"in\_timestamp" : "2019-02-14T13:15:30Z",  
"app\_name" : "Test APP 1"  
}

PUT package/doc/13  
{  
"package\_name": "Test",  
"in\_timestamp" : "2019-03-02T13:18:30Z",  
"app\_name" : "Test APP 2"  
}  
PUT package/doc/14  
{  
"package\_name": "Test",  
"out\_timestamp" : "2019-03-03T13:20:30Z",  
"in\_timestamp" : "2019-03-02T13:18:30Z",  
"app\_name" : "Test APP 2"  
}

PUT package/doc/15  
{  
"package\_name": "Test",  
"in\_timestamp" : "2019-03-03T13:21:30Z",  
"app\_name" : "Test APP 3"  
}

PUT package/doc/16  
{  
"package\_name": "Test",  
"out\_timestamp" : "2019-03-04T13:25:30Z",  
"in\_timestamp" : "2019-03-03T13:21:30Z",  
"app\_name" : "Test APP 3"  
}

---

<div class="post-metadata">

**Author:** ![Bill\_McConaghy](https://avatars.discourse-cdn.com/v4/letter/b/ed655f/32.png) [@Bill\_McConaghy](https://discuss.elastic.co/u/Bill_McConaghy)\
**Post date:** [April 29, 2019, 2:02pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932/4 "2019-04-29T14:02:15Z")

</div>

I think this what you would need for the Elasticsearch query: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html)

However, I'm not sure that this is supported by Kibana visualizations. If you could change the way you are storing the in/out times so that they are all fields on a single document, then you could use a scripted field to calculate the time spent and then use this field in a visualization.

---

<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:** [May 27, 2019, 2:13pm UTC](https://discuss.elastic.co/t/perform-calculation-on-metric-aggregation-in-visualization/178932/5 "2019-05-27T14:13:33Z")

</div>

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