# Moving Average Custom Range and Interval

**URL:** <https://discuss.elastic.co/t/moving-average-custom-range-and-interval/91337>\
**Category:** Elasticsearch\
**Created:** [June 29, 2017, 6:49pm UTC](https://discuss.elastic.co/t/moving-average-custom-range-and-interval/91337 "2017-06-29T18:49:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Baker](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@William\_Baker](https://discuss.elastic.co/u/William_Baker)\
**Post date:** [June 29, 2017, 6:49pm UTC](https://discuss.elastic.co/t/moving-average-custom-range-and-interval/91337/1 "2017-06-29T18:49:06Z")

</div>

My goal is to create moving averages, over a certain range, using a certain interval. The query that I am using right now is the following;

```
{
    "size": 0,
    "query": {
    	"range" : {
        	"@timestamp" : {
            	"gte": "2017-06-01T02:00:00.000Z", 
            	"lte": "2017-07-01T00:00:00.000Z"
        	}
    	}
	},
    "aggs": {
        "my_date_histo":{                
            "date_histogram":{
                "field":"@timestamp",
                "interval":"1d"
            },
            "aggs":{
                "the_sum":{
                    "sum":{ "field": "resp_pkts" } 
                },
                "the_movavg":{
                    "moving_avg":{ "buckets_path": "the_sum" } 
                }
            }
        }
    }
}

```

Which successfully generates moving averages from 06/01/17 to 07/01/17.

The problem is that a day is considered from midnight to midnight, but I want the 24 hour period to be considered (now - 24 hours). This is a problem because, for example, right now the last day of the moving averages only contains 14 hours, because it is looking from midnight - now. As I said, I need it to be (now-24 hours).

That way each moving average will have the same number of hours (24). Is this possible without building my own aggregation?

---

<div class="post-metadata">

**Author:** ![William\_Baker](https://avatars.discourse-cdn.com/v4/letter/w/3d9bf3/32.png) [@William\_Baker](https://discuss.elastic.co/u/William_Baker)\
**Post date:** [June 29, 2017, 7:46pm UTC](https://discuss.elastic.co/t/moving-average-custom-range-and-interval/91337/2 "2017-06-29T19:46:14Z")

</div>

It looks like I can answer my own question.

The offset parameter of the date\_histogram seems to be the solution. So, I will calculate the number of minutes from midnight until now and use that as the offset.

---

<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:** [July 27, 2017, 7:46pm UTC](https://discuss.elastic.co/t/moving-average-custom-range-and-interval/91337/3 "2017-07-27T19:46:15Z")

</div>

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