# Using "Math" aggregation and "Group by filter" together in Visual Builder

**URL:** <https://discuss.elastic.co/t/using-math-aggregation-and-group-by-filter-together-in-visual-builder/161127>\
**Category:** Kibana\
**Created:** [December 17, 2018, 11:12am UTC](https://discuss.elastic.co/t/using-math-aggregation-and-group-by-filter-together-in-visual-builder/161127 "2018-12-17T11:12:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicpav](https://avatars.discourse-cdn.com/v4/letter/v/9de053/32.png) [@vicpav](https://discuss.elastic.co/u/vicpav)\
**Post date:** [December 17, 2018, 11:12am UTC](https://discuss.elastic.co/t/using-math-aggregation-and-group-by-filter-together-in-visual-builder/161127/1 "2018-12-17T11:12:58Z")

</div>

I have the data coming in the format (simplified for clarity):

```auto
{ 
   "@timestamp": date,
   "order": {
      "status": string,
      "price": float
   }
}

```

Now, I want to make a time series plot that sums up the `order.price` separately for `order.status: completed` and `order.status: cancelled`, but the latter should be plotted as a negative value.

Due to the fact that the graph is shared with other metrics, I am forced to use the right y-axis. The plan is to have just one axis for `order.status: completed` and `order.status: cancelled` plotting the values above and below the x-axis, respectively. That is why I cannot split the data into two series or I'd get two y-axis.

It is the negative value for `order.status: cancelled` that I am struggling with. The series stricture:

- Aggregation (Sum of `order.price`)
- Group by (Filters `order.status: completed` / `order.status: cancelled`)  
produces the graph with two curves but both of positive values.

The series

- Aggregation (Sum of `order.price`)
- Aggregation (var `price`: Sum of order.price; Expression: `params.price * -1`)
- Group by (Filters `order.status: completed` / `order.status: cancelled`)  
makes both curves to be of negative values.

And I cannot pass as Math parameter anything but a number, i.e. I cannot make the if condition for `order.status` term inside the Math execution script.

---

<div class="post-metadata">

**Author:** ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)\
**Post date:** [December 20, 2018, 7:28pm UTC](https://discuss.elastic.co/t/using-math-aggregation-and-group-by-filter-together-in-visual-builder/161127/2 "2018-12-20T19:28:29Z")

</div>

You don't need to use the group by here. You can create two series. The one that you want to be negative you could use the math aggregation as you've suggested. Then, in the options, under the filter, use something like `order.status: cancelled`. In the other series, use `order.status: completed`. Let me know if that works!

---

<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:** [January 17, 2019, 7:28pm UTC](https://discuss.elastic.co/t/using-math-aggregation-and-group-by-filter-together-in-visual-builder/161127/3 "2019-01-17T19:28:34Z")

</div>

Closed on January 17, 2019, 7:28pm UTC

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