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
s--turn
Helper II
Helper II

Rank - but only if the rows meet criteria

Hello,

 

I am trying to achieve the results that I've added in red, in the final column.  i.e. I only want the rank to operate on rows that meet the criteria.  However, it seems to be operating on all rows, which is why in this example (filtered on a specific contactid) the ranking appears to start at 2. 

 

sturn_0-1745503696317.png

 

Is there a way to do this?  I have tried a few different techniques involving variables and virtual tables, but they never get anywhere!

 

Just to clarify what I'm trying to achieve, this is a table of memberships (membershipid) belonging to different contacts (contactid).  Many contacts appear more than once in the table, as they have a membership for each year.  I want to rank their memberships from earliest to latest - but I am only interested in the ones meeting my criteria of Grade = Graduate and Subgrade <> Free.  

 

Thank you in advance.

 

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @s--turn ,

 

Your current DAX measure is almost there but it explains why you're seeing the rank start from 2 — it ranks all rows using RANK, but the condition inside the IF merely hides the rank value for those that don’t qualify (like Grade = Graduate and Subgrade = "Free"). This doesn’t exclude them from the ranking engine — it just blanks them out after the rank is calculated.

To fix this and make the ranking only apply to rows that meet your condition, you need to build a filtered table inside RANKX rather than use IF outside. Here’s the corrected version using RANKX and FILTER, which will ensure the ranks start from 1 and only apply to qualifying rows:

Paying Graduate Rank = 
VAR CurrentContact = Memberships[contactid]
VAR FilteredTable =
    FILTER(
        ALL(Memberships),
        Memberships[contactid] = CurrentContact &&
        Memberships[Grade] = "Graduate" &&
        Memberships[Subgrade] <> "Free"
    )
RETURN
IF(
    Memberships[Grade] = "Graduate" && Memberships[Subgrade] <> "Free",
    RANKX(
        FilteredTable,
        Memberships[blc_validto],
        ,
        ASC,
        DENSE
    )
)

This will:

Only rank rows where the Grade is Graduate and Subgrade is not Free.

Rank them per contactid, so the order restarts for each person.

Prevent skipped numbers in rank, because only valid rows are passed into RANKX.

Your current RANK function with PARTITIONBY works similarly to RANKX + FILTER, but because it doesn’t exclude the "Free" subgrade row before ranking, the first result is 2. This fix ensures your rank will always start from 1 for eligible rows.

Let me know if you'd like the result to show "Not Ranked" or "-" for non-qualifying rows instead of leaving them blank.

 

Best regards,

View solution in original post

4 REPLIES 4
DataNinja777
Super User
Super User

Hi @s--turn ,

 

Your current DAX measure is almost there but it explains why you're seeing the rank start from 2 — it ranks all rows using RANK, but the condition inside the IF merely hides the rank value for those that don’t qualify (like Grade = Graduate and Subgrade = "Free"). This doesn’t exclude them from the ranking engine — it just blanks them out after the rank is calculated.

To fix this and make the ranking only apply to rows that meet your condition, you need to build a filtered table inside RANKX rather than use IF outside. Here’s the corrected version using RANKX and FILTER, which will ensure the ranks start from 1 and only apply to qualifying rows:

Paying Graduate Rank = 
VAR CurrentContact = Memberships[contactid]
VAR FilteredTable =
    FILTER(
        ALL(Memberships),
        Memberships[contactid] = CurrentContact &&
        Memberships[Grade] = "Graduate" &&
        Memberships[Subgrade] <> "Free"
    )
RETURN
IF(
    Memberships[Grade] = "Graduate" && Memberships[Subgrade] <> "Free",
    RANKX(
        FilteredTable,
        Memberships[blc_validto],
        ,
        ASC,
        DENSE
    )
)

This will:

Only rank rows where the Grade is Graduate and Subgrade is not Free.

Rank them per contactid, so the order restarts for each person.

Prevent skipped numbers in rank, because only valid rows are passed into RANKX.

Your current RANK function with PARTITIONBY works similarly to RANKX + FILTER, but because it doesn’t exclude the "Free" subgrade row before ranking, the first result is 2. This fix ensures your rank will always start from 1 for eligible rows.

Let me know if you'd like the result to show "Not Ranked" or "-" for non-qualifying rows instead of leaving them blank.

 

Best regards,

Thanks SO MUCH @DataNinja777 !  That has worked!  I really appeciate it 🙂 

lbendlin
Super User
Super User

Use the Relation parameter, with the prefiltered rows/aggregations that you want.

Thanks @lbendlin - I've tried that, but it says there is a circular dependency:

sturn_0-1745507202624.png

I've tried a few other ways of doing this, but nothing seems to be right!

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors