# Use Date Histogram with Two Periods

**URL:** https://discuss.elastic.co/t/use-date-histogram-with-two-periods/200929
**Category:** Elasticsearch
**Created:** [September 24, 2019, 8:10pm UTC](https://discuss.elastic.co/t/use-date-histogram-with-two-periods/200929 "2019-09-24T20:10:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![TomTom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomtom/32/3676_2.png) [@TomTom](https://discuss.elastic.co/u/TomTom)
#### Post date: [September 24, 2019, 8:10pm UTC](https://discuss.elastic.co/t/use-date-histogram-with-two-periods/200929/1 "2019-09-24T20:10:26Z")

</div>

Is it possible to use aggregations with date histogram to return multiple groups without having to perform the same search multiple times?

Example:

| Day of Week | Week 1 | Week 2 |
| --- | --- | --- |
| Monday | 120 | 125 |
| Tuesday | 110 | 100 |
| Wednesday | 200 | 220 |
| Thursday | 320 | 200 |
| Friday | 150 | 123 |
| Saturday | 1234 | 1231 |
| Sunday | 435 | 123 |

I was only able to do the grouping for a week, with the code below:

```
{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "account_id": {
                                        "value": 1234
                                    }
                                }
                            },
                            {
                                "term": {
                                    "finished": {
                                        "value": "Y"
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "range": {
                        "date_of_creation": {
                            "from": "2019-09-10T00:00:00-0000",
                            "to": "2019-09-24T23:59:59-0000",
                            "include_lower": true,
                            "include_upper": true
                        }
                    }
                }
            ]
        }
    },
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "group": {
            "date_histogram": {
                "field": "date_of_creation",
                "interval": "day",
                "format": "e"
            }
        }
    }
}
```

---

<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: [September 25, 2019, 11:59am UTC](https://discuss.elastic.co/t/use-date-histogram-with-two-periods/200929/2 "2019-09-25T11:59:18Z")

</div>

I think the correct way here would be to have a root level aggregation, that aggregates on the day of the week, which can be done with a terms aggregation using a script like `return doc['datetime'].value.getDayOfWeek();` - and then have a nested aggregation that is a date histogram based on weeks - with some hope thatI got your requirement right 🙂

Hope this helps.

---

<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: [October 23, 2019, 11:59am UTC](https://discuss.elastic.co/t/use-date-histogram-with-two-periods/200929/3 "2019-10-23T11:59:27Z")

</div>

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