# Json script divide sum of two fields and multiple it to get the percentage

**URL:** https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295
**Category:** Kibana
**Created:** [May 26, 2020, 9:48am UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295 "2020-05-26T09:48:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![akshay\_dindure1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akshay_dindure1994/32/95967_2.png) [@akshay\_dindure1994](https://discuss.elastic.co/u/akshay_dindure1994)
#### Post date: [May 26, 2020, 9:48am UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/1 "2020-05-26T09:48:39Z")

</div>

**Trying to calculate percentage for sum of two fields in metric for visulazation**

```auto
{
"script": "(doc['v1'].sum()/doc['v2'].sum())*100"
}

```

  

`But getting wrong result `

```auto
{
"script": "(doc['v1'].sum()/ 2)"
}

```

  
`but here i get right result.`  
i don't want to create this using TSVB . i want to calculate in metric visualization using advance json .

---

<div class="post-metadata">

### Author: ![myasonik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myasonik/32/62369_2.png) [@myasonik](https://discuss.elastic.co/u/myasonik)
#### Post date: [May 26, 2020, 10:03pm UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/2 "2020-05-26T22:03:16Z")

</div>

Hey @akshay_dindure1994!

This might be best done using scripted fields! See our docs: [https://www.elastic.co/guide/en/kibana/current/scripted-fields.html](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html)

---

<div class="post-metadata">

### Author: ![akshay\_dindure1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akshay_dindure1994/32/95967_2.png) [@akshay\_dindure1994](https://discuss.elastic.co/u/akshay_dindure1994)
#### Post date: [May 27, 2020, 9:46am UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/3 "2020-05-27T09:46:48Z")

</div>

`Hi @myasoni thx for the respone , but i have tried the painless script but i am not get the expected result`  
  
`Script field is working on document i.e row by row it calculates and give the output ,but i need sum of column 1 then sum of column 2 then multiple by 100 i.e i need to calculate percentage on overall documents in one index`  
  
`When i use script fields i am not getting the expected result `  
  
`For eg`

```auto
painless script
doc['v1'].value/doc['v2'].value*100

```

```auto
table created in kibana
v1 | v1 | result of painless script 
10|20 | 50%
10|50 | 20%
50| 25 |200%

```

```auto
v1 sum of total = 70 
v2 sum of total = 95
end result which i need (v1/v2)*100 =73.68421052631579‬ %

```

---

<div class="post-metadata">

### Author: ![myasonik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myasonik/32/62369_2.png) [@myasonik](https://discuss.elastic.co/u/myasonik)
#### Post date: [May 27, 2020, 5:16pm UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/4 "2020-05-27T17:16:55Z")

</div>

To sum the whole field, you'll need to use a [sum aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html)

---

<div class="post-metadata">

### Author: ![akshay\_dindure1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/akshay_dindure1994/32/95967_2.png) [@akshay\_dindure1994](https://discuss.elastic.co/u/akshay_dindure1994)
#### Post date: [May 29, 2020, 9:45am UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/5 "2020-05-29T09:45:48Z")

</div>

` Hi @myasonik got the sum aggregation part. But i want to use sum aggregation and bucket aggregation in kibana Visualization how can i achieve that with out using TSVB. I am using metric visualization and trying to do sum aggregation and bucket script in advance json part. Can you Please help me with situation.`

---

<div class="post-metadata">

### Author: ![myasonik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myasonik/32/62369_2.png) [@myasonik](https://discuss.elastic.co/u/myasonik)
#### Post date: [June 3, 2020, 6:58pm UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/6 "2020-06-03T18:58:40Z")

</div>

Sorry about the delayed response @akshay_dindure1994

Did some digging and this isn't currently possible in Visualizations unfortunately. You can see the [aggregations roadmap on Github](https://github.com/elastic/kibana/issues/58628) and can show your support for [bucket scripts](https://github.com/elastic/kibana/issues/4707) specifically if you want (we take community need into account when setting priorities).

---

<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 1, 2020, 6:58pm UTC](https://discuss.elastic.co/t/json-script-divide-sum-of-two-fields-and-multiple-it-to-get-the-percentage/234295/7 "2020-07-01T18:58:47Z")

</div>

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