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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Limerick
Frequent Visitor

RANKX with FILTER, EARLIER in a Group of Data

Hello together, 

 

im trying to create a RANKX over a few orders that are sorted by Date. 
The Raw Data looks like this:

Customer     Order-Number     Date     
A1a2b3c01.01.1999
A1a2b3c01.01.2000
A2a2b3c01.01.2000
A3a2b3c01.01.2000
B1a2b3c01.01.2000
B2a2b3c01.01.2001
B3a2b3c01.01.2001
B3a2b3c01.01.2001

 

The result i need looks like this:

Customer     Order-Number     Date     RANK     
A1a2b3c01.01.1999   1
A1a2b3c01.01.1999   1
A2a2b3c01.01.2000   2
A3a2b3c01.01.2000   3
B1a2b3c01.01.2000   1
B2a2b3c01.01.2001   2
B3a2b3c01.01.2001   3
B3a2b3c01.01.2001   3

The ranking starts again for every customer and is depending on the order-number in combination with the date.

I have written this code: 

Ranking = RANKX(
FILTER(ALL('Table'),
'Table'[Customer] = EARLIER('Table'[Customer]) ,
'Table'[Date],,ASC,Dense)

The Result looks like this:
Customer   Order-Number   Date      RANK
A   1a2b3c   01.01.1999      1
A   1a2b3c   01.01.1999      1
A   2a2b3c   01.01.2000      2
A   3a2b3c   01.01.2000      2
B   1a2b3c   01.01.2000      1
B   2a2b3c   01.01.2001      2
B   3a2b3c   01.01.2001      2
B   3a2b3c   01.01.2001      2

So its pretty close to the wished result.
It is Ranking for each Customer by the Date. 
But now i need to rank it over the Date and the Order-Number.

So i tried this:
Ranking = RANKX(
FILTER(ALL('Table'),
'Table'[Customer] = EARLIER('Table'[Customer]
&& 'Table'[Order-Number] = 'Table'[Order-Number]) ,
'Table'[Date],,ASC,Dense)
 
But unfortunately it doesnt have any effect on the result - its still looking like the last table above.

For my understanding, the Filter is grapping with EARLIER the first Customer and is continuing as long as the Customer are identical. I also would need that for the Order-Number, so I try to say "do it for the Order-Number that are equal".
I cant use the EARLIER on the Order-Number, because then the RANKX is ranking every row with 1.

The table i need should look like this:
Customer     Order-Number     Date     RANK     
A1a2b3c01.01.1999   1
A1a2b3c01.01.1999   1
A2a2b3c01.01.2000   2
A3a2b3c01.01.2000   3
B1a2b3c01.01.2000   1
B2a2b3c01.01.2001   2
B3a2b3c01.01.2001   3
B3a2b3c01.01.2001   3
Unfortunately I also cant Rank over the Order-Number, because its a randomly generated number.

I hope you guys can help me out 🙂 

Ty in advance!



1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Here is a column expression that shows one way to do it.

 

Rank =
VAR thiscustomer = Orders[Customer]
VAR thisorder = Orders[Order-Number]
VAR thisdate = Orders[Date]
VAR stringtorank =
    FORMAT ( thisdate"YYYYMMDD" ) & thisorder
VAR thiscustomertable =
    FILTER ( Orders, Orders[Customer] = thiscustomer )
VAR withstringstorank =
    ADDCOLUMNS (
        thiscustomertable,
        "cStringtoRank"FORMAT ( Orders[Date], "YYYYMMDD" ) & Orders[Order-Number]
    )
VAR result =
    RANKX ( withstringstorank, [cStringtoRank], stringtorank, aSC )
RETURN
    result

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

Here is a column expression that shows one way to do it.

 

Rank =
VAR thiscustomer = Orders[Customer]
VAR thisorder = Orders[Order-Number]
VAR thisdate = Orders[Date]
VAR stringtorank =
    FORMAT ( thisdate"YYYYMMDD" ) & thisorder
VAR thiscustomertable =
    FILTER ( Orders, Orders[Customer] = thiscustomer )
VAR withstringstorank =
    ADDCOLUMNS (
        thiscustomertable,
        "cStringtoRank"FORMAT ( Orders[Date], "YYYYMMDD" ) & Orders[Order-Number]
    )
VAR result =
    RANKX ( withstringstorank, [cStringtoRank], stringtorank, aSC )
RETURN
    result

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thank you, this is perfekt!
Just had to add DENSE behinde the ASC but nice solution 🙂

amitchandak
Super User
Super User

@Limerick , Try like

Ranking = RANKX(
FILTER(ALL('Table'),
'Table'[Customer] = EARLIER('Table'[Customer]) ) ,
'Table'[Order-Number],,ASC,Dense)

Hello @amitchandak ,
thank you for your answer.
Unfortunately, as I mentioned before, I cant Rank by the Order-Number itself.
It is important that the order goes by the date.
Best regards 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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