# Range Aggregation using difference between 2 fields

**URL:** <https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946>\
**Category:** Kibana\
**Created:** [July 24, 2019, 5:16am UTC](https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946 "2019-07-24T05:16:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karthik2411](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@Karthik2411](https://discuss.elastic.co/u/Karthik2411)\
**Post date:** [July 24, 2019, 5:16am UTC](https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946/1 "2019-07-24T05:16:35Z")

</div>

I want to create a visualization where buckets must be created with range or a value(0-5, 5-10, 10-15).  
This value is not available in a field but should be derived from difference between 2 fields.

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [July 24, 2019, 5:58pm UTC](https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946/2 "2019-07-24T17:58:46Z")

</div>

Hey @Karthik2411, you can use a [Scripted Field](https://www.elastic.co/guide/en/kibana/current/scripted-fields.html) to calculate the difference, and then use this in a range aggregation:

 ![29%20AM](https://us1.discourse-cdn.com/elastic/original/3X/3/c/3c06c7a9f284fea50dda757a7eb6743339794e83.png)

---

<div class="post-metadata">

**Author:** ![Karthik2411](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@Karthik2411](https://discuss.elastic.co/u/Karthik2411)\
**Post date:** [July 24, 2019, 8:07pm UTC](https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946/3 "2019-07-24T20:07:16Z")

</div>

Thank you so much for the reply @Brandon_Kobel . That helped me.

---

<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 21, 2019, 8:07pm UTC](https://discuss.elastic.co/t/range-aggregation-using-difference-between-2-fields/191946/4 "2019-08-21T20:07:19Z")

</div>

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