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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

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
Anonymous
Not applicable

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

Anonymous
Not applicable

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.

Anonymous
Not applicable

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.