# Division of two aggregated field under Scripted Field

**URL:** <https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802>\
**Category:** Kibana\
**Created:** [July 26, 2018, 3:01pm UTC](https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802 "2018-07-26T15:01:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [July 26, 2018, 3:01pm UTC](https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802/1 "2018-07-26T15:01:04Z")

</div>

Hi All

I have data in this format

| Order | Month | Revenue |
| --- | --- | --- |
| Order001 | Jan | 10 |
| Order001 | Jan | 20 |
| Order001 | Jan | 30 |
| Order001 | Jan | 40 |
| Order002 | Jan | 50 |
| Order002 | Jan | 60 |
| Order002 | Jan | 70 |
| Order003 | Feb | 80 |
| Order003 | Feb | 90 |
| Order003 | Feb | 100 |
| Order004 | Feb | 20 |
| Order004 | Feb | 10 |
| Order005 | Feb | 10 |

Now by using Scripted Field i want to calculate Average Value whose formula is  
Average Value = (Sum of Revenue / Unique Count of Orders) for a given Month  
Average Value for Jan = 280/2 = 140  
Average Value for Feb=310/3= 103

How can i achieve this using scripted field?

Thanks

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [July 26, 2018, 8:09pm UTC](https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802/2 "2018-07-26T20:09:05Z")

</div>

There really isn't any way to get this information via scripted fields, but you can do this with the "Average Bucket" metric.

If you set it up like in the screenshot it will split the bucket the data by order, sum up the revenue for each bucket, and then take an average of those sums, getting the same value you described.  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/b/eb3c8b10aeb97d7c822e5da354d733c4ab2a81f2.png)

If you combine that with a "Split rows" bucket on month, you can get a visualization showing what you asked for:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/6/56026451a7883c04b8a9e9b974968b81791d5b0e.png)

---

<div class="post-metadata">

**Author:** ![rkhapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rkhapre/32/48333_2.png) [@rkhapre](https://discuss.elastic.co/u/rkhapre)\
**Post date:** [July 27, 2018, 9:16am UTC](https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802/3 "2018-07-27T09:16:08Z")

</div>

Okay, this is a good solution. Thanks @spalger.

---

<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:** [August 24, 2018, 9:24am UTC](https://discuss.elastic.co/t/division-of-two-aggregated-field-under-scripted-field/141802/4 "2018-08-24T09:24:09Z")

</div>

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