# Trying to get both min and max as part of window aggregation into a single value list

**URL:** <https://community.influxdata.com/t/trying-to-get-both-min-and-max-as-part-of-window-aggregation-into-a-single-value-list/31265>\
**Category:** Fluxlang\
**Created:** [August 23, 2023, 9:07pm UTC](https://community.influxdata.com/t/trying-to-get-both-min-and-max-as-part-of-window-aggregation-into-a-single-value-list/31265 "2023-08-23T21:07:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iptable](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/iptable/32/14171_2.png) [@iptable](https://community.influxdata.com/u/iptable)\
**Post date:** [August 23, 2023, 9:07pm UTC](https://community.influxdata.com/t/trying-to-get-both-min-and-max-as-part-of-window-aggregation-into-a-single-value-list/31265/1 "2023-08-23T21:07:21Z")

</div>

I have data returning a single series, so I have \_value and \_time. Now I need to aggregate by aggregate window. I am trying to find all outliers by aggregating using both min and max (so I want the min and max value for the aggregation). This will allow me to find spikes and drops when looking at graphs through time. I do not want multiple columns - one for min and one for max, I want a single column (so one line in grafana), making sure in each aggregation window I pick one min and one max and never miss the min and max values.

Example with min:

```auto
from(bucket: "aerolab")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
      r._measurement == "nsDevice" and
      r._field == "UsedBytes"
  )
  |> aggregateWindow(every: v.windowPeriod, fn: min)

```

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [August 24, 2023, 5:39pm UTC](https://community.influxdata.com/t/trying-to-get-both-min-and-max-as-part-of-window-aggregation-into-a-single-value-list/31265/2 "2023-08-24T17:39:39Z")

</div>

Hello @iptable,  
The following post could be helpful

> [@How to do multiple aggregation (min, max, mean) together using Flux query?](https://community.influxdata.com/t/how-to-do-multiple-aggregation-min-max-mean-together-using-flux-query/15527/2):
>
> Still getting my head around flux as well. I asked a similar question last week and Scott suggested this method to do it (and it’s been working well!)

---

<div class="post-metadata">

**Author:** ![iptable](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/iptable/32/14171_2.png) [@iptable](https://community.influxdata.com/u/iptable)\
**Post date:** [August 24, 2023, 11:31pm UTC](https://community.influxdata.com/t/trying-to-get-both-min-and-max-as-part-of-window-aggregation-into-a-single-value-list/31265/3 "2023-08-24T23:31:41Z")

</div>

Thank you. I got to this, which works (for whoever might be looking for/at this):

```auto
data = () => from(bucket: "aerolab")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "nsDevice" and
    r._field == "ReadBytesPerSecond" and 
    r.nodeIdent =~ /${nodeIdent:regex}/
  )

min = data()
  |> aggregateWindow(every: v.windowPeriod, fn: min)

max = data()
  |> aggregateWindow(every: v.windowPeriod, fn: max)

union(tables: [min, max])

```

The nodeIdent is for a multi-select filter in in grafana (dropdown variables). Now my problem is that, while this works, it produces 2x the number of datapoints that I should be producing (since I am picking 2 datapoints per window instead of one).

So a followup question, which would solve this for me, is there a way to make the aggregateWindow 2x the variable?

I tried the below, but that throws an error:

```auto
...
|> aggregateWindow(every: v.windowPeriod*2, fn: min)
...
|> aggregateWindow(every: v.windowPeriod*2, fn: max)
...

```

Side note for others who might find this thread, the reason data = is a function and not a result is because it works much faster this way. Something to do with pipeline optimisation.
