# Difference between two aggregations in Kibana. Error

**URL:** <https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988>\
**Category:** Kibana\
**Created:** [May 29, 2020, 9:27pm UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988 "2020-05-29T21:27:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vdalvi](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@vdalvi](https://discuss.elastic.co/u/vdalvi)\
**Post date:** [May 29, 2020, 9:27pm UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988/1 "2020-05-29T21:27:05Z")

</div>

Hi,

I am trying to perform simple difference between the values in two aggregations but unable to do so...

Kibana Query

```auto
{
  "_source": false,
  "query": {
    "range": {
      "timestamp": {
        "gte": "now-90m/m",
        "lte": "now"
      }
    }
  },
  "aggs": {
    "baselineCmtsCount": {
      "cardinality": {
        "field": "cmtsId"
      }
    },
    "every45min": {
      "date_histogram": {
        "field": "timestamp",
        "fixed_interval": "45m",
        "order": {
          "_key": "asc"
        }
      },
      "aggs": {
        "cmtsPer45min": {
          "cardinality": {
            "field": "cmtsId"
          }
        }
      }
    },
    "cmtsDerivative": {
      "bucket_script": {
        "buckets_path": {
          "var1": "baselineCmtsCount.value",
          "var2": "every45min.buckets.1.cmtsPer45min.value"
        },
        "script": "params.var1 - params.var2"
      }
    }
  }
}

```

Is bucket\_script correct or what is wrong ? Please help 🙂

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [May 30, 2020, 5:06am UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988/2 "2020-05-30T05:06:28Z")

</div>

> [@vdalvi](#):
>
> `cmtsDerivative`

Hi, Elasticsearch provides a `derivative` aggregation: [Derivative aggregation | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-derivative-aggregation.html)

Hopefully using that aggregation type will work for you 🙂

-Tim

---

<div class="post-metadata">

**Author:** ![vdalvi](https://avatars.discourse-cdn.com/v4/letter/v/54ee81/32.png) [@vdalvi](https://discuss.elastic.co/u/vdalvi)\
**Post date:** [May 30, 2020, 6:23pm UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988/3 "2020-05-30T18:23:15Z")

</div>

Hi Tim,

I believe the derivative aggregation would work within the same aggregation ? I want to perform the difference between values of two separate aggregations:

1st agg : "baselineCmtsCount"  
2nd agg : "every45min"

As per my original code I tried to do this using a script and getting the below error

```
  {
    "type": "aggregation_execution_exception",
    "reason": "Invalid pipeline aggregation named [cmtsDerivative] of type [bucket_script]. Only sibling pipeline aggregations are allowed at the top level"
  },
```

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [June 9, 2020, 5:35pm UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988/4 "2020-06-09T17:35:28Z")

</div>

Hi, since this is not a Kibana topic, would you mind asking in [https://discuss.elastic.co/c/elastic-stack/elasticsearch/6](https://discuss.elastic.co/c/elastic-stack/elasticsearch/6) to talk about this issue with the Elasticsearch experts? I want to make sure you get the best help.

Thanks,  
-Tim

---

<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:** [July 7, 2020, 5:35pm UTC](https://discuss.elastic.co/t/difference-between-two-aggregations-in-kibana-error/234988/5 "2020-07-07T17:35:34Z")

</div>

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