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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
TheJaks
Regular Visitor

confused by Rankx

hi all

 

in my table Payments, I'm trying to rank the invoices per PolicyID, such that the very oldest invoice is ranked 1, the 2nd 2 and so on. The only thing I'd like to do in addition, is not rank those rows where there isn't an invoice date yet (blank). I am trying the following code:

 

InvoiceRank =
RANKX (
    FILTER (
        Payments,
        NOT(ISBLANK(Payments[ActInvoiceDate])) && Payments[PolicyID] = EARLIER(Payments[PolicyID])
    ),
    Payments[ActInvoiceDate],
    ,
    ASC,
    Dense
)
 
This still ranks the blank invoices.
Any ideas why this may not give the expected results? Thanks
 
EDIT
 
I think this seems to work, not 100% sure I understand why 😄
 
IF (
    NOT(ISBLANK(Payments[ActInvoiceDate])), RANKX (
    FILTER (
        Payments,
         Payments[PolicyID] = EARLIER(Payments[PolicyID])  && NOT(ISBLANK(Payments[ActInvoiceDate]))
    ),
    Payments[ActInvoiceDate],
    ,
    ASC,
    Dense
))
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks @lbendlin 

Hi, @TheJaks 

I think you can understand along the lines below:

First, in your first expression, you try to exclude with Payments[ActInvoiceDate] as null, and then calculate the rank:

vjianpengmsft_0-1713753383953.png

In a calculated column, rankx sorts the entire table. The filter in Rank restricts the sorting context and does not remove the actual blank rows from the table. So in my case, the first 3 rows get the correct sorting. However, rows with an empty ActInvoiceDate are still sorted.

When I delete the mouse selection condition in column2, the sorting will change, which is a good illustration of the use of filter in Rankx to limit the context of sorting:

vjianpengmsft_1-1713753728981.png

vjianpengmsft_2-1713753829777.png

The reason why your second expression works is that you used the IF function to check if ActInvoiceDate is empty, sort if it is not empty, otherwise it will show empty. Here, your rankx is sorting for rows where ActInvoiceDate is not empty, and the context of the sort depends on the context of the filter restriction in Rankx. That's why this code works the way you intended.

 

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thanks @lbendlin 

Hi, @TheJaks 

I think you can understand along the lines below:

First, in your first expression, you try to exclude with Payments[ActInvoiceDate] as null, and then calculate the rank:

vjianpengmsft_0-1713753383953.png

In a calculated column, rankx sorts the entire table. The filter in Rank restricts the sorting context and does not remove the actual blank rows from the table. So in my case, the first 3 rows get the correct sorting. However, rows with an empty ActInvoiceDate are still sorted.

When I delete the mouse selection condition in column2, the sorting will change, which is a good illustration of the use of filter in Rankx to limit the context of sorting:

vjianpengmsft_1-1713753728981.png

vjianpengmsft_2-1713753829777.png

The reason why your second expression works is that you used the IF function to check if ActInvoiceDate is empty, sort if it is not empty, otherwise it will show empty. Here, your rankx is sorting for rows where ActInvoiceDate is not empty, and the context of the sort depends on the context of the filter restriction in Rankx. That's why this code works the way you intended.

 

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

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

lbendlin
Super User
Super User

Are Policy IDs temporally sorted?  Can you rank by dates instead?

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.