# Date\_histogram returning wrong number of buckets

**URL:** <https://discuss.elastic.co/t/date-histogram-returning-wrong-number-of-buckets/219702>\
**Category:** Elasticsearch\
**Created:** [February 18, 2020, 5:17am UTC](https://discuss.elastic.co/t/date-histogram-returning-wrong-number-of-buckets/219702 "2020-02-18T05:17:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kpiyush](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kpiyush/32/45904_2.png) [@kpiyush](https://discuss.elastic.co/u/kpiyush)\
**Post date:** [February 18, 2020, 5:17am UTC](https://discuss.elastic.co/t/date-histogram-returning-wrong-number-of-buckets/219702/1 "2020-02-18T05:17:10Z")

</div>

Hi,

I am using following query to get the aggs based on date on per day interval with extended\_bounds

/index1

```auto
{
    "size": 0,
    "query":{"bool":{"must":[{"range":{"CreatedDate":{"lte":"2020-02-13T23:59:59"}}}]}},
    "aggs": {
        "new_users": {
            "filter": {
                "range": {
                    "CreatedDate": {
                        "gte": "2020-01-15T00:00:00",
                        "lte": "2020-02-13T23:59:59"
                    }
                }
            },
            "aggs": {
                "user_growth_by_month": {
                    "date_histogram": {
                        "field": "CreatedDate",
                        "interval": "day",
                        "format": "date_optional_time",
                        "time_zone": "-12:00",
                        "min_doc_count": 0,
                        "extended_bounds": {
                            "min": "2020-01-15T00:00:00",
                            "max": "2020-02-13T23:59:59"
                        }
                    },
                    "aggs": {
                        "cumulative_growth": {
                            "cumulative_sum": {
                                "buckets_path": "_count"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

As mentioned in the query, I have given the range of date `"gte": "2020-01-15T00:00:00"`, but in the response, the buckets are starting with the previous date.

```auto
"buckets": [
                    {
                        "cumulative_growth": {
                            "value": 1
                        },
                        "doc_count": 1,
                        "key": 1579003200000,
                        "key_as_string": "2020-01-14T00:00:00.000-12:00"
                    },
                    {
                        "cumulative_growth": {
                            "value": 4
                        },
                        "doc_count": 3,
                        "key": 1579089600000,
                        "key_as_string": "2020-01-15T00:00:00.000-12:00"
                    }
]

```

So, instead of getting 30 buckets, I am receving 31 buckets.

While debugging, I noticed this behavior happening for the given date range only.

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 19, 2020, 2:59pm UTC](https://discuss.elastic.co/t/date-histogram-returning-wrong-number-of-buckets/219702/2 "2020-02-19T14:59:13Z")

</div>

Hey,

is it possible to simplify and remove some variables from this sample and also share the full output as well as the Elasticsearch version you are working with?

First, move the filter agg range to the query range (also note that this date is UTC).  
Second, remove the pipeline agg.

if the response is still not what you expect, can you put the request+response in a gist and link it?

Thanks!

---

<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:** [March 18, 2020, 2:59pm UTC](https://discuss.elastic.co/t/date-histogram-returning-wrong-number-of-buckets/219702/3 "2020-03-18T14:59:28Z")

</div>

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