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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Amrselim1989
Helper II
Helper II

DistinctCount OF Ranks

Hello Guys,

 

kindly note i have the below code which is working fine and gives me the rank i want..

 

Rank = VAR SourceTable = ADDCOLUMNS ( ALL ( Sales[ItemCode] ), "@Amt", [Sum of Net Sales Value] ) 
VAR
Result = RANK ( DENSE, SourceTable, ORDERBY ( [@Amt], DESC) )
RETURN
Result

but what i want to achieve is to distinct_count of the ranks itself, so i was thinking to create a variable table inside the measure, and then use distinct count to count the ranks something like this,

 

 Rank = ADDCOLUMNS ( ALL ( Sales[ItemCode] ), "@Amt", [Sum of Net Sales Value] )  
VAR
Result = RANK ( DENSE, SourceTable, ORDERBY ( [@Amt], DESC) )
VAR
_DisCount = ADDCOLUMNS(ALL ( Sales[ItemCode] ),"@Rank",Result)
VAR
_Dis_Count = distinctcount(_DisCount,[@Rank])
RETURN
Result & "of" _Dis_Count

so i can say that product is rank 2 of 25 total ranks,

i want it in the same measure not with a separated table outside, is it possible, i don't know why i can't use this variable column [Rank] like this ??!!!!

1 ACCEPTED SOLUTION

Hi,

Please check the below picture and the attached pbix file.

Jihwan_Kim_0-1688843265602.png

 

Distinct count of rank V2 =
VAR _SourceTable =
    ADDCOLUMNS ( ALL ( Sales[ItemCode] ), "@Amt", [Sum of Net Sales Value] )
VAR _currentrowrank =
    RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
VAR _t =
    SUMMARIZE (
        ADDCOLUMNS (
            ALL ( Sales[ItemCode] ),
            "@Rank", RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
        ),
        [@Rank]
    )
VAR _Dis_Count =
    COUNTROWS ( _t )
RETURN
    _Dis_Count

 

Another way to achieve this is, I think, try to get the last rank number in the rank result.



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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi, I am not sure what is the expected result, but I tried to create a sample pbix file like below.
Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1688802181985.png

 

 

Distinct count of rank = 
VAR _SourceTable =
    ADDCOLUMNS ( ALL ( Sales[ItemCode] ), "@Amt", [Sum of Net Sales Value] )
VAR _currentrowrank =
    RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
VAR _DisCount =
    FILTER (
        ADDCOLUMNS (
            ALL ( Sales[ItemCode] ),
            "@Rank", RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
        ),
        [@Rank] = _currentrowrank
    )
VAR _Dis_Count =
    COUNTROWS ( _DisCount )
RETURN
    _Dis_Count

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Hello sir,

thank you so much to reply me, but what i want to achieve is not that, it's actually so simple 

 

Amrselim1989_0-1688821121272.png

Amrselim1989_1-1688821417893.png

 

it's so easy to achieve with the calculated column, but i want it to be inside the measure, to follow other filtrations that may occur, is it possible??!!!

 

Amrselim1989_0-1688823047270.png

why i can't use this variable column i did just as i did it up there this is sooo frustrating!!! 

Hi,

Please check the below picture and the attached pbix file.

Jihwan_Kim_0-1688843265602.png

 

Distinct count of rank V2 =
VAR _SourceTable =
    ADDCOLUMNS ( ALL ( Sales[ItemCode] ), "@Amt", [Sum of Net Sales Value] )
VAR _currentrowrank =
    RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
VAR _t =
    SUMMARIZE (
        ADDCOLUMNS (
            ALL ( Sales[ItemCode] ),
            "@Rank", RANK ( DENSE, _SourceTable, ORDERBY ( [@Amt], DESC ) )
        ),
        [@Rank]
    )
VAR _Dis_Count =
    COUNTROWS ( _t )
RETURN
    _Dis_Count

 

Another way to achieve this is, I think, try to get the last rank number in the rank result.



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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Yes, Thank you so much, i just knew that i have to use the X aggregation function inside the variable tables to be able to use their column like MAXX instead of the distinct count...

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.