# Scripted Field at aggregated level

**URL:** https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487
**Category:** Kibana
**Created:** [July 15, 2019, 10:00am UTC](https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487 "2019-07-15T10:00:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ranjith](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@ranjith](https://discuss.elastic.co/u/ranjith)
#### Post date: [July 15, 2019, 10:00am UTC](https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487/1 "2019-07-15T10:00:08Z")

</div>

Is it possible to create a scripted field that will calculate percentage variance at aggregate level?

For Example, the below data table is at detail level – the total variance that we are getting as 16 which is incorrect, instead it should be 6

Example 1  
The below data table is at detail level – the total that we are getting is incorrect

| Category | Quantity 2019 | Quantity 2018 | Variance | Percentage Variance |
| --- | --- | --- | --- | --- |
| 100A | 10 | 8 | 2 | 1.25 |
| 100A | 12 | 15 | 3 | 0.8 |
| 100A | 15 | 12 | 3 | 1.25 |
| 100A | 12 | 20 | 8 | 0.6 |
| Total 100A | 49 | 55 | 16 | 3.9 |

Example 2: We want it at aggregated level. The numbers below are correct.

| Category | Quantity 2019 | Quantity 2018 | Variance | Percentage Variance |
| --- | --- | --- | --- | --- |
| 100A | 49 | 55 | 6 | 0.89 |

Can anyone Please help me on this scenario?  
Thanks in Advance.

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [July 18, 2019, 11:52am UTC](https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487/2 "2019-07-18T11:52:33Z")

</div>

Hello,

Scripted fields only work in the context of a single document, so they would not be able to do calculation at aggregation level.

---

<div class="post-metadata">

### Author: ![ranjith](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@ranjith](https://discuss.elastic.co/u/ranjith)
#### Post date: [July 18, 2019, 12:19pm UTC](https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487/3 "2019-07-18T12:19:11Z")

</div>

Hi Marius,

Thanks for your response.  
Is there someother way we can achieve this in kibana directly or it would be also fine i can do this in elasticsearch.

Thanks

---

<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 15, 2019, 12:19pm UTC](https://discuss.elastic.co/t/scripted-field-at-aggregated-level/190487/4 "2019-08-15T12:19:11Z")

</div>

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