# How to calculate average off bucket valeus?

**URL:** https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241
**Category:** Elasticsearch
**Created:** [May 10, 2017, 12:20pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241 "2017-05-10T12:20:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Maiko\_Kuipers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maiko_kuipers/32/18104_2.png) [@Maiko\_Kuipers](https://discuss.elastic.co/u/Maiko_Kuipers)
#### Post date: [May 10, 2017, 12:20pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/1 "2017-05-10T12:20:26Z")

</div>

HI,

I Have the following problem. Here are my aggs.  
I now want to get the average of my just created agg: "tot\_kwh".  
Can someone explain me how to do this?

```
  "aggs": {
    "group_by_transaction_id": {
      "terms": {
        "field": "Transaction_ID"
      },
      "aggs": {
        "tot_kwh": {
          "sum": {
            "field": "DeltaMinutesPreviousRowWithinTransaction"
          }
      
    }
    }
  
}
}
```

---

<div class="post-metadata">

### Author: ![crickes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crickes/32/18009_2.png) [@crickes](https://discuss.elastic.co/u/crickes)
#### Post date: [May 10, 2017, 12:26pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/2 "2017-05-10T12:26:45Z")

</div>

Use the stats aggregation instead of sum, and it will produce the average value for you.

> "aggs": {  
> "group\_by\_transaction\_id": {  
> "terms": {  
> "field": "Transaction\_ID"  
> },  
> "aggs": {  
> "tot\_kwh": {  
> "stats": {  
> "field": "DeltaMinutesPreviousRowWithinTransaction"  
> }
> 
> }  
> }

---

<div class="post-metadata">

### Author: ![Maiko\_Kuipers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maiko_kuipers/32/18104_2.png) [@Maiko\_Kuipers](https://discuss.elastic.co/u/Maiko_Kuipers)
#### Post date: [May 10, 2017, 12:49pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/3 "2017-05-10T12:49:09Z")

</div>

Hi crickes,  
Thanks for your reply, but thats not exaclty what I meant. I currently have multiple buckets which have a value of "tot\_kwh" that I've calculated. And now I want to calculate the average of all those tot\_kwh values.  
I posted the reponse I'm getting from my current querie below to make things more clear.

```
"buckets": [
{
  "key": "1931709",
  "doc_count": 1038,
  "tot_kwh": {
    "value": 1036
  }
},
{
  "key": "1933523",
  "doc_count": 682,
  "tot_kwh": {
    "value": 682
  }
},
{
  "key": "1932252",
  "doc_count": 604,
  "tot_kwh": {
    "value": 2990
  }
}
```

---

<div class="post-metadata">

### Author: ![crickes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crickes/32/18009_2.png) [@crickes](https://discuss.elastic.co/u/crickes)
#### Post date: [May 10, 2017, 1:00pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/4 "2017-05-10T13:00:43Z")

</div>

Ah ok, then I think you need to be looking at the pipeline AVG Bucket aggregation.

[https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-pipeline-avg-bucket-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-pipeline-avg-bucket-aggregation.html)

Something similar to (not tested):

```
"aggs": {
    "group_by_transaction_id": {
      "terms": {
        "field": "Transaction_ID"
      },
      "aggs": {
        "tot_kwh": {
          "stats": {
            "field": "DeltaMinutesPreviousRowWithinTransaction"
          }
      },
      "avg_total_power": {
           "avg_bucket": {
                "buckets_path": "group_by_transaction_id>tot_kwh" 
            }
}
}
```

---

<div class="post-metadata">

### Author: ![Maiko\_Kuipers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maiko_kuipers/32/18104_2.png) [@Maiko\_Kuipers](https://discuss.elastic.co/u/Maiko_Kuipers)
#### Post date: [May 10, 2017, 1:29pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/5 "2017-05-10T13:29:47Z")

</div>

Hi crickes,

This seems to work thank you very much!

---

<div class="post-metadata">

### Author: ![crickes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/crickes/32/18009_2.png) [@crickes](https://discuss.elastic.co/u/crickes)
#### Post date: [May 10, 2017, 2:23pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/6 "2017-05-10T14:23:24Z")

</div>

Glad I could help.

---

<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 7, 2017, 2:24pm UTC](https://discuss.elastic.co/t/how-to-calculate-average-off-bucket-valeus/85241/7 "2017-06-07T14:24:08Z")

</div>

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