# Is there any way to aggregate an average without outliers?

**URL:** <https://discuss.elastic.co/t/is-there-any-way-to-aggregate-an-average-without-outliers/259940>\
**Category:** Elasticsearch\
**Created:** [December 31, 2020, 7:47am UTC](https://discuss.elastic.co/t/is-there-any-way-to-aggregate-an-average-without-outliers/259940 "2020-12-31T07:47:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ofir\_y](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ofir_y/32/81597_2.png) [@ofir\_y](https://discuss.elastic.co/u/ofir_y)\
**Post date:** [December 31, 2020, 7:47am UTC](https://discuss.elastic.co/t/is-there-any-way-to-aggregate-an-average-without-outliers/259940/1 "2020-12-31T07:47:41Z")

</div>

I need a way to create a transform that will aggregate the average of a field but without the outliers (let's say all values that falls between 10%-90% percentiles). for example if I have the following values:  
[1,2,3,4,5,6,7,8,9,10]

it will calculate the average of 2-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:** [January 12, 2021, 2:33pm UTC](https://discuss.elastic.co/t/is-there-any-way-to-aggregate-an-average-without-outliers/259940/2 "2021-01-12T14:33:01Z")

</div>

Unfortunately there is no out of the box solution for this.

What I can think of:

You could in addition to you existing `group_by` further group by `histogram`. In the aggregation you need to calculate an average for that bucket and you need the count of documents (`value_count` on one of the `group_by` fields).

The transform would create a document for every histogram bucket. In a 2nd pass you can query the transform dest index, using a `range` query to filter out the outliers and aggregate using a weighted average aggregations, this is where you need the count as weight.

The other idea: filter the outliers already in the transform or use a filter aggregation in the transform with a `avg` child aggregation.

To get the left and right cut off value you can use a `percentiles` aggregation.

To sum it up, I do not see a solution that can be easily implemented but both ideas require extra work and additional queries or at least 2 transforms.

---

<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:** [February 9, 2021, 2:33pm UTC](https://discuss.elastic.co/t/is-there-any-way-to-aggregate-an-average-without-outliers/259940/3 "2021-02-09T14:33:03Z")

</div>

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