# Metrics in visualization, calculate a field sum in JSON input script

**URL:** https://discuss.elastic.co/t/metrics-in-visualization-calculate-a-field-sum-in-json-input-script/185781
**Category:** Kibana
**Created:** [June 14, 2019, 7:25am UTC](https://discuss.elastic.co/t/metrics-in-visualization-calculate-a-field-sum-in-json-input-script/185781 "2019-06-14T07:25:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Magnus\_Andersson1](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Magnus\_Andersson1](https://discuss.elastic.co/u/Magnus_Andersson1)
#### Post date: [June 14, 2019, 7:25am UTC](https://discuss.elastic.co/t/metrics-in-visualization-calculate-a-field-sum-in-json-input-script/185781/1 "2019-06-14T07:25:44Z")

</div>

I have a Metric control in Canvas which uses this query:

```
SELECT 
	SUM(AbstractData.no_of_assets_with_SIR) * 100.0 / SUM(AbstractData.count_total_assets) as percentage
FROM 
	cpg_abstract_dev_project_kpi

```

Now I need to do the same thing as an Visualization on a Dashboard instead and I'm struggling with achieving the same result.

My approach so far has been to use a Metrics control in visualization, aggregate using SUM on the field "AbstractData.no\_of\_assets\_with\_SIR" and then add a script in JSON input to do the rest of the calculation (multiply "\_value" with 100 and then divide with the sum of the field AbstractData.count\_total\_assets). So far I have not been able to figure out how to get the sum of the field AbstractData.count\_total\_assets in the script. If not possible to calculate that sum directly in the script, I'm guessing that I would need yet another SUM aggregation, but I can´t see how I can do that in the Metric control. Any push in the right direction to solve this would be much appropriated!

This is my script so far:  
{  
"script": {  
"lang": "painless",  
"inline": "double total = 0.0; total = \_value \* 100; return total;"  
}  
}

I would also need to divide the "total" in the script with the sum of the field AbstractData.count\_total\_assets

Best regards  
/Magnus

---

<div class="post-metadata">

### Author: ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)
#### Post date: [June 24, 2019, 3:50pm UTC](https://discuss.elastic.co/t/metrics-in-visualization-calculate-a-field-sum-in-json-input-script/185781/2 "2019-06-24T15:50:53Z")

</div>

I think the Visual Builder visualization is your best option for a metric that includes that calculation. It includes a ratio selection. It's also known as "Time Series Visual Builder" or tsvb.

[https://www.elastic.co/guide/en/kibana/current/time-series-visual-builder.html](https://www.elastic.co/guide/en/kibana/current/time-series-visual-builder.html)

There are some discuss posts here on it, as well as some YouTube videos you can find pretty easy.

Let us know if you need help with it.

Regards,  
Lee

---

<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 22, 2019, 3:50pm UTC](https://discuss.elastic.co/t/metrics-in-visualization-calculate-a-field-sum-in-json-input-script/185781/3 "2019-07-22T15:50:54Z")

</div>

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