kendo-react
kendo-react copied to clipboard
[Bug][Enhancement][Grid][kendo-data-query] Average aggregate is calculated incorrectly when null values are present
I'm submitting a...
- Bug report
- Suggestion for improvement
Current behavior
- if there is
null
or other non-numeric value in the last row, result ofaverage
aggregate will benull
- if there are some
null
or other non-numeric value in rows between numeric values, result ofaverage
aggregate will be incorrect number
Expected behavior
The best would be if average
ignores non-numeric values in presence of numeric (and it can use last non-numeric if there were no numeric values at all)
Minimal reproduction of the problem with instructions
The sandbox ( forked from https://www.telerik.com/kendo-react-ui/components/grid/grouping/grouping/ )
https://stackblitz.com/edit/react-6oe2cf?file=app%2Fmain.jsx https://react-6oe2cf.stackblitz.io
Please check Average Unit Price for "Beverages" and "Condiments"
What is the motivation or use case for changing the behavior?
Sometimes data has items "with value" and "without value" mixed; but it still makes sense to calculate Average for items having value :)
Environment
Browser:
- Chrome (desktop) version 123.0.6312.106
- Firefox version 124.0.2
System:
- Platform: Windows 11
Suggested solution
the piece of code which I would suggest for "Average" aggregate:
average: () => {
let value = 0;
let count = 0;
let lastNonNumeric = undefined;
return {
calc: (curr) => {
if (isNumeric(curr)) {
value += curr;
count++;
} else {
lastNonNumeric = curr;
}
},
result: () => count ? value / count : lastNonNumeric
};
},
Hi @fnkear ,
Thank you for reporting the issue and suggesting a fix. I have isolated it to a '@progress/kendo-data-query' bug in this example - https://stackblitz.com/edit/react-6oe2cf-eewany?file=app%2Fmain.jsx and will suggest the fix to our responsible dev team for research.
If you have any other questions, you could also submit a ticket in our support system.
Greetings, Plamen
Hi @zdravkov ! Yes, your reproduction is much cleaner 👍 Thank you a lot!