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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
magnusks
Helper I
Helper I

Max value in column based on distinct count another column

Hi,

 

I have a table consisting of title of projects and the principal investigator (PI) for those projects. The projects in this table are projects that have applied for external funding. Due to this, some of the projects are listed several times, thus I cannot remove duplicates in the power query since I need them for others measures (i.e. number of applications sent for external funding). 

 

Lets get back to the problem at hand. I want to calculate which "PI" has the most number of "projects".

 

Table name: Allprojects

 

ProjectPI
AAnna
BAnna
AAnna
CJohn
DMike
EElsa

 

I have tried this measure (see below), but it returns that Anna has 3 projects. But as you can see, she only has 2 distinct ones. I want the output to be: Anna: 2

 

Mostprojects PI =
var _table=
SUMMARIZE(
    'Allprojects','Allprojects'[PI],
    "Count",COUNTX(FILTER(ALL('Allprojects'),'Allprojects'[PI]=MAX('Allprojects'[PI])),'Allprojects'[PI]))
var _table2=
FILTER(
    _table,[Count]=MAXX(_table,[Count]))
return
    MAXX(_table2,'Allprojects'[PI])&": "&MAXX(_table2,[Count] )

 

Thanks

 

1 ACCEPTED SOLUTION

hi @magnusks 

 

Reposting. This is a better solution.

 

PI Name with max Count = No matter what columns you add remove. It will always give you PI with max distinct count along with distincg count. You can modify the RETURN statement to return one or the other if you want.
 
 
PI Name with max Count =

VAR _Summ =
ADDCOLUMNS(
            ALL(TestTbl4[PI]),
            "@DCountProject",
            VAR _PI = [PI]
            RETURN CALCULATE(DISTINCTCOUNT(TestTbl4[Project]), REMOVEFILTERS(TestTbl4), TestTbl4[PI] = _PI)
)
VAR _TOP = TOPN(1, _Summ, [@DCountProject], DESC, [PI], ASC)

RETURN SELECTCOLUMNS( _TOP, "@PI", [PI])&" - "&SELECTCOLUMNS( _TOP, "@DistinctCount", [@DCountProject])
 
talespin_0-1707380384700.png

 

View solution in original post

8 REPLIES 8
v-weiyan1-msft
Community Support
Community Support

Hi @magnusks ,

 

@talespin nice method! And based on the sample and description you provided, you may also consider using the following code.

Mostprojects PI = 
var _table=
SUMMARIZE(
    'Table','Table'[PI],
    "Count",CALCULATE(DISTINCTCOUNT('Table'[Project]),ALLEXCEPT('Table','Table'[PI])))
var _table2=
FILTER(
    _table,[Count]=MAXX(_table,[Count]))
return
    MAXX(_table2,'Table'[PI])&": "&MAXX(_table2,[Count] )

Result is as below.

vweiyan1msft_0-1707382667207.png

 

Best Regards,
Yulia Yan

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

magnusks
Helper I
Helper I

hi @talespin 

 

Thanks. I tried the dax you suggested, but it only returns the no of distinct projects. I want it to also dispay the name of the PI with the most distinct prosjects. 

 

Thanks

hi @magnusks ,

 

Please share what result you expect, This measure works for both visuals in screenshot. The count is at PI level.

talespin_0-1707309420298.png

 

Hi @talespin 

 

Im expecting a result that allows me to create a card that looks something like this

Anna.JPG

 

(But with 2 instead of 3).


Thanks

hi @magnusks 

 

Reposting. This is a better solution.

 

PI Name with max Count = No matter what columns you add remove. It will always give you PI with max distinct count along with distincg count. You can modify the RETURN statement to return one or the other if you want.
 
 
PI Name with max Count =

VAR _Summ =
ADDCOLUMNS(
            ALL(TestTbl4[PI]),
            "@DCountProject",
            VAR _PI = [PI]
            RETURN CALCULATE(DISTINCTCOUNT(TestTbl4[Project]), REMOVEFILTERS(TestTbl4), TestTbl4[PI] = _PI)
)
VAR _TOP = TOPN(1, _Summ, [@DCountProject], DESC, [PI], ASC)

RETURN SELECTCOLUMNS( _TOP, "@PI", [PI])&" - "&SELECTCOLUMNS( _TOP, "@DistinctCount", [@DCountProject])
 
talespin_0-1707380384700.png

 

Thank you - that worked 👍

@magnusks 

 

You're welcome.

talespin
Solution Sage
Solution Sage

hi @magnusks 

 

Please use this.

 

PI Distinct Count =
VAR _PI = SELECTEDVALUE( TestTbl4[PI])
RETURN CALCULATE( DISTINCTCOUNT(TestTbl4[Project]), REMOVEFILTERS(TestTbl4[Project]) )

Helpful resources

Announcements
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.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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