telerik / kendo-react

Issue tracker - KendoReact http://www.telerik.com/kendo-react-ui/
https://kendo-react-teal.vercel.app
Other
212 stars 37 forks source link

[Bug][Enhancement][Grid][kendo-data-query] Average aggregate is calculated incorrectly when null values are present #2151

Open fnkear opened 6 months ago

fnkear commented 6 months ago

I'm submitting a...

Current behavior

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:

System:

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
    };
},
zdravkov commented 5 months ago

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

fnkear commented 5 months ago

Hi @zdravkov ! Yes, your reproduction is much cleaner 👍 Thank you a lot!