# Timelion count with a condition

**URL:** https://discuss.elastic.co/t/timelion-count-with-a-condition/154925
**Category:** Kibana
**Tags:** timelion
**Created:** [October 31, 2018, 8:42pm UTC](https://discuss.elastic.co/t/timelion-count-with-a-condition/154925 "2018-10-31T20:42:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)
#### Post date: [October 31, 2018, 8:42pm UTC](https://discuss.elastic.co/t/timelion-count-with-a-condition/154925/1 "2018-10-31T20:42:56Z")

</div>

I have a need for doing a timelion count of a certain field (myField)... but only if another field (errorField) is not 0.

So I made a scripted field: myFieldIfNoError  
if (doc['errorField'].value != null && doc['errorField'].value != 0){ return doc['myField'].value; } return null.

.es(index=myindex,timefield=myTimeStamp,metric=count:myFieldIfNoError)

But the count is still counting the 0 and null values. I only want a count of myField when errorField is not 0.

What's the right thing to do?

---

<div class="post-metadata">

### Author: ![thomasneirynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomasneirynck/32/23313_2.png) [@thomasneirynck](https://discuss.elastic.co/u/thomasneirynck)
#### Post date: [October 31, 2018, 9:59pm UTC](https://discuss.elastic.co/t/timelion-count-with-a-condition/154925/2 "2018-10-31T21:59:55Z")

</div>

hi @Joseph_Mak,

I would have the scripted field return either `0` and `1` and then instead of doing a count, do a `sum()` on that field. That will give you the count, and omit the documents with the `0`-value.

---

<div class="post-metadata">

### Author: ![Joseph\_Mak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joseph_mak/32/42230_2.png) [@Joseph\_Mak](https://discuss.elastic.co/u/Joseph_Mak)
#### Post date: [November 1, 2018, 2:48pm UTC](https://discuss.elastic.co/t/timelion-count-with-a-condition/154925/3 "2018-11-01T14:48:07Z")

</div>

Thanks that seems to do the trick (was thinking I probably need a .if())

---

<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: [November 29, 2018, 2:48pm UTC](https://discuss.elastic.co/t/timelion-count-with-a-condition/154925/4 "2018-11-29T14:48:07Z")

</div>

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