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
Anonymous
Not applicable

Use of RANKX() for further calculations

Hi all,

 

I have this table which comes sorted randomly:

 

DateProductBatchShift
27-04-2020Product 12N7JMorning
08-05-2020Product 15R3EMorning
19-05-2020Product 2848YEvening
19-05-2020Product 2848YEvening
19-05-2020Product 28S9GEvening
22-05-2020Product 18V2UEvening
22-05-2020Product 48V3TNight
26-05-2020Product 1AX3SMorning
08-06-2020Product 1CL7REvening
12-06-2020Product 1DR2FEvening
19-06-2020Product 1FF4PEvening
08-07-2020Product 1LD4NMorning
07-07-2020Product 3LL7AMorning
13-07-2020Product 1NH8UMorning
16-04-2020Product 1WS9VNight
16-04-2020Product 1WS9WEvening

 

What I need is to rank the bathes by date and shift, going from older to newest which is first comes the newest date and shift is first evening, then morning and oldst is night shift.

I've tried to do it with 

Rank = RANKX(FILTER('Table','Table'[Product]=EARLIER('Table'[Product])),'Table'[Batch],,DESC,Dense) just to rank the dates but it looks like as the table is not sorted by date, the rank is not working properly.
 
This is what I'd like to get is something like this, in order to pick the last three for further calculations:
 
DateProductBatchShiftRank
27-04-2020Product 12N7JMorning9
08-05-2020Product 15R3EMorning8
19-05-2020Product 2848YEvening1
19-05-2020Product 2848YEvening1
19-05-2020Product 28S9GEvening2
22-05-2020Product 18V2UEvening7
22-05-2020Product 48V3TNight1
26-05-2020Product 1AX3SMorning6
08-06-2020Product 1CL7REvening5
12-06-2020Product 1DR2FEvening4
19-06-2020Product 1FF4PEvening3
08-07-2020Product 1LD4NMorning2
07-07-2020Product 3LL7AMorning1
13-07-2020Product 1NH8UMorning1
16-04-2020Product 1WS9VNight10
16-04-2020Product 1WS9WEvening11

 

Which, if I sort it by date to see it better:

DateProductBatchShiftRank
13-07-2020Product 1NH8UMorning1
08-07-2020Product 1LD4NMorning2
07-07-2020Product 3LL7AMorning1
19-06-2020Product 1FF4PEvening3
12-06-2020Product 1DR2FEvening4
08-06-2020Product 1CL7REvening5
26-05-2020Product 1AX3SMorning6
22-05-2020Product 18V2UEvening7
22-05-2020Product 48V3TNight1
19-05-2020Product 2848YEvening1
19-05-2020Product 2848YEvening1
19-05-2020Product 28S9GEvening2
08-05-2020Product 15R3EMorning8
27-04-2020Product 12N7JMorning9
16-04-2020Product 1WS9VNight10
16-04-2020Product 1WS9WEvening11

 

Any help, please?

 

Thanks in advance!

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

 

Create a COLUMN

 

_add = 

var a = CALCULATE(MAx('Table'[DATE ]), FILTER('Table', 'Table'[DATE ] = EARLIER('Table'[DATE ]) && 'Table'[ Product] = EARLIER('Table'[ Product]) && 'Table'[Batch] <> EARLIER('Table'[Batch])))

var b = IF (a <> BLANK() && 'Table'[Shift] = "Morning" , 3, 0)

RETURN



SWITCH(
    TRUE(),
   a <> BLANK() && 'Table'[Shift] = "Morning",.3,
   a <> BLANK() && 'Table'[Shift] = "Evening",.2,
   a <> BLANK() && 'Table'[Shift] = "Night",.1
)

 

Create the Rank Column

Ranki = 
var a = MAXX(FILTER('Table','Table'[Batch] = EARLIER('Table'[Batch]) && 'Table'[ Product] = EARLIER('Table'[ Product])),'Table'[DATE ])

var b = RANKX(FILTER('Table','Table'[ Product] = EARLIER('Table'[ Product])),'Table'[DATE ] + 'Table'[_add] ,,DESC,Dense)
RETURN
b

 

1.jpg

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

View solution in original post

8 REPLIES 8
parry2k
Super User
Super User

@Anonymous there is one explaining RANKX blog post that can help you sort ranking for multiple columns. Check it here. 

 

