# TIme series Visual builder : How to do Math calculations betweent the output of two series?

**URL:** https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414
**Category:** Kibana
**Created:** [February 16, 2021, 11:01am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414 "2021-02-16T11:01:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 16, 2021, 11:01am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/1 "2021-02-16T11:01:19Z")

</div>

Hi Team,

I am using Time series visual builder: Top N Chart, i am in a scenario where i am doing Math calculation in 5 different series. See below Screenshot:

Pic 1: Final Chart  
Pic 2: Different time series i have choosen  
Pic 3: 1 of the time series where i am doing math calculations.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ab734e74f97b08581b17ac163f5bb3dd1c5e726.png) ![Pic 2](https://us1.discourse-cdn.com/elastic/original/3X/d/8/d8e6679d4f923f7ae9e1c941dbb309a6a9d0d284.png) ![Pic 3](https://us1.discourse-cdn.com/elastic/original/3X/0/8/08b8948df330fee06142ce604e2e58c5d6b5eacd.png)

Question: Each of the series calculates the number of hours saved by a particular automation process, what i need is a way to do sum on the output of these 6 series so that i can get the total time saved by all processes.

As of now i cannot seem to find a way to do this in time series visual builder, can we do it in this chart or is this a functionality available in another chart?

Thank you  
Harpreet Singh

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [February 16, 2021, 11:11am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/2 "2021-02-16T11:11:41Z")

</div>

It's not possible to do cross-series calculations in TSVB. I don't know how your other series look like, but if they are similar, you might be able to collapse them all into one series.

Are you using different filters for each of those? If that's not the case, you can probably add all of the metric aggregations in a single series and then have one large "math" aggregation in the bottom doing all of the calculations at once.

---

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 16, 2021, 11:16am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/3 "2021-02-16T11:16:43Z")

</div>

Hi @flash1293

Yeah thats exactly the problem, all my series work on different filters hence different data set altogether, so clubbing them all in one wont be possible.

If this is not possible in TSVB, then can we do something like this in any other visualization type?

Let me elaborate a bit more on what i ma trying to do so that you get more perspective:  
I want to do 5 calculations(arithmetic equation is different for all 5) on 5 different filters(that gives 5 different dataset) and later i want to do a sum on the output of all previous 5.

I hope this clears it a bit more

Thank you  
Harpreet Singh

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [February 16, 2021, 12:21pm UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/4 "2021-02-16T12:21:48Z")

</div>

We are currently working on supporting these kinds of calculations in Lens, but it will be at least two minor versions till we get there.

Till then, I think you will have to fall back to Vega: [Learn to build rich Vega Visualizations in Kibana | Elastic Blog](https://www.elastic.co/blog/getting-started-with-vega-visualizations-in-kibana) It allows you to query Elasticsearch directly using the query DSL which allows these kinds of calulcations. A combination of [Bucket script aggregation | Elasticsearch Reference [7.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html) and [Filter aggregation | Elasticsearch Reference [7.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html) should do the trick.

---

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 16, 2021, 12:24pm UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/5 "2021-02-16T12:24:31Z")

</div>

Thanks @flash1293  
Appreciate it!!

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [February 16, 2021, 12:25pm UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/6 "2021-02-16T12:25:20Z")

</div>

Vega and Elasticsearch DSL are a large topic, feel free to update this thread if you get stuck somewhere.

---

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 18, 2021, 12:44pm UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/7 "2021-02-18T12:44:34Z")

</div>

Hi @flash1293 ,  
Thanks for the nudge towards Vega, i am going through the docs and it seems this might give us what we need.

While working with this i have stumbles upon an another issue, hope you can help.  
I have a scenario where i have to take the cardinality aggregation on an String field and then multiply the output of the cardinality field by an integer. See below request for reference: But seems like i cant do that in kibana, i have tried using the IntegerParse( ) methods too but no luck there as well. Any ideas how i can achive that?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/1/5135c00acb5244b29e74e2cc64bf66a4d8ba9671.png)

---

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 21, 2021, 3:29pm UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/8 "2021-02-21T15:29:12Z")

</div>

Hi @flash1293  
Can you help me with the above?

---

<div class="post-metadata">

### Author: ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)
#### Post date: [February 22, 2021, 8:21am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/9 "2021-02-22T08:21:11Z")

</div>

Hey, sorry for the late reply. This example should contain all the things you need, let me know whether it helps:

```auto
POST /*logs/_search
{
  "size": 0,
  "aggs": {
    "groups": {
      "filters": {
        "filters": {
          "all": {
            "match_all": {}
          }
        }
      },
      "aggs": {
        "US": {
          "filter": {
            "term": {
              "geo.src": "US"
            }
          },
          "aggs": {
            "uniqueips": {
              "cardinality": {
                "field": "clientip"
              }
            }
          }
        },
        "factored_ips": {
          "bucket_script": {
            "buckets_path": {
              "usUniqueIps": "US>uniqueips"
            },
            "script": "params.usUniqueIps * 2"
          }
        }
      }
    }
  }
}

```

It uses a separate bucket\_script agg to do the calculation on top of the cardinality aggregation. To do that, you what to wrap the whole thing into a "filters" agg

---

<div class="post-metadata">

### Author: ![Harpreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet_singh/32/83971_2.png) [@Harpreet\_Singh](https://discuss.elastic.co/u/Harpreet_Singh)
#### Post date: [February 22, 2021, 11:14am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/10 "2021-02-22T11:14:46Z")

</div>

Thanks again @flash1293!!

---

<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: [March 22, 2021, 11:15am UTC](https://discuss.elastic.co/t/time-series-visual-builder-how-to-do-math-calculations-betweent-the-output-of-two-series/264414/11 "2021-03-22T11:15:42Z")

</div>

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