# Perform division of two fields in a Kibana visualisation

**URL:** <https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772>\
**Category:** Kibana\
**Created:** [March 1, 2021, 9:47am UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772 "2021-03-01T09:47:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![2f404140b7ab0f69189f](https://avatars.discourse-cdn.com/v4/letter/2/bb73d2/32.png) [@2f404140b7ab0f69189f](https://discuss.elastic.co/u/2f404140b7ab0f69189f)\
**Post date:** [March 1, 2021, 9:47am UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/1 "2021-03-01T09:47:30Z")

</div>

I'm unable to perform a basic division of two fields on Kibana. First, I tried to do so in a Line Chart bu using the Advanced Json Input section with this script:  
`{"script": "if(doc['kpi.abc'].value > 0) then {(doc['kpi.def'].value / doc['kpi.abc'].value)*100}"} `  
but I received 'esaggs' error.  
When I tried to create a scripted field in the index using these codes:

```auto
if (doc['kpi.abc'].value > 0) { 
  return (doc['kpi.def'].value / doc['kpi.abc'].value);
}

```

and

```auto
doc['kpi.abc'].value > '0' ? doc['kpi.def'].value / doc['kpi.abc'].value : doc['kpi.abc'].value == '0' ? '0'

```

I still received errors. How can I resolve this?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [March 1, 2021, 10:30am UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/2 "2021-03-01T10:30:21Z")

</div>

There are probably documents in your index which don't have a value for `kpi.NLAPAG1LOTOT` - make sure to only access values in documents where they exist by appending this to the script:

```auto
if(doc["kpi.NLAPAG1LOTOT"].size()==0) { return null }

```

make sure to do this for all fields you are using in your script.

The first script you pasted is correct - in the second you are comparing with a string `'0'` instead of the number zero.

---

<div class="post-metadata">

**Author:** ![2f404140b7ab0f69189f](https://avatars.discourse-cdn.com/v4/letter/2/bb73d2/32.png) [@2f404140b7ab0f69189f](https://discuss.elastic.co/u/2f404140b7ab0f69189f)\
**Post date:** [March 1, 2021, 12:03pm UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/3 "2021-03-01T12:03:38Z")

</div>

> [@flash1293](#):
>
> `if(doc["kpi.abc"].size()==0) { return null }`

Hey, thanks for the response. I've implemented your suggestion and am using this code in my line chart visualisation, but still facing an issue with it : esaggs error

```auto
{"script": "if(doc['kpi.abc'].size()==0) {return null} else if (doc['kpi.abc'].value > 0) {return (doc['kpi.def'].value / doc['kpi.abc'].value)*100}"}

```

Additionally, my scripted field is made now but gives no output:

```auto
if(doc["kpi.abc"].size()==0) { return null; }
else if (doc['kpi.abc'].value > 0) { 
  return (doc['kpi.def'].value / doc['kpi.abc'].value)*100;}

```

---

<div class="post-metadata">

**Author:** ![2f404140b7ab0f69189f](https://avatars.discourse-cdn.com/v4/letter/2/bb73d2/32.png) [@2f404140b7ab0f69189f](https://discuss.elastic.co/u/2f404140b7ab0f69189f)\
**Post date:** [March 1, 2021, 12:05pm UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/4 "2021-03-01T12:05:10Z")

</div>

```auto
if(doc["kpi.abc"].size()==0) { return null; }
else if (doc['kpi.abc'].value > 0) { 
  return (doc['kpi.def'].value / doc['kpi.abc'].value)*100;
}

```

This is the scripted field. Sorry, it didn't get uploaded in my previous reply.

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [March 1, 2021, 1:17pm UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/5 "2021-03-01T13:17:22Z")

</div>

The script in advanced json won't work, the scripted field is the right approach here. Please add a similar check for the `kpi.NLAPAG1LOTOT` and remove the script from advanced json (just select the scripted field from the dropdown)

---

<div class="post-metadata">

**Author:** ![2f404140b7ab0f69189f](https://avatars.discourse-cdn.com/v4/letter/2/bb73d2/32.png) [@2f404140b7ab0f69189f](https://discuss.elastic.co/u/2f404140b7ab0f69189f)\
**Post date:** [March 1, 2021, 1:19pm UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/6 "2021-03-01T13:19:54Z")

</div>

Ok, thanks a lot!

---

<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 29, 2021, 1:20pm UTC](https://discuss.elastic.co/t/perform-division-of-two-fields-in-a-kibana-visualisation/265772/7 "2021-03-29T13:20:03Z")

</div>

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