# Convert rounding logic to flux

**URL:** <https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438>\
**Category:** Dashboards\
**Created:** [September 19, 2024, 4:01pm UTC](https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438 "2024-09-19T16:01:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSG](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ssg/32/16453_2.png) [@SSG](https://community.influxdata.com/u/SSG)\
**Post date:** [September 19, 2024, 4:01pm UTC](https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438/1 "2024-09-19T16:01:08Z")

</div>

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”)

---

<div class="post-metadata">

**Author:** ![SSG](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ssg/32/16453_2.png) [@SSG](https://community.influxdata.com/u/SSG)\
**Post date:** [September 19, 2024, 6:01pm UTC](https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438/2 "2024-09-19T18:01:24Z")

</div>

@Anaisdg : Could you please help me on this.

---

<div class="post-metadata">

**Author:** ![SSG](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ssg/32/16453_2.png) [@SSG](https://community.influxdata.com/u/SSG)\
**Post date:** [September 20, 2024, 2:10pm UTC](https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438/3 "2024-09-20T14:10:30Z")

</div>

Any immediate help would be appreciated.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [September 22, 2024, 10:54am UTC](https://community.influxdata.com/t/convert-rounding-logic-to-flux/37438/4 "2024-09-22T10:54:53Z")

</div>

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`
