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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
smpa01
Super User
Super User

Forcing RANKX to rank from 1

Hi,

My source data is following which is a minimum reproducible example of my large dataset

dateemp_id
1/1/20212
1/2/20212
1/3/20212
1/4/20212
1/1/20213
1/2/20213
1/3/20213
1/4/20213
1/1/20216
1/2/20216
1/3/20216
1/4/20216
2/1/20212
2/2/20212
2/3/20212
2/4/20212
2/1/20213
2/2/20213
2/3/20213
2/4/20213
2/1/20216
2/2/20216
2/3/20216
2/4/20216

 

| date     | emp_id |
|----------|--------|
| 1/1/2021 | 2      |
| 1/2/2021 | 2      |
| 1/3/2021 | 2      |
| 1/4/2021 | 2      |
| 1/1/2021 | 3      |
| 1/2/2021 | 3      |
| 1/3/2021 | 3      |
| 1/4/2021 | 3      |
| 1/1/2021 | 6      |
| 1/2/2021 | 6      |
| 1/3/2021 | 6      |
| 1/4/2021 | 6      |
| 2/1/2021 | 2      |
| 2/2/2021 | 2      |
| 2/3/2021 | 2      |
| 2/4/2021 | 2      |
| 2/1/2021 | 3      |
| 2/2/2021 | 3      |
| 2/3/2021 | 3      |
| 2/4/2021 | 3      |
| 2/1/2021 | 6      |
| 2/2/2021 | 6      |
| 2/3/2021 | 6      |
| 2/4/2021 | 6      |

 

and my desired outcome is following which is ranking of date by employee

 

| date     | emp_id | rank |
|----------|--------|------|
| 1/1/2021 | 2      | 1    |
| 1/2/2021 | 2      | 2    |
| 1/3/2021 | 2      | 3    |
| 1/4/2021 | 2      | 4    |
| 1/1/2021 | 3      | 1    |
| 1/2/2021 | 3      | 2    |
| 1/3/2021 | 3      | 3    |
| 1/4/2021 | 3      | 4    |
| 1/1/2021 | 6      | 1    |
| 1/2/2021 | 6      | 2    |
| 1/3/2021 | 6      | 3    |
| 1/4/2021 | 6      | 4    |
| 2/1/2021 | 2      | 5    |
| 2/2/2021 | 2      | 6    |
| 2/3/2021 | 2      | 7    |
| 2/4/2021 | 2      | 8    |
| 2/1/2021 | 3      | 5    |
| 2/2/2021 | 3      | 6    |
| 2/3/2021 | 3      | 7    |
| 2/4/2021 | 3      | 8    |
| 2/1/2021 | 6      | 5    |
| 2/2/2021 | 6      | 6    |
| 2/3/2021 | 6      | 7    |
| 2/4/2021 | 6      | 8    |

 

To come to this I can use the following measure using ALLSELECTED

 

Measure 6 = 
VAR _1 = MAX('fact'[emp_id])
VAR _2 = RANKX(FILTER(ALLSELECTED('fact'),'fact'[emp_id]=_1),CALCULATE(MAX('fact'[date])),,ASC,Dense) RETURN _2

 

but I don't want to use ALLSELECTED as it has severe performance issue. Instead, I can use ALLEXCEPT to come here but the ranking with ALLEXCEPT does not start with 1. I wonder why? @OwenAuger 

Secondly, how can I make changes in ALLEXCEPT measure to force the measure to start ranking from 1

 

Measure 2 = RANKX(ALLEXCEPT('fact','fact'[emp_id]),CALCULATE(max('fact'[date])),,ASC,Dense)

 

Capture.PNG

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

RANKX is one of the most underrated functions regarding its complexity. In addition, ALLEXCEPT even gets the case worse.

Measure 2 = RANKX(ALLEXCEPT('fact','fact'[emp_id]),CALCULATE(max('fact'[date])),,ASC,Dense)

First, I hope you are clear on that ALLEXCEPT here is a table function; it constructs such a table to be consumed by CALCULATE(max('fact'[date])) for a lookup table for ranking in each row of the viz.

Screenshot 2021-07-15 124003.png

Secondly, CALCULATE(max('fact'[date])) for the lookup table evaluates under a mixed evaluation context, that's to say, the above table + current filter context.

Thirdly, CALCULATE(max('fact'[date])) is evaluated once again under current filter context only; and the result is used along with lookup table for ranking.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

RANKX is one of the most underrated functions regarding its complexity. In addition, ALLEXCEPT even gets the case worse.

Measure 2 = RANKX(ALLEXCEPT('fact','fact'[emp_id]),CALCULATE(max('fact'[date])),,ASC,Dense)

First, I hope you are clear on that ALLEXCEPT here is a table function; it constructs such a table to be consumed by CALCULATE(max('fact'[date])) for a lookup table for ranking in each row of the viz.

Screenshot 2021-07-15 124003.png

Secondly, CALCULATE(max('fact'[date])) for the lookup table evaluates under a mixed evaluation context, that's to say, the above table + current filter context.

Thirdly, CALCULATE(max('fact'[date])) is evaluated once again under current filter context only; and the result is used along with lookup table for ranking.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Many Thanks @CNENFRNL for your time to clarify doubt.

Having said that, I will keep this thread open for a while as I intend to ask the same question to Marco and see if he can spare some time to look into it.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
smpa01
Super User
Super User

I can resolve this by using

Measure 8 = RANKX(FILTER(ALL('fact'),'fact'[emp_id]=MAX('fact'[emp_id])),CALCULATE(max('fact'[date])),,ASC,Dense)

Capture.PNG

but I still wonder why RANKING did not start with 1 while using ALLEXCEPT.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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

Users online (2,313)