Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Dynamic Row_Number

Hi,

I have a requirement where I have employees and their marks by class and year as mentioned below. In the below example, Year is my slicer. Based on this slicers selection I have to rank the employees based on their marks also the row numbers.

Actual Data:

EmployeeNameYearTotal Marks
Sofia201997
Mohan201996
Peter201990
Abhishek201990
Chris201890
Mike201858
Chaitanya201858
Paul201847
Raman201745
Bliss201734
Vinod201734
Karthik201729
Anand201623
Raju201623

 

For example 1: If I select 2019 as Year, then my expected output should be

EmployeeNameTotal MarksRankRow Number
Sofia9711
Mohan9622
Peter9033
Abhishek9034

 

For example 2: If I select 2017 as Year, then my expected output should be

EmployeeNameTotal MarksRankRow Number
Raman4511
Bliss3422
Vinod3423
Karthik2934

 

For example 3: If I select all Years, then my expected output should be

EmployeeNameTotal MarksRankRow Number
Sofia9711
Mohan9622
Peter9033
Abhishek9034
Chris9035
Mike5846
Chaitanya5847
Paul4758
Raman4569
Bliss34710
Vinod34711
Karthik29812
Anand23913
Raju23914

 

I got the Rank covered as expected using the below Rank measure.

Rank = 
RANKX (
    ALLSELECTED ( Employee[EmployeeName] ),
    CALCULATE ( SUM ( Employee[totalMarks] ) ),
    ,
    DESC,
    DENSE
)

Can someone help me with how to get this Row_Number in Power BI using DAX to work dynamically on Slicer selection?

 

Thanks in advance!

  • Anonymous ,

     

    You need to create an addtional index and then create a measure using DAX below:

    Row Number = 
    COUNTROWS (
        FILTER (
            ALLSELECTED ( Employee ),
            COUNTROWS (
                FILTER (
                    Employee,
                    Employee[Total Marks] <= EARLIER ( Employee[Total Marks] )
                        && Employee[Index] >= EARLIER ( Employee[Index] )
                )
            )
        )
    )

     

     

    Community Support Team _ Jimmy Tao

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

     

5 Replies

  • jthomson's avatar
    jthomson
    Solution Sage

    It's exactly the same as your RANKX function, just find some way to break ties and incorporate that in it - maybe just add an index and use that

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi jthomson,

       

      Thanks for your response. I tried with Index, but no luck.

      • v-yuta-msft's avatar
        v-yuta-msft
        Community Support

        Anonymous ,

         

        You need to create an addtional index and then create a measure using DAX below:

        Row Number = 
        COUNTROWS (
            FILTER (
                ALLSELECTED ( Employee ),
                COUNTROWS (
                    FILTER (
                        Employee,
                        Employee[Total Marks] <= EARLIER ( Employee[Total Marks] )
                            && Employee[Index] >= EARLIER ( Employee[Index] )
                    )
                )
            )
        )

         

         

        Community Support Team _ Jimmy Tao

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