Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
klehar
Helper V
Helper V

Calculate average across columns excluding the blanks

Hi,

 

RollSciencePhysicsBiologyMaths
112141123
213151216
31416130
415null1417
5null17null17

 

I want to calculate the average across the columns(for each student/rollno)

But the caveat is : if there is a blank then that doesn't go in the average.

E.g.

For roll 1 it would be (12+14+11+23)/4

For roll 5 it would be (17+17)/2

For roll 4 it would be (15+14+17)/3

For roll 3 it would be (14+16+13+0)/4

 

How can i do this in dax?

Also note that, when the cells contain 0 it should be counted in the average but not when it is blank(roll 3)

1 ACCEPTED SOLUTION

Hi @klehar 

Try this code to create a calculated column.

 

Avg =
VAR _T =
    ADDCOLUMNS (
        'Table',
        "C_S", IF ( 'Table'[Science] <> BLANK (), 1, 0 ),
        "C_P", IF ( 'Table'[Physics] <> BLANK (), 1, 0 ),
        "C_B", IF ( 'Table'[Biology] <> BLANK (), 1, 0 ),
        "C_M", IF ( 'Table'[Maths] <> BLANK (), 1, 0 )
    )
VAR _Sum = 'Table'[Science] + 'Table'[Physics] + 'Table'[Biology] + 'Table'[Maths]
VAR _Count =
    SUMX (
        FILTER ( _T, [Roll] = EARLIER ( 'Table'[Roll] ) ),
        [C_S] + [C_P] + [C_B] + [C_M]
    )
VAR _Avg =
    DIVIDE ( _Sum, _Count )
RETURN
    _Avg

 

Result is as below.

1.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
NidhiBhusari
Helper IV
Helper IV

Hi @klehar 

In Power Query first do the unpivoting of the table

NidhiBhusari_1-1629268766232.png

Once done use the DAX , 

Col =

Var A = CALCULATE(SUM('Table'[Value]),GROUPBY('Table','Table'[Roll]))
Var B = A/5
return B


Output:-

NidhiBhusari_2-1629268822356.png

Let me know if its working for you and giving correct output.

 

Hi Nidhi,

 

I want the solution to be purely DAX

The reason is : In the actual data, all my subjects are calculated fields derived in DAX itself

Hi @klehar 

Try this code to create a calculated column.

 

Avg =
VAR _T =
    ADDCOLUMNS (
        'Table',
        "C_S", IF ( 'Table'[Science] <> BLANK (), 1, 0 ),
        "C_P", IF ( 'Table'[Physics] <> BLANK (), 1, 0 ),
        "C_B", IF ( 'Table'[Biology] <> BLANK (), 1, 0 ),
        "C_M", IF ( 'Table'[Maths] <> BLANK (), 1, 0 )
    )
VAR _Sum = 'Table'[Science] + 'Table'[Physics] + 'Table'[Biology] + 'Table'[Maths]
VAR _Count =
    SUMX (
        FILTER ( _T, [Roll] = EARLIER ( 'Table'[Roll] ) ),
        [C_S] + [C_P] + [C_B] + [C_M]
    )
VAR _Avg =
    DIVIDE ( _Sum, _Count )
RETURN
    _Avg

 

Result is as below.

1.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I have a similar issue, however there seems to be an error at the earlier function.

My data:

avb_140391_0-1668208710565.png

 

S_5or6 =
VAR _T =
    ADDCOLUMNS (
        'vw_QuestQuestionAnswersCategories',
        "[C5]", IF ('vw_QuestQuestionAnswersCategories'[5] <> 0,1,0),
        "[C6]", IF ('vw_QuestQuestionAnswersCategories'[6] <> 0,1,0)
    )

VAR _Sum = sum('vw_QuestQuestionAnswersCategories'[5]) + sum('vw_QuestQuestionAnswersCategories'[6])

VAR _Count =
SUMX(
    FILTER( _T,[Full_QuestionText] = EARLIER('vw_QuestQuestionAnswersCategories'[Full_QuestionText)),
    [C5] + [C6]
)    

VAR _Avg =
    DIVIDE ( _Sum, _Count )
RETURN
    _Avg

Hi,

In the Query Editor, you should select the first 2 columns, right click and select "Unpivot Other Columns".  Thereafter a simple average measure should work

Avg = average(Data[Value])

Hoep this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.