# Kibana group by taskname and count average days between dates

**URL:** <https://discuss.elastic.co/t/kibana-group-by-taskname-and-count-average-days-between-dates/218131>\
**Category:** Kibana\
**Created:** [February 6, 2020, 9:34am UTC](https://discuss.elastic.co/t/kibana-group-by-taskname-and-count-average-days-between-dates/218131 "2020-02-06T09:34:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SBH](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@SBH](https://discuss.elastic.co/u/SBH)\
**Post date:** [February 6, 2020, 9:34am UTC](https://discuss.elastic.co/t/kibana-group-by-taskname-and-count-average-days-between-dates/218131/1 "2020-02-06T09:34:46Z")

</div>

Hi

I have an index containing taskname, startdate and enddate. I wan't to group by taskname, that's no problem, but now i wan't to calculate the interval between start and enddate and show the average per group. Any suggestions?

Thanks in advance.

/Soren

---

<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 6, 2020, 10:19am UTC](https://discuss.elastic.co/t/kibana-group-by-taskname-and-count-average-days-between-dates/218131/2 "2020-02-06T10:19:07Z")

</div>

Hi, you can do this by adding a [scripted field](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html). The script should take start and enddate and calculate the duration of the document. Then you can use a regular average aggregation on the newly created field.

If this applies to your use case you can also calculate the duration prior to ingesting the document and put it in the document to begin with (this allows more efficient querying which can become relevant if you have lots of data)

---

<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 5, 2020, 10:19am UTC](https://discuss.elastic.co/t/kibana-group-by-taskname-and-count-average-days-between-dates/218131/3 "2020-03-05T10:19:10Z")

</div>

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