# Percentile calculation using two fields and condition

**URL:** <https://discuss.elastic.co/t/percentile-calculation-using-two-fields-and-condition/186053>\
**Category:** Elasticsearch\
**Created:** [June 17, 2019, 11:52am UTC](https://discuss.elastic.co/t/percentile-calculation-using-two-fields-and-condition/186053 "2019-06-17T11:52:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkshara](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pkshara/32/48222_2.png) [@pkshara](https://discuss.elastic.co/u/pkshara)\
**Post date:** [June 17, 2019, 11:52am UTC](https://discuss.elastic.co/t/percentile-calculation-using-two-fields-and-condition/186053/1 "2019-06-17T11:52:33Z")

</div>

Hi All,

I am trying to calculate the percentile of success rate, using 2 fields count on one condition.

Expl : I have 1000 hits

Fields are

time difference, flag, name

I need to calculate in the below format, Please suggest me how to do it in Elasticsearch/Kibana/Grafana

Success Rate Percentile = (count(time difference \<=5 AND flag=Y)/count(\*))\*100

Thanks In Advance

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [June 21, 2019, 2:26pm UTC](https://discuss.elastic.co/t/percentile-calculation-using-two-fields-and-condition/186053/2 "2019-06-21T14:26:30Z")

</div>

A [scripted percentiles agg](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#_script_7) might work for you. You can access the `time_difference` and `flag` field in the script and emit whatever values you need.

Can you describe the output a bit more, maybe give an example? The "Success Rate Percentile" equation doesn't look like a percentile, more like an average?

---

<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 19, 2019, 2:26pm UTC](https://discuss.elastic.co/t/percentile-calculation-using-two-fields-and-condition/186053/3 "2019-07-19T14:26:33Z")

</div>

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