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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Median of distrubution i DAX

Dear community!

 

I want to calculate the median age of all cars in this this dataset but I can't get a grip of how to do it in DAX. It's like calculating the median of a distribution function.  For example: The answer here for Mazda is three months. (total number of Mazda+ 1)/2 = 262 which corresponds to three months (larger than 232 but less than 367). But I also need to do the calculation for all brands.

 

 I would guess the answer involves MedianX and Summarize but hopefully someone now how to put them togheter as well 🙂

 

BR Thomas

 

BrandAge of car in monthsNumber of carsCumulative number of Cars
Mazda0100100
Mazda150150
Mazda282232
Mazda3135367
Mazda420387
Mazda561448
Mazda675523
Kia032 
Kia141 
Kia280 
Kia3120 
Kia4130 
Kia550 

 

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below measures and the attached pbis file.

 

cumulate numbers : =
VAR currentbrand =
MAX ( Data[Brand] )
VAR currentmonth =
MAX ( Data[Age of car in months] )
RETURN
CALCULATE (
SUM ( Data[Number of cars] ),
FILTER (
ALL ( Data ),
Data[Brand] = currentbrand
&& Data[Age of car in months] <= currentmonth
)
)
 
Expected result : =
VAR targetnumber =
( SUM ( Data[Number of cars] ) + 1 ) / 2
VAR newtable =
FILTER (
ADDCOLUMNS (
SUMMARIZE ( data, Data[Age of car in months] ),
"@cumulatenumbers", [cumulate numbers :]
),
[@cumulatenumbers] > targetnumber
)
RETURN
IF (
HASONEVALUE ( Data[Brand] ),
MINX ( newtable, Data[Age of car in months] )
)
 
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

2 REPLIES 2
Jihwan_Kim
Super User
Super User

Hi,

Please check the below measures and the attached pbis file.

 

cumulate numbers : =
VAR currentbrand =
MAX ( Data[Brand] )
VAR currentmonth =
MAX ( Data[Age of car in months] )
RETURN
CALCULATE (
SUM ( Data[Number of cars] ),
FILTER (
ALL ( Data ),
Data[Brand] = currentbrand
&& Data[Age of car in months] <= currentmonth
)
)
 
Expected result : =
VAR targetnumber =
( SUM ( Data[Number of cars] ) + 1 ) / 2
VAR newtable =
FILTER (
ADDCOLUMNS (
SUMMARIZE ( data, Data[Age of car in months] ),
"@cumulatenumbers", [cumulate numbers :]
),
[@cumulatenumbers] > targetnumber
)
RETURN
IF (
HASONEVALUE ( Data[Brand] ),
MINX ( newtable, Data[Age of car in months] )
)
 
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Anonymous
Not applicable

Many thanks Jihwan_Kim, kudos!

I have tried it out and it works perfectly fine 👍
Just one adjustment, if you are to use it witout the column "brand" (i.e Median of all cars) you have to change:

IF (
HASONEVALUE ( Data[Brand] ),
MINX ( newtable, Data[Age of car in months] )
)
in measure "Expected result" to just: 
MINX ( newtable, Data[Age of car in months] )

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors