Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreWe'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
Hi,
| Roll | Science | Physics | Biology | Maths |
| 1 | 12 | 14 | 11 | 23 |
| 2 | 13 | 15 | 12 | 16 |
| 3 | 14 | 16 | 13 | 0 |
| 4 | 15 | null | 14 | 17 |
| 5 | null | 17 | null | 17 |
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)
Solved! Go to 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.
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.
Hi @klehar
In Power Query first do the unpivoting of the table
Once done use the DAX ,
Output:-
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.
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:
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.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 53 | |
| 37 | |
| 35 | |
| 19 | |
| 17 |
| User | Count |
|---|---|
| 74 | |
| 70 | |
| 39 | |
| 35 | |
| 23 |