# Does kibana support weighted average calculations

**URL:** <https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928>\
**Category:** Kibana\
**Created:** [December 1, 2017, 12:54pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928 "2017-12-01T12:54:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 1, 2017, 12:54pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/1 "2017-12-01T12:54:14Z")

</div>

Hi, Does kibana support weighted average if yes, can someone shed some light on it ? or is there any way to write a query for same?

Thank you..

---

<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:** [December 1, 2017, 10:27pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/2 "2017-12-01T22:27:54Z")

</div>

What are you weighting the values by? Something in each document or some fixed values? Off-hand, this seems like a candidate for using Scripted Fields in Kibana.

This blog has some good examples; [https://www.elastic.co/blog/using-painless-kibana-scripted-fields](https://www.elastic.co/blog/using-painless-kibana-scripted-fields)

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 2, 2017, 5:33pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/3 "2017-12-02T17:33:48Z")

</div>

Hi Lee, thanks for your response can i do below calculations ?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/c/dc1da4cab78313981764ac212ddd288883bcfbf0.png)Data

![image](https://us1.discourse-cdn.com/elastic/original/3X/8/c/8c0419c09c51ae1826470d6a0433294858fad562.png)  
if a person smoke more than three times habit\_risk\_score should double

**final\_risk** per subject = habit\_risk\_score \* weight

can you guide me on how to store new field "final\_risk" in existing index and visualize it in Kibana

thank you.... 🙂

---

<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:** [December 4, 2017, 3:19am UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/4 "2017-12-04T03:19:52Z")

</div>

Here's a good blog about scripted field in Kibana; [https://www.elastic.co/blog/using-painless-kibana-scripted-fields](https://www.elastic.co/blog/using-painless-kibana-scripted-fields)

But note that scripted fields are calculated per document. So you wouldn't be able to use the total count of multiple docs for a subject to influence the weight.  
Maybe you can calculate a risk score for each doc and then add them up?

---

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 4, 2017, 6:47pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/5 "2017-12-04T18:47:31Z")

</div>

Thanks Lee , i am trying this, can we use scripted field as input for other scripted field.

example :  
First use regex to create a scripted field **habits**'smoke'  
Then create another scripted field **score**'2' value based on previous scripted field through if construct ?

---

<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:** [December 5, 2017, 7:47pm UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/6 "2017-12-05T19:47:02Z")

</div>

I don't think you can use one scripted field inside another scripted field.

---

<div class="post-metadata">

**Author:** ![scch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scch/32/25504_2.png) [@scch](https://discuss.elastic.co/u/scch)\
**Post date:** [December 11, 2017, 6:14am UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/7 "2017-12-11T06:14:51Z")

</div>

thank you ....will calculate it again when required... 🙂

---

<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 8, 2018, 6:14am UTC](https://discuss.elastic.co/t/does-kibana-support-weighted-average-calculations/109928/8 "2018-01-08T06:14:57Z")

</div>

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