Aggregate window with dynamic every based on total points

Is there any easy way to do a dynamic aggregatewindow based of total data points returned in a query. So something like the below.

from(bucket: “Bucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == “test”)
|> filter(fn: (r) => r["_field"] == “Value1” or r["_field"] == “value2”)
|>aggregateWindow(ever: if count(column:"_value") <= 10000 then 1m else 10s, fn mean())

@Eto This is possible, but it won’t be very efficient since the query has to read the data twice (once to count the number of rows and another to aggregate the data). To make sure the query is as optimized as possible, structure the base dataset as a function. This allows pushdowns to continue beyond the data variable declaration.

data = () => from(bucket: "Bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "test")
    |> filter(fn: (r) => r["_field"] == "Value1" or r["_field"] == "value2")

count = (data() |> group() |> count() |> findColumn(fn: (key) => true, column: "_value"))[0]
every = if count <= 10000 then 1m else 10s

data()
    |> aggregateWindow(every: every, fn: mean)