# Calculate timestamp duration to next document

**URL:** <https://discuss.elastic.co/t/calculate-timestamp-duration-to-next-document/297244>\
**Category:** Logstash\
**Created:** [February 15, 2022, 1:04pm UTC](https://discuss.elastic.co/t/calculate-timestamp-duration-to-next-document/297244 "2022-02-15T13:04:52Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [February 17, 2022, 10:08am UTC](https://discuss.elastic.co/t/calculate-timestamp-duration-to-next-document/297244/9 "2022-02-17T10:08:44Z")

</div>

You can concatenate the individual events using a scripted metric like in this post: [Concatenating array objects in elasticsearch transform aggregations](https://discuss.elastic.co/t/concatenating-array-objects-in-elasticsearch-transform-aggregations/289980)

The script should be enhanced to sort the events by time, then you can iterate over the list and add duration fields.

> [@jannik.b](#):
>
> I would like to have it just individually so that I can build such a table:

Unfortunately I don't see a possibility to flatten the structure. With "flatten" I mean breaking the group by into individual documents again.

---

_[View the full topic](https://discuss.elastic.co/t/calculate-timestamp-duration-to-next-document/297244)._
