# Creating a watcher to compare moving average values

**URL:** <https://discuss.elastic.co/t/creating-a-watcher-to-compare-moving-average-values/289634>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [November 18, 2021, 7:10pm UTC](https://discuss.elastic.co/t/creating-a-watcher-to-compare-moving-average-values/289634 "2021-11-18T19:10:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_P](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luis_p/32/92453_2.png) [@Luis\_P](https://discuss.elastic.co/u/Luis_P)\
**Post date:** [November 18, 2021, 7:10pm UTC](https://discuss.elastic.co/t/creating-a-watcher-to-compare-moving-average-values/289634/1 "2021-11-18T19:10:26Z")

</div>

Hello there,

I'm trying to build a watcher to compare two values for a moving average, one being the latest bucket moving average value and the other the moving average value for a bucket one month earlier (-1 Month offset) and I can't figure out how to access the value for the 1 month offset bucket. I have the following query:

```auto
"aggs": {
  "Current": {
    "date_histogram": {
      "field": "@timestamp",
      "calendar_interval": "day"
    },
    "aggs": {
      "average": {
        "avg": {
          "field": "RobotExecutionTime"
        }
      },
      "MA": {
        "moving_fn": {
          "buckets_path": "average",
          "window": 30,
          "script": "MovingFunctions.linearWeightedAvg(values)"
        }
      }
    }
  }

```

This query has both values I need, along with all the other results of the query and I'd like to know if I can filter it to only show the one with the latest timestamp and one with a timestamp from a month earlier.

I'd appreciate the help!

---

<div class="post-metadata">

**Author:** ![Luis\_P](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luis_p/32/92453_2.png) [@Luis\_P](https://discuss.elastic.co/u/Luis_P)\
**Post date:** [November 22, 2021, 6:13pm UTC](https://discuss.elastic.co/t/creating-a-watcher-to-compare-moving-average-values/289634/2 "2021-11-22T18:13:23Z")

</div>

Anyone, please?

---

<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:** [December 20, 2021, 6:14pm UTC](https://discuss.elastic.co/t/creating-a-watcher-to-compare-moving-average-values/289634/3 "2021-12-20T18:14:00Z")

</div>

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