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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
hansei
Helper V
Helper V

How to calculate a Weighted Mode

Using the following example table

UnitsUnit Price
1100
2300
1

100

3

300

1100

 

While getting the weighted mean of unit price is fairly trivial

 

SUMX( Table, [Units] * [Unit Price] ) / SUM( Table[Units)
= 1800 / 8
= 225

 


I am trying to figure out the weighted mode which should equal 300, and am at a loss. Please help 🙂

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Here's the long way of doing this:

 

Mode =
var a = ADDCOLUMNS(values('Table'[Unit Price]),"pr",'Table'[Unit Price])
var b= ADDCOLUMNS(a,"ct",CALCULATE(sum('Table'[Units]),filter('Table','Table'[Unit Price]=[pr])))
var c = topn(1,b,[ct],DESC)
return CONCATENATEX(c,[pr],",")
 
here's a slightly shorter version:
 
Mode =
var c = SUMMARIZE('Table','Table'[Unit Price],"ct",sum('Table'[Units]))
return CONCATENATEX(TOPN(1,c,[ct],DESC),'Table'[Unit Price])
 
and here is a really nice one:
 
Mode = LASTNONBLANK(SUMMARIZE('Table','Table'[Unit Price]),sum('Table'[Units]))

View solution in original post

5 REPLIES 5
amitchandak
Super User
Super User

@hansei , what is the logic for 300

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
lbendlin
Super User
Super User

Here's the long way of doing this:

 

Mode =
var a = ADDCOLUMNS(values('Table'[Unit Price]),"pr",'Table'[Unit Price])
var b= ADDCOLUMNS(a,"ct",CALCULATE(sum('Table'[Units]),filter('Table','Table'[Unit Price]=[pr])))
var c = topn(1,b,[ct],DESC)
return CONCATENATEX(c,[pr],",")
 
here's a slightly shorter version:
 
Mode =
var c = SUMMARIZE('Table','Table'[Unit Price],"ct",sum('Table'[Units]))
return CONCATENATEX(TOPN(1,c,[ct],DESC),'Table'[Unit Price])
 
and here is a really nice one:
 
Mode = LASTNONBLANK(SUMMARIZE('Table','Table'[Unit Price]),sum('Table'[Units]))

@lbendlin, thanks for that. Your third example does not actually do a weighted average, as summarized is unordered. But the second one is cleaner than what I had actually ended up using:

Mode = MINX(
    TOPN(
        1, 
        ADDCOLUMNS(
            VALUES( Table[Unit Price] ),
            "Frequency", 
            CALCULATE( SUM( Table[Units] ) )
        ), 
        [Frequency], 
        0        
    ), 
    Table[Unit Price]
)

 

That's actually a really good observation - I will need to investigate if SUMMARIZE() does any sorting or not. I thought it does 🙂

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.