# How to add personnal aggs in Kibana

**URL:** <https://discuss.elastic.co/t/how-to-add-personnal-aggs-in-kibana/294691>\
**Category:** Kibana\
**Created:** [January 18, 2022, 12:26pm UTC](https://discuss.elastic.co/t/how-to-add-personnal-aggs-in-kibana/294691 "2022-01-18T12:26:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scorfly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scorfly/32/100470_2.png) [@Scorfly](https://discuss.elastic.co/u/Scorfly)\
**Post date:** [January 18, 2022, 12:26pm UTC](https://discuss.elastic.co/t/how-to-add-personnal-aggs-in-kibana/294691/1 "2022-01-18T12:26:57Z")

</div>

Hello !

I have doc for each of my product.  
My product have **serviceCreationDate** and **serviceTerminationDate** field.

i try to have the diff between service created and terminated. However i dont find a solution with Kibana.

Exemple ES request working :

```auto
{
  "aggs": {
    "4": {
      "date_histogram": {
        "field": "serviceCreationDate",
        "interval": "1w",
        "time_zone": "Europe/Berlin",
        "min_doc_count": 0
      },
      "aggs": {
        "test": {
            "bucket_script": {
                "buckets_path": {
                    "terminated": "7",
                    "activated": "6"
                },
                "script": "params.activated - params.terminated"
            }
        },
        "6": {
          "cumulative_sum": {
            "buckets_path": "6-metric"
          }
        },
        "7": {
          "cumulative_sum": {
            "buckets_path": "7-metric"
          }
        },
        "7-metric": {
          "sum_bucket": {
            "buckets_path": "7-metric-bucket>_count"
          }
        },
        "7-metric-bucket": {
          "date_histogram": {
            "field": "serviceTerminationDate",
            "interval": "1w",
            "time_zone": "Europe/Berlin",
            "min_doc_count": 1
          }
        },
        "6-metric": {
          "sum_bucket": {
            "buckets_path": "6-metric-bucket>_count"
          }
        },
        "6-metric-bucket": {
          "date_histogram": {
            "field": "serviceCreationDate",
            "interval": "1w",
            "time_zone": "Europe/Berlin",
            "min_doc_count": 1
          }
        }
      }
    }
  },
  "size": 0,
  "_source": {
    "excludes": []
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [
    {
      "field": "serviceCreationDate",
      "format": "date_time"
    },
    {
      "field": "serviceTerminationDate",
      "format": "date_time"
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        },
        {
          "match_all": {}
        },
        {
          "range": {
            "serviceCreationDate": {
              "gte": 1484734919956,
              "lte": 1642501319956,
              "format": "epoch_millis"
            }
          }
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  }
}
}

```

This give we the result i want  
However, i dont find any solution to add the "test" part with kibana ( 6.8.23 )

Do you have some suggestion ?

Best regards.

---

<div class="post-metadata">

**Author:** ![Scorfly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/scorfly/32/100470_2.png) [@Scorfly](https://discuss.elastic.co/u/Scorfly)\
**Post date:** [January 18, 2022, 1:28pm UTC](https://discuss.elastic.co/t/how-to-add-personnal-aggs-in-kibana/294691/2 "2022-01-18T13:28:01Z")

</div>

After hour of tests and research i found a solution withe timelion !

```auto
.es(index=index-name, timefield=serviceCreationDate).subtract(.es(index=index-name, timefield=serviceTerminationDate)).label('delta').cusum()

```

---

<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:** [February 15, 2022, 1:28pm UTC](https://discuss.elastic.co/t/how-to-add-personnal-aggs-in-kibana/294691/3 "2022-02-15T13:28:48Z")

</div>

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