# Elastic Percentile Rank

**URL:** <https://discuss.elastic.co/t/elastic-percentile-rank/195446>\
**Category:** Elasticsearch\
**Created:** [August 16, 2019, 7:37am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446 "2019-08-16T07:37:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![620528](https://avatars.discourse-cdn.com/v4/letter/6/13edae/32.png) [@620528](https://discuss.elastic.co/u/620528)\
**Post date:** [August 16, 2019, 7:37am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/1 "2019-08-16T07:37:43Z")

</div>

I need to calculate the percentile rank of my client in a set of data when i apply a filter. For Instance when i apply a particular filter , my client might not fall in that dataset , but still i want my client to be included in the dataset and find the percentile of where he stands . In-short is it possible t override the percentile calculation of elastic search using scripting . Any suggestion on that front would be great.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 16, 2019, 12:53pm UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/2 "2019-08-16T12:53:26Z")

</div>

I do not fully understand the requirement. Do you want to include all data in the percentile calculation independent from the filter used in the query?

---

<div class="post-metadata">

**Author:** ![620528](https://avatars.discourse-cdn.com/v4/letter/6/13edae/32.png) [@620528](https://discuss.elastic.co/u/620528)\
**Post date:** [August 18, 2019, 2:45am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/3 "2019-08-18T02:45:13Z")

</div>

Thanks for your reply @spinscale.

Yes that is one of the requirement. As Elastic calculates the persentile by applying filter on the data, in one of the negative scenario case it is showing wrong percentile value over there. Can you please help ?

---

<div class="post-metadata">

**Author:** ![Poornisarathy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/poornisarathy/32/52523_2.png) [@Poornisarathy](https://discuss.elastic.co/u/Poornisarathy)\
**Post date:** [August 19, 2019, 5:54am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/4 "2019-08-19T05:54:41Z")

</div>

@spinscale for instance , i need to mark the percentile of my client data in a pool of data, I need to calculate percentile , including all data irrespective of the client data falling under the filter .

Eg : From a pool of 20 data , when i want to mark percentile for my client after applying a particular filter ( say age \> 40) and my client does not fall into the filter criteria , still i want to include him calculating percentile. so , if 12 people satisfy the filter criteria , i want it to be 13 .

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 19, 2019, 6:47am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/5 "2019-08-19T06:47:45Z")

</div>

when you need to use all data, you can wrap the percentile aggregation with a [global aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-bucket-global-aggregation.html)

---

<div class="post-metadata">

**Author:** ![Poornisarathy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/poornisarathy/32/52523_2.png) [@Poornisarathy](https://discuss.elastic.co/u/Poornisarathy)\
**Post date:** [August 19, 2019, 7:08am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/6 "2019-08-19T07:08:18Z")

</div>

@spinscale its not like i need to use all data , i want to include my client data , in all the dataset which i have got after filtering. so, out of 20 data , after applying filters i get 12 data ( in which my client data is excluded) i want to include my client data in it and do percentile calculation for my client.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 19, 2019, 7:36am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/7 "2019-08-19T07:36:28Z")

</div>

maybe you can use a [post\_filter](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-request-body.html#request-body-search-post-filter) to filter your document further, alternatively just use a [filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/search-aggregations-bucket-filter-aggregation.html) on top of the global one. Two requests could also be an option, as the aggregation only request could potentially be cached if it gets reused.

---

<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:** [September 16, 2019, 7:36am UTC](https://discuss.elastic.co/t/elastic-percentile-rank/195446/8 "2019-09-16T07:36:34Z")

</div>

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