# How to calculate conditional sum on y axis

**URL:** <https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162>\
**Category:** Kibana\
**Created:** [February 3, 2022, 9:33am UTC](https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162 "2022-02-03T09:33:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyanka\_Rajpal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyanka_rajpal/32/101287_2.png) [@Priyanka\_Rajpal](https://discuss.elastic.co/u/Priyanka_Rajpal)\
**Post date:** [February 3, 2022, 9:33am UTC](https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162/1 "2022-02-03T09:33:03Z")

</div>

Is there a way to represent/calculate the following for y axis :  
sum = 0;  
for all items in the index :  
if condition 1 : then sum = sum+ m  
if condition 2 : then sum= sum + (1.3_m)  
if condition 3 : then sum = sum + (1.5_m)

return sum;

where m is a numeric field in the index used.  
And this should calculate sum for all the independent filters in split series.  
Over x axis I have aggregation of Date Histogram.

---

<div class="post-metadata">

**Author:** ![JLeysens](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jleysens/32/67404_2.png) [@JLeysens](https://discuss.elastic.co/u/JLeysens)\
**Post date:** [February 3, 2022, 2:44pm UTC](https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162/2 "2022-02-03T14:44:18Z")

</div>

Hm, if I'm following you correctly, perhaps this is something you could achieve with Runtime fields in which case you'd pre-calculate the values of m into a new field and use sum on that?

> **[Runtime fields | Elasticsearch Guide \[7.17\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html#runtime)**

---

<div class="post-metadata">

**Author:** ![Priyanka\_Rajpal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/priyanka_rajpal/32/101287_2.png) [@Priyanka\_Rajpal](https://discuss.elastic.co/u/Priyanka_Rajpal)\
**Post date:** [February 21, 2022, 4:55am UTC](https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162/3 "2022-02-21T04:55:20Z")

</div>

Thanks, I used the approach you suggested and it works as expected.

---

<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:** [March 21, 2022, 4:55am UTC](https://discuss.elastic.co/t/how-to-calculate-conditional-sum-on-y-axis/296162/4 "2022-03-21T04:55:48Z")

</div>

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