# How to calculate duration in aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644>\
**Category:** Elasticsearch\
**Created:** [June 22, 2016, 9:59am UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644 "2016-06-22T09:59:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruival](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@ruival](https://discuss.elastic.co/u/ruival)\
**Post date:** [June 22, 2016, 9:59am UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644/1 "2016-06-22T09:59:55Z")

</div>

Hi all,

first posts here in elasticsearch forum and new to this exciting technology.

working on a proof of concept and already loaded/indexed some data into elastic.

Data is structured like the sample bellow:

SIG201600064827 Registo do Pedido - Submetida em 2016-05-22 15:19:11.357 JANDRADE42  
SIG201600064827 Registo do Pedido - Finalizada em 2016-05-22 15:19:25.220 SISTEMA  
SIG201600064827 Validação do Pedido - Atribuída em 2016-05-22 15:20:00.300 JJESUS01

The challenge here is to calculate the duration of the event.

Each line of the above sample is one step, so , as we can see , the task in the sample above has been completed in 3 steps.  
How can i build an aggregation and calculate the duration of the task ?

duration = time of step "Validação do Pedido - Atribuída em" - time of step "Registo do Pedido - Submetida em"

best regards

Rui Madaleno

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 22, 2016, 2:00pm UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644/2 "2016-06-22T14:00:03Z")

</div>

Are you needing to do this for many task IDs? e.g. you want to find the average/min/max duration in an index with millions of tasks?

---

<div class="post-metadata">

**Author:** ![ruival](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@ruival](https://discuss.elastic.co/u/ruival)\
**Post date:** [June 22, 2016, 3:04pm UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644/3 "2016-06-22T15:04:03Z")

</div>

Hi Mark, thank you for your response.

in fact the logical structure of my data is

```
<task id> <step name> <time of step> <user executing the step>

```

i need to answer questions like

"how long does it takes the task to be completed ?"

So, i really need to perform this calculation in thousands of taskid's

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [June 22, 2016, 3:07pm UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644/4 "2016-06-22T15:07:29Z")

</div>

Toughest question to answer on an event store is something like "what is the average duration of web sessions on my website?". This looks like that sort of question.

Why it is hard and how to solve it is covered in the "entity centric indexing" talk here: [https://www.youtube.com/watch?v=yBf7oeJKH2Y](https://www.youtube.com/watch?v=yBf7oeJKH2Y)

It includes example scripts you can download.

---

<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 5, 2017, 10:41pm UTC](https://discuss.elastic.co/t/how-to-calculate-duration-in-aggregation/53644/5 "2017-07-05T22:41:22Z")

</div>


