# How to aggregate based on another aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947>\
**Category:** Elasticsearch\
**Created:** [March 17, 2022, 12:19pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947 "2022-03-17T12:19:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kosodrom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosodrom/32/99278_2.png) [@Kosodrom](https://discuss.elastic.co/u/Kosodrom)\
**Post date:** [March 17, 2022, 12:19pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/1 "2022-03-17T12:19:10Z")

</div>

Hi folks,

I have a certain amount of user transactions per day. My final goal is to have the **average of daily sums of transactions per week**.

What I have done so far is the first step (the easy one). I created a chart which shows me **sums of transactions per day** which looks like so:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/f/3f1976f558d4596c87e4f8b124d72362e30d91ac.png)

Now I want to achieve the second step: I want to have the **average of the sums per day for each 7 days** in another second chart. Do you have any hint how I can implement this ?

Thank you

---

<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:** [March 18, 2022, 12:25pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/2 "2022-03-18T12:25:54Z")

</div>

Take a look at pipeline aggregations: [Pipeline aggregations | Elasticsearch Guide [8.1] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.1/search-aggregations-pipeline.html)

If you are using Lens in Kibana, you could probably go with a moving average in there without leaning into the details of pipeline aggs?

---

<div class="post-metadata">

**Author:** ![Kosodrom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosodrom/32/99278_2.png) [@Kosodrom](https://discuss.elastic.co/u/Kosodrom)\
**Post date:** [March 18, 2022, 1:17pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/3 "2022-03-18T13:17:36Z")

</div>

Thanks for the response.

In Lense I tried to use moving avarage of sums of transactions but the results are still shown per day instead of required per week:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/8/387db9e81d0f2c90eda191d1b9d000c0b8c58c66.png)

If I could break ti somehow in 7 days chunks. That'd be great.

Regarding pipeline aggregations I will take a look on that. I think I tried similar approach with TSVB Visualization type, but there I could not find how to break it down per week

---

<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:** [March 18, 2022, 3:50pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/4 "2022-03-18T15:50:08Z")

</div>

When setting the horizontal axis, you can customize the time interval and set it to one week - would that work?

---

<div class="post-metadata">

**Author:** ![Kosodrom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosodrom/32/99278_2.png) [@Kosodrom](https://discuss.elastic.co/u/Kosodrom)\
**Post date:** [March 23, 2022, 1:26pm UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/5 "2022-03-23T13:26:13Z")

</div>

I this case the graph will show me the sum of the transactions per week. But what I need is:

1. sums of transactions per day and
2. The average of these sums for each 7 days

Basically I need two aggregations and I must use the output of the first one as input for the second one with different histogram interval. And I cannot find an approach to do that.

---

<div class="post-metadata">

**Author:** ![Kosodrom](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kosodrom/32/99278_2.png) [@Kosodrom](https://discuss.elastic.co/u/Kosodrom)\
**Post date:** [March 24, 2022, 8:34am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/6 "2022-03-24T08:34:48Z")

</div>

Alright, so the moving-avarage was exactly what I was looking for. I just misunderstood how the moving-average works and tried to break down the graph in weeks, but using moving-average with window 7 with daily interval it is not needed. Every day in the chart shows you the average of previous 7 days basically.

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:** [April 21, 2022, 8:34am UTC](https://discuss.elastic.co/t/how-to-aggregate-based-on-another-aggregation/299947/7 "2022-04-21T08:34:50Z")

</div>

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