# Query DSL for MoM, YoY, WoW calculation

**URL:** <https://discuss.elastic.co/t/query-dsl-for-mom-yoy-wow-calculation/113480>\
**Category:** Elasticsearch\
**Created:** [December 28, 2017, 4:38pm UTC](https://discuss.elastic.co/t/query-dsl-for-mom-yoy-wow-calculation/113480 "2017-12-28T16:38:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dheeran](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@dheeran](https://discuss.elastic.co/u/dheeran)\
**Post date:** [December 28, 2017, 4:38pm UTC](https://discuss.elastic.co/t/query-dsl-for-mom-yoy-wow-calculation/113480/1 "2017-12-28T16:38:23Z")

</div>

Hi,

I'm in situation to calculate MoM, YoY, WoW for a metric. I'm thinking of fetching all the docs and filtering in programming language. But I feel its not a right way. Can some one help me in forming Query DSL so that load will be on ES server.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 29, 2017, 8:42am UTC](https://discuss.elastic.co/t/query-dsl-for-mom-yoy-wow-calculation/113480/2 "2017-12-29T08:42:24Z")

</div>

I suspect you are looking for the date\_histogram aggregation? [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)

Your aggregation would look something like that for YoY:

```auto
POST /sales/_search?size=0
{
    "aggs" : {
        "YoY" : {
            "date_histogram" : {
                "field" : "my_date_field",
                "interval" : "1Y"
            },
            "aggs": {
              "metric_YoY": {
                "avg": {
                  "field": "my_metric_field"
                }
              }
            }
        }
    }
}

```

I used an average aggregation so that you can follow how the average value of the `my_metric_field` moves over time, but you can use any aggregation, eg. `sum`, `percentiles`, etc.

---

<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:** [January 26, 2018, 8:42am UTC](https://discuss.elastic.co/t/query-dsl-for-mom-yoy-wow-calculation/113480/3 "2018-01-26T08:42:30Z")

</div>

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