# Scripted Field on blank or zero fields

**URL:** <https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135>\
**Category:** Kibana\
**Created:** [March 29, 2018, 5:41pm UTC](https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135 "2018-03-29T17:41:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinVarbanov](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@MartinVarbanov](https://discuss.elastic.co/u/MartinVarbanov)\
**Post date:** [March 29, 2018, 5:41pm UTC](https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135/1 "2018-03-29T17:41:13Z")

</div>

Hello everyone,  
I am new to Kabana and I am trying to create a scripted field using the following script:

`(doc['account_chargeable_hours'].value) / ( doc['avail_hrs'].value - doc['hsv_hours'].value )`

However it is possible some or all of the values to be zero or blank. How to solve such issue?

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [March 29, 2018, 9:30pm UTC](https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135/2 "2018-03-29T21:30:03Z")

</div>

You could do something like this (untested)

```auto
if (doc.containsKey('account_chargeable_hours') && doc.containsKey('avail_hrs') && doc.containsKey('hsv_hours')) {
  return (doc['account_chargeable_hours'].value) / ( doc['avail_hrs'].value - doc['hsv_hours'].value )
}

```

---

<div class="post-metadata">

**Author:** ![MartinVarbanov](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@MartinVarbanov](https://discuss.elastic.co/u/MartinVarbanov)\
**Post date:** [March 30, 2018, 9:34am UTC](https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135/3 "2018-03-30T09:34:46Z")

</div>

Hi Tyler,

The problem is bigger. If I use the time series I can correctly build my chart however the scripted field calculation is leading to mathematical error which I demonstrate with the attached picture.  
Is it possible with a scripted field to archive 0.49 instead of 0.55?

 ![Kibana](https://us1.discourse-cdn.com/elastic/original/3X/4/1/41883f5a11fff0c0a380ec0ea91d7032058b5d39.JPG)

Thank you,  
Martin

---

<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:** [April 27, 2018, 9:34am UTC](https://discuss.elastic.co/t/scripted-field-on-blank-or-zero-fields/126135/4 "2018-04-27T09:34:53Z")

</div>

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