# Last Valid Value of a Field in Scripted Fields

**URL:** <https://discuss.elastic.co/t/last-valid-value-of-a-field-in-scripted-fields/250918>\
**Category:** Elasticsearch\
**Created:** [October 5, 2020, 4:12am UTC](https://discuss.elastic.co/t/last-valid-value-of-a-field-in-scripted-fields/250918 "2020-10-05T04:12:23Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Hendrik\_Muhs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hendrik_muhs/32/25802_2.png) [@Hendrik\_Muhs](https://discuss.elastic.co/u/Hendrik_Muhs)\
**Post date:** [October 5, 2020, 7:23pm UTC](https://discuss.elastic.co/t/last-valid-value-of-a-field-in-scripted-fields/250918/6 "2020-10-05T19:23:33Z")

</div>

You can check this [discuss post](https://discuss.elastic.co/t/merging-documents-based-on-matched-fields-values/249728), this is about merging 2 documents. Conceptually you want the same but in addition you want to apply a calculation. In a nutshell you need to get the values for `x` and `y` in the map phase and do the calculation in the reduce phase.

If you haven't worked with aggregations before: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html)

If you want to create new documents, checkout transform: [https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html)

I know this is a lot to read. It might be easier to look for blogs first, e.g. [https://www.elastic.co/blog/intro-to-aggregations/](https://www.elastic.co/blog/intro-to-aggregations/) or this webinar recording about transform: [https://www.elastic.co/webinars/introducing-data-frame-transforms-for-elastic-machine-learning](https://www.elastic.co/webinars/introducing-data-frame-transforms-for-elastic-machine-learning)

---

_[View the full topic](https://discuss.elastic.co/t/last-valid-value-of-a-field-in-scripted-fields/250918)._
