Convert rounding logic to flux

Can someone please help urgently -i have x and y columns. i need to round them to 2 decimal places and then group by rounded x and y values and count their occurences. this would give me a weight value. I am trying floowing but its not working - import “math”

start_date = 2024-09-10T00:00:00Z // Adjust as needed
end_date = 2024-09-11T23:59:59Z // Adjust as needed

data = from(bucket: “output”)
|> range(start: start_date, stop: end_date) // Define the time range
|> filter(fn: (r) => r._measurement == “test”)
|> filter(fn: (r) => r._field == “x” or r._field == “y”) // Focus on “x” and “y” fields

// Map to round x and y, and keep the _field and _value
rounded = data
|> map(fn: (r) => ({
r with
x_rounded: if r._field == “x” then math.round(x: r._value * 100.0) / 100.0 else 0.0,
y_rounded: if r._field == “y” then math.round(x: r._value * 100.0) / 100.0 else 0.0,
_value: r._value // Keep the original value for counting
}))

// Filter out rows where both rounded values are zero
filtered = rounded
|> filter(fn: (r) => r.x_rounded != 0.0 or r.y_rounded != 0.0)

// Group by rounded values of x and y, then count occurrences
result = filtered
|> group(columns: [“x_rounded”, “y_rounded”])
|> count(column: “_value”) // Count occurrences of each unique pair

result
|> yield(name: “weights”)

@Anaisdg : Could you please help me on this.

Any immediate help would be appreciated.

Hi @SSG

When you run your query, what results or errors do you get? You just said “but its not working”, which does not really help understand what is wrong.

Can you share a sample of your unfiltered data here in .CSV format so that others can try to run your query on it and help debug it or improve it?

Also, please use “Preformatted Text” when posting code here so it is easier for others to view, like this:
|> range(start: start_date, stop: end_date) // Define the time range