# DateHistogram offset does not work when empty buckets

**URL:** <https://discuss.elastic.co/t/datehistogram-offset-does-not-work-when-empty-buckets/88048>\
**Category:** Elasticsearch\
**Created:** [June 2, 2017, 9:35am UTC](https://discuss.elastic.co/t/datehistogram-offset-does-not-work-when-empty-buckets/88048 "2017-06-02T09:35:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![varpa89](https://avatars.discourse-cdn.com/v4/letter/v/96bed5/32.png) [@varpa89](https://discuss.elastic.co/u/varpa89)\
**Post date:** [June 2, 2017, 9:35am UTC](https://discuss.elastic.co/t/datehistogram-offset-does-not-work-when-empty-buckets/88048/1 "2017-06-02T09:35:30Z")

</div>

I'm trying to build date histogram for 3 months with interval by 7 days. I know that intervals are started from 1970-01-01 so I use the offset +2d. There is no data in this period, but my buckets start from 2016-12-29 because it is Thursday like 1970-01-01. I just want to my first bucket start from the first date of period  
Tested on elastic 5.2.2 and 5.4.0

The problem started from this [topic](https://discuss.elastic.co/t/confusing-datehistogram-behavior-buckets-starts-not-from-min-bound-date/87343)

**Query**

```
{
  "size" : 0,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "terms" : {
            "doc.accountNumber" : [
              "1234567890"
            ],
            "boost" : 1.0
          }
        },
        {
          "range" : {
            "doc.dateExecute" : {
              "from" : "2017-02-01",
              "to" : "2017-03-31",
              "include_lower" : true,
              "include_upper" : true,
              "time_zone" : "UTC",
              "boost" : 1.0
            }
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0,
      "_name" : "onDates"
    }
  },
  "aggregations" : {
    "time" : {
      "date_histogram" : {
        "field" : "doc.dateExecute",
        "format" : "yyyy-MM-dd",
        "time_zone" : "UTC",
        "interval" : "7d",
        "offset" : "+2d",
        "order" : {
          "_key" : "asc"
        },
        "keyed" : false,
        "min_doc_count" : 0,
        "extended_bounds" : {
          "min" : "2017-01-01",
          "max" : "2017-03-31"
        }
      },
      "aggregations" : {
        "incomingAmount" : {
          "sum" : {
            "field" : "doc.incomingAmount"
          }
        },
        "outgoingAmount" : {
          "sum" : {
            "field" : "doc.outgoingAmount"
          }
        }
      }
    }
  }
}

```

**Result**

```
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "time": {
      "buckets": [
        {
          "key_as_string": "2016-12-29",
          "key": 1482969600000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-01-05",
          "key": 1483574400000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-01-12",
          "key": 1484179200000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-01-19",
          "key": 1484784000000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-01-26",
          "key": 1485388800000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-02-02",
          "key": 1485993600000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-02-09",
          "key": 1486598400000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-02-16",
          "key": 1487203200000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-02-23",
          "key": 1487808000000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-03-02",
          "key": 1488412800000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-03-09",
          "key": 1489017600000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-03-16",
          "key": 1489622400000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-03-23",
          "key": 1490227200000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        },
        {
          "key_as_string": "2017-03-30",
          "key": 1490832000000,
          "doc_count": 0,
          "incomingAmount": {
            "value": 0
          },
          "outgoingAmount": {
            "value": 0
          }
        }
      ]
    }
  }
}
```

---

<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:** [June 30, 2017, 9:35am UTC](https://discuss.elastic.co/t/datehistogram-offset-does-not-work-when-empty-buckets/88048/2 "2017-06-30T09:35:39Z")

</div>

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