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

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

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

But getting wrong result

"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 .

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

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

painless script
table created in kibana
v1 | v1 | result of painless script 
10|20  |  50%
10|50  |  20%
50| 25  |200%
v1 sum of total = 70 
v2 sum of total = 95
end result which i need  (v1/v2)*100 =73.68421052631579‬ %

To sum the whole field, you'll need to use a sum aggregation

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.

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 and can show your support for bucket scripts specifically if you want (we take community need into account when setting priorities).

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