# Scripted field with division does not show result

**URL:** <https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246>\
**Category:** Kibana\
**Created:** [August 16, 2017, 11:20am UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246 "2017-08-16T11:20:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 16, 2017, 11:20am UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/1 "2017-08-16T11:20:25Z")

</div>

HI,  
I have created a very simple scripted field in Kibana 5 to make the division of two other fields (numeric) and the result doesn't show up.

The scripted field is defined this way:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/1/4136b08207551f0e0cd6a817930c9b902ee69246.png)

I tried to replace the division (/) by a sum (+) and it works fine.  
I tried to replace the doc['qty\_population'].value by a value like 1234 and it is OK too.  
So why the division of two fields is not working ?

Thanks a lot for your help.  
Dominique

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 16, 2017, 7:12pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/2 "2017-08-16T19:12:04Z")

</div>

When you says it's not working, what are you getting in Kibana? 0? Some incorrect value? An error?

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 16, 2017, 7:54pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/3 "2017-08-16T19:54:57Z")

</div>

If you're getting an error when you go to Discover in Kibana, please click the More Info button and copy/paste it here. If you're getting an error there, it could be that you have some docs in this index which don't have a value (or have 0 value) for `qty_population`.  
It's pretty easy to check by filtering on that field existing, or by filtering on it's value.  
If it's the case, you can work-around that with a little fancier scripting in your scripted field.

---

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 17, 2017, 8:43am UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/4 "2017-08-17T08:43:06Z")

</div>

Hi Lee,  
Thanks a lot for your time.  
Actually I found the root cause of my problem.  
The two fields work\_cost\_eur and qty\_population are sometimes empty, sometimes equal to 0, sometimes filled with correct values but they are not filled with correct values in the same record. In some records you have a work cost, in others you have population but never in the same record so the division will always fail if it is done at record level. For a certain period, I must do the sum of the work costs, then the sum of the population and then divide the first by the second.  
A tip on how do do that in Kibana ?  
Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 17, 2017, 9:15am UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/5 "2017-08-17T09:15:59Z")

</div>

To illustrate, I want to divide the green by the blue, periods are months:

![image](https://us1.discourse-cdn.com/elastic/original/3X/b/7/b7aef7964483f2619c3b09eca6057153b9254a64.png)

And display it with bars.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 17, 2017, 5:11pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/6 "2017-08-17T17:11:53Z")

</div>

I would suggest the Visual Builder visualization.

Here's one post where we do a ration based on a filter;

> [@Doing Math over count of events](https://discuss.elastic.co/t/doing-math-over-count-of-events/97278/2):
>
> Here's one option if you're on a recent version of Kibana that has the Visual Builder visualization type. I know it's in 5.4.0 and later. go to the Panel Options tab and set your index pattern (here I'm using metricbeat-\*); [image] back on the Data tab select Filter Ratio [image] I picked metricset.name:process as my numerator, but yours would be something like transactionStatus:Success [image] The Gauge tab shows a single value and there some options to format as a percent, change…

But you need to sum specific values, so I think you need to do it a little different. This post uses the calculation aggregation;

> [@How to add 2 metrics visualization?](https://discuss.elastic.co/t/how-to-add-2-metrics-visualization/89017/2):
>
> If you are using 5.4 you can achieve this with the Time Series Visual Builder. [https://www.elastic.co/guide/en/kibana/current/\_featured\_visualizations.html](https://www.elastic.co/guide/en/kibana/current/_featured_visualizations.html) You can do many calculations on aggregations. For example, here is a comparison of average bytes calculated automatically vs manually: automatically: Running the calculations manually: [image]

Let me know if this works for you.

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 18, 2017, 12:21pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/7 "2017-08-18T12:21:29Z")

</div>

Hi Lee,  
I tried Visual Builder but in your second example above (manually) I have the calculation choice which is greyed, probably because I need a child aggregation ? (parent pipeline aggregation requires child aggregation)  
I guess this is not my case. Is it mandatory to have this child aggregation ?

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 18, 2017, 4:59pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/8 "2017-08-18T16:59:23Z")

</div>

I think this should get you the ratio of your 2 metrics.

1. Create a Sum aggregation for the sum of the first metric
2. Create a Sum aggregation for the sum of the second metric
3. Create a Calculation aggregation, create 2 variables to represent the 2 sums above, put the ratio of the 2 in the Painless Script field. Make sure to put `params.` before your variable names

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/2/02e88e87777b54d364cd5ec4df55ec189515692f.png)

---

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 21, 2017, 2:44pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/9 "2017-08-21T14:44:59Z")

</div>

Hi Lee,  
I tried it but I reach a limitation.  
My costs and population can be displayed MONTHLY in regular Kibana:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/5/658f122de87341d7eb5be2fe71c748d0c00ee350.png)

But when I go in Visual Builder I can only display my costs and populations WEEKLY (if I try monthly I get this message: Max buckets exceeded: 527039 is greater than 2000, try a larger time interval in the panel options. Strange as Regular Kibana can do it)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/4/b406d5e2666e804f080a63a83ddec651e32b84e6.png)

The problem is that my populations are an average for a month but stored in the first week of the month:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/3/335eddf6861404d5d3a77df39af0ef9c69f1970f.png)

SO I cannot divide costs per population because sometimes populations values are empty.  
Maybe I should do all these calculation in my index. Like this it will be easy to display.

Thanks again for your help anyway.  
Maybe you have a tip for the monthly display in Visual Builder.

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 21, 2017, 8:59pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/10 "2017-08-21T20:59:16Z")

</div>

Did you try `1m` (one minute) or `1M` (one Month)?

---

<div class="post-metadata">

**Author:** ![doume06](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doume06/32/21201_2.png) [@doume06](https://discuss.elastic.co/u/doume06)\
**Post date:** [August 22, 2017, 12:05pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/11 "2017-08-22T12:05:09Z")

</div>

Hi Lee,  
Indeed I was using 1m as I thought it was 1month.  
I used 1M instead and it works just fine.  
Thanks a lot for your help, it was of great help.  
Cheers.  
PS: I will probably have other questions/feedback. Is the forum the right place to post messages or not ?

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [August 22, 2017, 1:11pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/12 "2017-08-22T13:11:56Z")

</div>

Glad it worked for you!

Yes, this is the best place. The Kibana engineering team monitors this forum and tries to make sure users get a response (either from us or from other users). So feel free to subscribe to new posts and help if you can 🙂

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![Gwendaline\_p](https://avatars.discourse-cdn.com/v4/letter/g/5f8ce5/32.png) [@Gwendaline\_p](https://discuss.elastic.co/u/Gwendaline_p)\
**Post date:** [September 6, 2017, 8:50am UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/13 "2017-09-06T08:50:03Z")

</div>

Hi Lee ,

For people who don't have a recent version (for example, I have the 4.5.4), how can I do the same thing please ?

Kind regards ,  
Gwendaline

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [September 6, 2017, 7:43pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/14 "2017-09-06T19:43:02Z")

</div>

Hi Gwendaline,

Sorry, but I don't think there is a way built in to 4.5.4. Someone might be able to create a custom plugin, but that would be beyond my current skills.

The latest Kibana release was 5.5.2 and 6.0.0-beta2 was recently released, so there won't be any new features added to 4.x or 5.x versions of Kibana. It would be best if you (or your company) could upgrade. Kibana developers are currently working on features for the 6.1 release.

Regards,  
Lee

---

<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:** [October 4, 2017, 7:43pm UTC](https://discuss.elastic.co/t/scripted-field-with-division-does-not-show-result/97246/15 "2017-10-04T19:43:09Z")

</div>

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