# Subtracting 2 fields different document on Scripted Field / json input

**URL:** https://discuss.elastic.co/t/subtracting-2-fields-different-document-on-scripted-field-json-input/286203
**Category:** Kibana
**Created:** [October 8, 2021, 8:17am UTC](https://discuss.elastic.co/t/subtracting-2-fields-different-document-on-scripted-field-json-input/286203 "2021-10-08T08:17:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rikizita3](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@rikizita3](https://discuss.elastic.co/u/rikizita3)
#### Post date: [October 8, 2021, 8:17am UTC](https://discuss.elastic.co/t/subtracting-2-fields-different-document-on-scripted-field-json-input/286203/1 "2021-10-08T08:17:01Z")

</div>

I have a set of data below.

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

I want to add the red marker then subtract it to the blue marker.  
I tried using Scripted Field.

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

But when I go back to discovery I have this error.

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

If I will put it on the line graph I want to subtract the blue line from the green line to see the difference on the data.

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

---

<div class="post-metadata">

### Author: ![mattkime](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattkime/32/43522_2.png) [@mattkime](https://discuss.elastic.co/u/mattkime)
#### Post date: [October 8, 2021, 2:44pm UTC](https://discuss.elastic.co/t/subtracting-2-fields-different-document-on-scripted-field-json-input/286203/3 "2021-10-08T14:44:12Z")

</div>

Hello @rikizita3

It looks like you need to perform calculations across data. The best way to address this would be to get both fields into the same document. How are you ingesting your data?

---

<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: [November 5, 2021, 2:44pm UTC](https://discuss.elastic.co/t/subtracting-2-fields-different-document-on-scripted-field-json-input/286203/4 "2021-11-05T14:44:53Z")

</div>

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