Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

How to calculate average value with null values

My query is related to handling null values in calculating the average at row level. Suppose we have 4 columns. COL1 COL2 COL3 COL4 11 12 11 NULL 24 22 NULL 33 44 NULL NULL 44 55 NULL NULL 55 M...
  • TomMartens's avatar
    8 years ago

    Hey,

     

    I used this DAX statement to create a "calculated column"

    the average = 
    var theTable = 
    UNION(
        ROW("value",'Table1'[Column1])
        ,ROW("value",'Table1'[Column2])
        ,ROW("value",'Table1'[Column3])
        ,ROW("value",'Table1'[Column4])
    )
    var theSum = 'Table1'[Column1] + 'Table1'[Column2] + 'Table1'[Column3] + 'Table1'[Column4] 
    var theDivisor = 
    COUNTROWS(
        FILTER(
            theTable
            ,[value] <> BLANK()
        )
    )
    return DIVIDE(theSum, theDivisor, BLANK())

    All the columns have a numeric data tape like decimal.

    I create a table from the columns that have to be considered using UNION(ROW(...),...)

    I create a simple sum from the values of in the columns that have to be considered

    I count the non empty rows in the table.

     

    Based on my sample data this will retrun these results:

     

    Hopefully this is what you are looking for,

     

    Regards,

    Tom