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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
s45kougo
Helper I
Helper I

Calculate median of aggregated data

Hi,

 

Hoping someone can help as I can't find any information on this...

 

I want to calculate the median price for some pre-aggregated data - I don't have access to the line-level data.

A simplified table looks like this:

Total SpendQuantityPrice
2555
20210
40104

This should give a median of 4.  But how do I write that calculation?

 

Thanks in advance

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@s45kougo - Here is a much more elegant solution, Page 32, PBIX Table (32) attached below sig.

Measure 32 = 
    VAR __Table =
        GENERATE(
        'Table (32)',
        VAR __Quantity = [Quantity]
        RETURN GENERATESERIES(1,__Quantity,1)
    )
RETURN
    MEDIANX(__Table,[Price])


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Community Champion
Community Champion

@s45kougo - Here is a much more elegant solution, Page 32, PBIX Table (32) attached below sig.

Measure 32 = 
    VAR __Table =
        GENERATE(
        'Table (32)',
        VAR __Quantity = [Quantity]
        RETURN GENERATESERIES(1,__Quantity,1)
    )
RETURN
    MEDIANX(__Table,[Price])


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Oh wow, you are amazing , thank you *so* much! This works perfectly. I thought it must be possible, but I had no idea how to disaggregate a table. Hope this helps someone else too in the future 🙂

Greg_Deckler
Community Champion
Community Champion

@s45kougo - There may be a more elegant solution and I will think about it but for this you could brute force it:

Measure = 
  VAR __Table25 = 
    ADDCOLUMNS(
      GENERATESERIES(1,MAXX(FILTER('Table',[Total Spend]=25),[Quantity]),1),
      "Price",MAXX(FILTER('Table',[Total Spend]=25),[Price])
    )
  VAR __Table20 = 
    ADDCOLUMNS(
      GENERATESERIES(1,MAXX(FILTER('Table',[Total Spend]=20),[Quantity]),1),
      "Price",MAXX(FILTER('Table',[Total Spend]=20),[Price])
    )
  VAR __Table40 = 
    ADDCOLUMNS(
      GENERATESERIES(1,MAXX(FILTER('Table',[Total Spend]=40),[Quantity]),1),
      "Price",MAXX(FILTER('Table',[Total Spend]=40),[Price])
    )
  VAR __Table = UNION(__Table25,__Table20,__Table40)
RETURN
  MEDIANX(__Table,[Price])


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

@s45kougo , In case this data is already aggregated. You can average

 

Avg = Divide(Sum(Table[Total Spend]),sum(Table[Quantity]))

Which will be five

You can try MEDIAN( Table[Price] ) 

But I doubt you can get 4

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak thanks for trying, but I do want the actual median of 4... I'm guessing that somehow I have to calculate a table that dis-aggregates, but not sure how to do this.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors