# Scripted field to get percentage

**URL:** <https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071>\
**Category:** Kibana\
**Created:** [June 22, 2018, 6:53pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071 "2018-06-22T18:53:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunny1](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@sunny1](https://discuss.elastic.co/u/sunny1)\
**Post date:** [June 22, 2018, 6:53pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/1 "2018-06-22T18:53:44Z")

</div>

Hello Team,  
I am new to scripted field and facing issue in creating one. I have field called status which has multiple values like "pass", "fail", and few more. I want to create a scripted field to get pass percentage on daily basis and plot it. So basically want to do total number of pass/total number of status field on daily basis. Can someone help me or guide me where i can read for the solution?

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [June 22, 2018, 7:34pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/2 "2018-06-22T19:34:19Z")

</div>

I'm not sure a scripted field will help here. What does it look like so far? Can you just do a terms agg in a visualization over the status field?

---

<div class="post-metadata">

**Author:** ![sunny1](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@sunny1](https://discuss.elastic.co/u/sunny1)\
**Post date:** [June 22, 2018, 7:36pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/3 "2018-06-22T19:36:27Z")

</div>

No its a string field basically so it does not show up in terms agg.

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [June 22, 2018, 7:37pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/4 "2018-06-22T19:37:14Z")

</div>

Can you use a scripted field to transform it to a number? `pass` = 2, `fail` = 1 or something?

---

<div class="post-metadata">

**Author:** ![sunny1](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@sunny1](https://discuss.elastic.co/u/sunny1)\
**Post date:** [June 22, 2018, 7:39pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/5 "2018-06-22T19:39:31Z")

</div>

Yes i did like this for pass and fail value

Field name:--passtotal  
if (doc['status.keyword'].value == 'pass') {return 1}

Field name:- failtotal  
if (doc['status.keyword'].value == 'fail') {return 1}

but i dont know how i can do passtotal/passtotal+failtotal to get percentage

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [June 22, 2018, 8:57pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/6 "2018-06-22T20:57:25Z")

</div>

Pretty sure this is not supported right now.

The scripted field won't work because you don't have the context of the entire data set to calculate the necessary percentage and we don't have the ability to show percentages in a data table yet: [https://github.com/elastic/kibana/issues/19489](https://github.com/elastic/kibana/issues/19489)

---

<div class="post-metadata">

**Author:** ![sunny1](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@sunny1](https://discuss.elastic.co/u/sunny1)\
**Post date:** [June 22, 2018, 10:07pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/7 "2018-06-22T22:07:56Z")

</div>

Got it. Will wait for enhancement. Thanks.

---

<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 20, 2018, 10:08pm UTC](https://discuss.elastic.co/t/scripted-field-to-get-percentage/137071/8 "2018-07-20T22:08:10Z")

</div>

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