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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
POSPOS
Post Patron
Post Patron

Average and Median DAX calculating incorectly

Hi All,

I have a requirement to calculate the average and median between two dates. 
Issue: Median is taking blank records when calculating.

Sample date:

Protocol NumberStartEnd
19-040206/24/201908/12/2019
19-050208/07/201910-23-2019
22-050202/06/201903/08/2023
22-050311/02/2022 
   

 

I have created a column to calculate the datediff

 

 

 

 

DATEDIFF(start,end,day)

 

 

 

 

Protocol NumberStartEndDatediff
19-040206/24/201908/12/201949
19-050208/07/201910-23-201957
22-050202/06/202303/08/202330
22-050311/02/2022  


Then, I created another measure to display average and median in "Avg/Median" format

 

 

 

 

Avg/Median = 
VAR a= AVERAGEX(
    VALUES('Avg'[Protocol Number]),
    CALCULATE(AVERAGE('Avg'[Datedifference])))
VAR b=MEDIANX(
    VALUES('Avg'[Protocol Number]),
    CALCULATE(MEDIAN('Avg'[Datedifference])))
RETURN 
ROUND(a,0)&" / "& ROUND(b,0)

 

 

 

 


As there is no end date for the the last record in the table, the datediff is blank.
Issue:  median is getting calculate based on 4 records instead of three records. 
Average  calcualtion is showing correctly for three records.
49+57+30=136 : Avg=136/3 = 45
Whereas the median is being calculated for 4 records. 0,30,49,57 = (30+49)/2=40

I would like the median also to be calculated for three records excluding blank record i.e., 30,49,57.
Expected result is 49 but DAX is giving 40.

Can someone please suggest on how to fix this issue. Also attaching here sample pbix file in the link. 
https://drive.google.com/file/d/1UkRN8IMLpsXRFSzR58s_tUbg1Bd-bsDY/view
Thank you.

1 ACCEPTED SOLUTION
rameezPatel
Frequent Visitor

Hi @POSPOS ,

 

try the following

Avg/Median =
VAR a= AVERAGEX(
    VALUES('sample'[Protocol Number]),
    CALCULATE(AVERAGE('sample'[Datediff])))
VAR b=
MEDIANX(
    SUMMARIZE(FILTER('sample',NOT(ISBLANK('sample'[Datediff]))),'sample'[Protocol Number],"mid",median('sample'[Datediff])),[mid]
  )  
RETURN
ROUND(a,0)&" / "&ROUND(b,0)
 
 
main different with average and Median is that average ignores blanks and Median doesnt.
so you need to filter out the blanks in your dax, 
so this this work for you .
If this approach suits your needs, kindly show your support by voting. 😊

View solution in original post

5 REPLIES 5
rameezPatel
Frequent Visitor

Hi @POSPOS ,

 

try the following

Avg/Median =
VAR a= AVERAGEX(
    VALUES('sample'[Protocol Number]),
    CALCULATE(AVERAGE('sample'[Datediff])))
VAR b=
MEDIANX(
    SUMMARIZE(FILTER('sample',NOT(ISBLANK('sample'[Datediff]))),'sample'[Protocol Number],"mid",median('sample'[Datediff])),[mid]
  )  
RETURN
ROUND(a,0)&" / "&ROUND(b,0)
 
 
main different with average and Median is that average ignores blanks and Median doesnt.
so you need to filter out the blanks in your dax, 
so this this work for you .
If this approach suits your needs, kindly show your support by voting. 😊

@rameezPatel 
Could you also please suggest on how we can write similar code for median to ignore blank records when the dates are from two different tables and the datediff is in the first table. Now I want to write this code with Table 2 as primary table.

Sample date is below - 
Table1:

Protocol NoDate1Datediff
1906/24/201949
2008/07/201957
2102/06/202330


Table2:

Protocol NoDate 2
1908/12/2019
2010/23/2019
2103/08/2023
2211/02/2022

 

 

@rameezPatel  Thank you for helping fix the issue. I am now getting expected results.

POSPOS
Post Patron
Post Patron

@ryan_mayu 
Either the start date or the end date could be blank . Logic for Average is working but median values are incorrect.
I did attach an updated pbix file a while ago with better data. Could you please help with that to fix the issue.
I have use the below code but does not give correct result for median.

Avg/Median = 
VAR a= AVERAGEX(
    VALUES('sample'[Protocol Number]),
    CALCULATE(AVERAGE('sample'[Datediff])))
VAR b=MEDIAN('sample'[Datediff])
RETURN 
ROUND(a,0)&" / "& ROUND(b,0)

 

ryan_mayu
Super User
Super User

@POSPOS 

maybe you can try this

Datediff = if(ISBLANK('Table'[End]),0,DATEDIFF('Table'[Start],'Table'[End],DAY))

avg = AVERAGEX(FILTER('Table','Table'[Datediff]<>0),'Table'[Datediff])

median = MEDIAN('Table'[Datediff])

pls see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.