# Kibana JSON input to calculate the percentage of two fields and display in metric visualisatio

**URL:** https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136
**Category:** Kibana
**Created:** [December 10, 2018, 9:59am UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136 "2018-12-10T09:59:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![raghav1107](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raghav1107](https://discuss.elastic.co/u/raghav1107)
#### Post date: [December 10, 2018, 9:59am UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136/1 "2018-12-10T09:59:53Z")

</div>

Hi,

I am using the below code in the json input to calculate the overall percentage of two fields and display in metric visualisation. But i am getting empty value in the metric display.

Here i want to calculate the sum of rows for field A and sum of rows for field B and then calculate the percentage. ![30%20PM%202](https://us1.discourse-cdn.com/elastic/original/3X/c/9/c95e89c96437e59dcd549c4e1d7b71b9fedc4802.jpeg)  
Can you please help me here ? I am using cabana version 6.1.1.

{  
"script": {  
"lang":"painless",  
"inline": "doc['automated'].sum()\*100/doc['iters'].sum()"  
}  
}

---

<div class="post-metadata">

### Author: ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)
#### Post date: [December 10, 2018, 10:29pm UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136/2 "2018-12-10T22:29:29Z")

</div>

I'll admit it's been a while since I used the JSON Input field, but I don't think you can use it like that. That input is more of a fallback for Elasticsearch Query DSL params you want to add to the aggregation that Kibana doesn't have a UI for.

You can create a scripted field in Management \> Index Patterns, which is kind of like a virtual field in Kibana driven by the script. Basically, you add a new field and the script you posted is the value, then you'll have a new field available in the dropdown throughout Kibana. Then you can use that new field in the metric vis.

---

<div class="post-metadata">

### Author: ![raghav1107](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@raghav1107](https://discuss.elastic.co/u/raghav1107)
#### Post date: [December 11, 2018, 5:28am UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136/3 "2018-12-11T05:28:30Z")

</div>

Adding scripted field will apply for all the rows in the index and will not apply when we add a new filter in the visualisation. Lets say there are 10 rows with field A, field B, field C in the index and want to calculate the percentage between field A/field B based on the filter for field C , then this will not work. Please suggest if there is any other way to achieve this.

---

<div class="post-metadata">

### Author: ![Joe\_Fleming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joe_fleming/32/3561_2.png) [@Joe\_Fleming](https://discuss.elastic.co/u/Joe_Fleming)
#### Post date: [December 14, 2018, 12:07am UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136/4 "2018-12-14T00:07:56Z")

</div>

> Lets say there are 10 rows with field A, field B, field C in the index and want to calculate the percentage between field A/field B based on the filter for field C , then this will not work.

Won't it? With the scripted field, you effectively end up with a table of 4 columns; A, B, C, and the painless output. So filtering on C will reduce the total number of rows, but every one will still have the scripted column, and you can still calculate the metric value from that column.

Here's a contrived example, using the sample flight data available in recent version of Kibana:

 ![06](https://us1.discourse-cdn.com/elastic/original/3X/1/4/14844c028e4ce18ad6a71c8163399be092abee6e.png)

Here, `hour_of_day` is a scripted field, and `Carrier` is not. I'm creating a metric that is the sum of `hour_of_day` and filtering based on Carrier. Based on my understanding of what you're trying to do, it's basically the same operation. Maybe I'm missing something though?

Disabling the column shows the total sum of `hour_of_day`:

 ![22](https://us1.discourse-cdn.com/elastic/original/3X/5/c/5cec1e5306861dea6582b44aa03f654bb8ee8fe8.png)

---

<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: [January 11, 2019, 12:08am UTC](https://discuss.elastic.co/t/kibana-json-input-to-calculate-the-percentage-of-two-fields-and-display-in-metric-visualisatio/160136/5 "2019-01-11T00:08:03Z")

</div>

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