Smart M.App discussions

Looking for answers in regards to M.Apps, M.App Exchange or M.App Studio? Smart M.App Tech Discussion board is where you can search, ask your questions and assist others by sharing your knowledge. Join the conversation, connect, contribute and share.
Showing results for 
Search instead for 
Do you mean 
Reply
Frequent Contributor
Posts: 107
Registered: ‎10-16-2015

exclude null values

I've got this table that has null values for certain columns.

Does anyone know whether the $avg function skips null values or not?

Technical Evangelist
Posts: 1,144
Registered: ‎09-11-2015

Re: exclude null values

You will have to try that Robot wink

 

-In many libraries, frameworks, DB languages,...  the average functions usually ignore null or non-numeric values. But I cannot tell if that is the case with Smart M.Apps.

 

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Frequent Contributor
Posts: 107
Registered: ‎10-16-2015

Re: exclude null values

I've found out that null values are not skipped in $count, $avg, $ratio etc. I think it should be.

v.$avg = function (arg) {
    return v.$sum(arg) / v.$count;
};

the function $.avg( arg ) uses one 'global' $count, where it actually should have a count per field.

 

I usually use this code to create a non-null average reducer for a certain field

function createAvgReducer( dim, fld ) {

return dim.group().reduce(
                function (d, v) {
                    if( v[fld] != null) {
                        d.count++;
                        d.total += +v[fld];
                    }
                    return d;
                },
                function (d, v) {
                    if( v[fld] != null) {
                        d.count--;
                        d.total -= +v[fld];
                    }
                    return d;
                },
                function () {
                    return {
                        total: 0,
                        count: 0,
                        avg: function () {
                            return this.count ? this.total / this.count : null;
                        }
                    }
                }
            )
}