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
mim
Advocate V
Advocate V

Rank Based on measures with row context.

Hello

 

I am trying to build a measure that ranks the values of another measures ( see attached picture), I know how to do as a calculated column, as you can use the row context ( [TOSTR FORECAST DATE]=EARLIER([TOSTR FORECAST DATE]), but i need to do it as a measure in a pivot table,

 

any idea how to do that, as measures don't support row context, I am using Excel 2013

 

 

 MEASURES.PNG

2 ACCEPTED SOLUTIONS

@mim@ImkeF, @Anonymous - interesting problem.

 

@Anonymous

I agree with your logic in the last post, and for Excel 2013 I would write the measure like this:

 

 

=
IF (
    HASONEVALUE ( MyTable[ID] ),
    RANKX (
        FILTER (
            ALL ( MyTable[ID] ),
            [TOSTR FORECAST DATE]
                = CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) )
        ),
        MyTable[ID],
        VALUES ( MyTable[ID] )
    )
)
  1. Added a HASONEVALUE check to allow evaluation only for single IDs
  2. Within FILTER, the measure for the currently iterated ID is [TOSTR FORECAST DATE], and this is compared with the measure from the original filter context CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) ).
    Putting VALUES ( MyTable[ID] ) as a filter argument effectively undoes the context transition.
  3. Ranking is just based on ordering of IDs, descending by default. Can be tweaked.

 

Sample workbook here in case useful.

 

Cheers,

Owen 🙂


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

Anonymous
Not applicable

Wait, are you trying to tell me you actually understand the 3rd argument to RANKX?  NOBODY DOES!!  😄


Totally based on Owen's really good thinking, *I* would probably do this slightly differently to avoid the 3rd param:

Rank of ID among those with same TOSTR FORECAST DATE 2:=IF (
HASONEVALUE ( MyTable[ID] ),
  RANKX (
    FILTER (
      ALL ( MyTable[ID] ),
      [TOSTR FORECAST DATE] = CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) )
    ),
    CALCULATE( VALUES( MyTable[ID] ) )
  )
)


View solution in original post

8 REPLIES 8
Ola
Frequent Visitor

Just for the record...

This Video goes through the formula: https://youtu.be/sfJWoQixi2U?list=PLrRPvpgDmw0nglJ9yX2XT5-K1A_AKHpvW

And this video shows the advantage of ALLSELECTED() : https://www.youtube.com/watch?v=z2qzJVeYhTY

 

Anonymous
Not applicable

Talk about a time I wish I had VAR/RETURN to use.

Just making sure I understand -- and helps my to clarify for somebody with a good idea...

Column 1 is some ID.
Column 2 is the result of a measure (happens to return a date).
Column 3 is a rank of some unseen other measure, but only against rows with same column 2 Date.

Ya?

Hi mim,

just to check understanding: You need the Rank as a measure, but the [TOSTR FORECAST DATE] is a column, so Rank per [TOSTR FORECAST DATE]-day?

 

Or is the date a measure as well?

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

yes TOSTR FORECAST DATE is a measure basically  if there is one unique date then the rank will be 1, if the same date is repeated 5 times then the rank should 1,2,3,4,5 the order is not important

Anonymous
Not applicable

Ignoring the difficulties in iterating over only rows that return the same measure value -- you have another problem.  RankX can't return different ranks for the same value.  I've only dealt with this by "cheating" -- and forcing tiny (almost random) variation in the value I am ranking.

 

At the highest level, we want:

= RANKX (
     [[All Rows That We Want to Rank Against Each Other]],
     [[measure value to rank, with some way of removing ties]]
 )


The rows we want to rank against each other are... ALL rows that have the same measure value as the CURRENT row.

I'm gonna cheat and use variables, then hopefully we can figure out how to convert it ?

TheRank =
  VAR MyDate = [MeasureThatGivesTheDate]
  VAR MyRows = FILTER( ALL(MyTable[RowId]), [MeasureThatGivesTheDate] = MyDate )
  RETURN RANKX(MyRows, CALCULATE ( MIN (MyTable[RowId] ) ) )

At least, that works in my head... (using RowId to force an arbitrary ranking of all the MyRows)

@mim@ImkeF, @Anonymous - interesting problem.

 

@Anonymous

I agree with your logic in the last post, and for Excel 2013 I would write the measure like this:

 

 

=
IF (
    HASONEVALUE ( MyTable[ID] ),
    RANKX (
        FILTER (
            ALL ( MyTable[ID] ),
            [TOSTR FORECAST DATE]
                = CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) )
        ),
        MyTable[ID],
        VALUES ( MyTable[ID] )
    )
)
  1. Added a HASONEVALUE check to allow evaluation only for single IDs
  2. Within FILTER, the measure for the currently iterated ID is [TOSTR FORECAST DATE], and this is compared with the measure from the original filter context CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) ).
    Putting VALUES ( MyTable[ID] ) as a filter argument effectively undoes the context transition.
  3. Ranking is just based on ordering of IDs, descending by default. Can be tweaked.

 

Sample workbook here in case useful.

 

Cheers,

Owen 🙂


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn
Anonymous
Not applicable

Wait, are you trying to tell me you actually understand the 3rd argument to RANKX?  NOBODY DOES!!  😄


Totally based on Owen's really good thinking, *I* would probably do this slightly differently to avoid the 3rd param:

Rank of ID among those with same TOSTR FORECAST DATE 2:=IF (
HASONEVALUE ( MyTable[ID] ),
  RANKX (
    FILTER (
      ALL ( MyTable[ID] ),
      [TOSTR FORECAST DATE] = CALCULATE ( [TOSTR FORECAST DATE], VALUES ( MyTable[ID] ) )
    ),
    CALCULATE( VALUES( MyTable[ID] ) )
  )
)


Guys thanks a lot, that's why PowerBi rocks !!! @Anonymous@OwenAuger@ImkeF

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.