# Handling an empty datatable

**URL:** <https://discuss.elastic.co/t/handling-an-empty-datatable/186963>\
**Category:** Kibana\
**Created:** [June 22, 2019, 4:52pm UTC](https://discuss.elastic.co/t/handling-an-empty-datatable/186963 "2019-06-22T16:52:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbarnett](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@gbarnett](https://discuss.elastic.co/u/gbarnett)\
**Post date:** [June 22, 2019, 4:52pm UTC](https://discuss.elastic.co/t/handling-an-empty-datatable/186963/1 "2019-06-22T16:52:19Z")

</div>

In Canvas, i’m Using the expression editor to calculate a percentage for use in a Metric object. After creating the datatable using ply, I’m executing the line:

math “mean(percentage)”

However, if my datatable is empty, this throws an exception. Is there a way in the expression editor to check for an empty datatable and only call the math fn if the datatable is NOT empty, otherwise set the metric value to 0?

---

<div class="post-metadata">

**Author:** ![gbarnett](https://avatars.discourse-cdn.com/v4/letter/g/ac91a4/32.png) [@gbarnett](https://discuss.elastic.co/u/gbarnett)\
**Post date:** [June 24, 2019, 2:05am UTC](https://discuss.elastic.co/t/handling-an-empty-datatable/186963/2 "2019-06-24T02:05:47Z")

</div>

Finally figured this one out. I replaced the line:

**math "mean(percentage)"**

with:

**if {rowCount | eq 0} then=0 else={math "mean(percentage)"}**

If the datatable is not empty, then it returns the average of the "percentage" column, otherwise it returns an "average" value of 0. Now I no longer get the error triangle at runtime of the canvas.

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [June 24, 2019, 3:08pm UTC](https://discuss.elastic.co/t/handling-an-empty-datatable/186963/3 "2019-06-24T15:08:35Z")

</div>

Thank you for posting your solution - helps others in community as well.

Thanks  
Rashmi

---

<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:08pm UTC](https://discuss.elastic.co/t/handling-an-empty-datatable/186963/4 "2019-07-22T15:08:38Z")

</div>

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