https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures

 

If for some reason, it doesn't help, explain what you did and what is not working and we can look into it.

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Hi again!

Thanks for your help but I've read the guidance but I still get the same problem and I know I'm doing something grong but I don't how how to do ir correctly. At the end, I'm never able to rank it from newest to oldest. And I know I need to put a condition where I filter all the dates before the date in the actual row but also I need to filter the product for that row. And I can't get a place to put both conditions properly.

 

I've tried many combinations but the most close I get is with:

 

RANKX(FILTER(ALL('Table'[Batch],'Table'[Date]),'Table'[Date]<TODAY()),'Table'[Batch],,ASC,Dense)

RANKX(FILTER(ALL('Table'[Batch],'Table'[Date]),DATESYTD('Table'[Date])),DISTINCTCOUNT('Table'[Batch]),,ASC,Dense)

 

But anyways, it looks like it still ranking it as they are in the table at first.

 

Any help with it, please?

 

Thanks!

 

 

Hi @Anonymous ,

 

Can  you explain the logic a little better.

 

Maybe I am missing something.

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

Anonymous
Not applicable

Sure! 🙂

 

I have four columns to take into account for the rank:

DATE         PRODUCT           BATCH          SHIFT

 

and I have the efficiency as the fifth column with which I'm going to make the calculations afterwards. The goal is to get the average speed of the last three batches for each product.

 

The data comes in a random sorting, not sorted by date I mean. So I get:

 

DATE                       PRODUCT            BATCH           SHIFT

09/02/2020            Product 1                A                Morning

09/02/2020            Product 1                A                Evening

10/02/2020            Product 1                A                Night

10/02/2020            Product 1                B                Morning

06/02/2020            Product 1                C                Morning

05/02/2020            Product 1                D                Night

05/02/2020            Product 2                E               Evening

07/02/2020            Product 2                F               Morning

 

And so on. So What I'd like to get is to get other to rank it as follows:

 

DATE                       PRODUCT            BATCH           SHIFT             RANK

09/02/2020            Product 1                A                Morning            2

09/02/2020            Product 1                A                Evening             2

10/02/2020            Product 1                A                Night                 2

10/02/2020            Product 1                B                Morning             1

06/02/2020            Product 1                C                Morning            3

05/02/2020            Product 1                D                Night                4

05/02/2020            Product 1                E               Morning             5

05/02/2020            Product 2                F               Evening               2

07/02/2020            Product 2                G               Morning              1

 

It would be this logic:

For each produc, rank each batch by date and shift, from newest to oldest, taking into account that the actual timelime is

 

Date X night --> Date X morning --> Date X evening --> Date X+1 night --> Date X+1 morning --> ... and so on

 

I hope it makes sense!

 

Thanks a lot for your help

 

 

Hi @Anonymous ,

 

 

10/02/2020            Product 1                A                Night                 2

 

is this 10/02/2020 or 09/02/2020

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

 

 

Hi @Anonymous ,

 

 

Create a COLUMN

 

_add = 

var a = CALCULATE(MAx('Table'[DATE ]), FILTER('Table', 'Table'[DATE ] = EARLIER('Table'[DATE ]) && 'Table'[ Product] = EARLIER('Table'[ Product]) && 'Table'[Batch] <> EARLIER('Table'[Batch])))

var b = IF (a <> BLANK() && 'Table'[Shift] = "Morning" , 3, 0)

RETURN



SWITCH(
    TRUE(),
   a <> BLANK() && 'Table'[Shift] = "Morning",.3,
   a <> BLANK() && 'Table'[Shift] = "Evening",.2,
   a <> BLANK() && 'Table'[Shift] = "Night",.1
)

 

Create the Rank Column

Ranki = 
var a = MAXX(FILTER('Table','Table'[Batch] = EARLIER('Table'[Batch]) && 'Table'[ Product] = EARLIER('Table'[ Product])),'Table'[DATE ])

var b = RANKX(FILTER('Table','Table'[ Product] = EARLIER('Table'[ Product])),'Table'[DATE ] + 'Table'[_add] ,,DESC,Dense)
RETURN
b

 

1.jpg

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

Anonymous
Not applicable

It worked!!!!!

 

Thank you very much for your help!

HI @Anonymous ,

 

Hapy to help 🙂

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

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.