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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

To create a DYNAMIC index column using DAX based on Rank column:

I wanted to created a DYNAMIC index column using DAX based on Rank column:

My Table: 

Table
NamesCounts
XXX1
YYY2
ZZZ3
AAA4
AAA2
BBB3
BBB2
CCC0
CCC1
DDD2
FFF1


I Created a Measure column 'Total Counts' by grouping 'Names' column & created Rank based on 'Total count'

NamesTotal countsRank
AAA61
BBB52
ZZZ33
DDD24
YYY24
CCC15
FFF15
XXX15

 


My exepected Output:
I need to create a Index column based on the Rank column as shown in the below table using DAX:

NamesTotal countsRankIndex
AAA611
BBB522
ZZZ333
DDD244
YYY245
CCC156
FFF157
XXX158




2 ACCEPTED SOLUTIONS
jennratten
Super User
Super User

This measure will produce the index needed based on the rank of the total counts, then breaking ties using the names sorted alphabetically.

 

Rank on Count and Name = 
VAR CountByName = [TotalCounts]
VAR CurrentName =
    SELECTEDVALUE ( TableOfCounts[Names] )
RETURN
    IF (
        NOT ISBLANK ( CurrentName )
            && ( CountByName > 0 ),
        VAR AllSelectedNames =
            ALLSELECTED ( TableOfCounts[Names] )
        VAR MaxNameRanked =
            CALCULATE ( COUNTROWS ( TableOfCounts ), REMOVEFILTERS () )
        VAR LookupTable =
            ADDCOLUMNS (
                AllSelectedNames,
                "@CountByName",
                    [TotalCounts] * MaxNameRanked
                        + RANKX ( AllSelectedNames, TableOfCounts[Names],, DESC, DENSE )
            )
        VAR LookupCurrentName =
            FILTER ( LookupTable, TableOfCounts[Names] = CurrentName )
        VAR CurrentValue =
            MAXX ( LookupCurrentName, [@CountByName] )
        VAR Ranking =
            RANKX ( LookupTable, [@CountByName], CurrentValue,, DENSE )
        RETURN
            Ranking
    )

 

jennratten_0-1629730111598.png

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

View solution in original post

Sure thing!  Here is the same script, but with comments added for explanation.  Note, this page provides additional insight. https://www.sqlbi.com/articles/rankx-on-multiple-columns-with-dax-and-power-bi/

 

Rank on Count and Name = 
-------------------------------------------------------
-- Variables Applicable to the Entire Measure
-------------------------------------------------------
VAR CountByName = [TotalCounts]                  -- TotalCounts measure 
VAR CurrentName =                                -- The name for the current iteration of the table.
    SELECTEDVALUE ( TableOfCounts[Names] )
RETURN
    IF (                                      
        NOT ISBLANK ( CurrentName )              -- Make sure the name is not blank
            && ( CountByName > 0 ),              -- and the total count for the name is greater than 0.
-------------------------------------------------------
-- Variables Applicable within the Row Context
-------------------------------------------------------
        VAR AllSelectedNames =                    -- Create a table of names.
            ALLSELECTED ( TableOfCounts[Names] ) 
        VAR MaxNameRanked =                       -- Since the name indices are distinct
            CALCULATE (                           -- the max index equals the # of table rows.   
                COUNTROWS ( TableOfCounts ), 
                REMOVEFILTERS ()                  -- retrieve the max for the entire model
            )                                     -- result is a constant value that does not require a query to be computed
        VAR LookupTable =                         -- Temp table with all names ranked.
            ADDCOLUMNS (                          -- Add a column
                AllSelectedNames,                 -- to this table.
                "@CountByName",                   -- New column name
                    [TotalCounts] * MaxNameRanked -
                        + RANKX (                 -- Convert names into a number by computing the rank of the name.
                            AllSelectedNames, 
                            TableOfCounts[Names],
                            , DESC, DENSE         -- Alpha DESC: later in the alphabet = better = lower integer
                        )
            )
        VAR LookupCurrentName =                   -- Temp table for the current row context (Name).
            FILTER ( 
                LookupTable, 
                TableOfCounts[Names] = CurrentName 
            )
        VAR CurrentValue =                        -- Temp table for the current row context (CountByName). 
            MAXX ( 
                LookupCurrentName, 
                [@CountByName] 
            )
        VAR Ranking =                             -- Calculate the rank.
            RANKX ( 
                LookupTable, 
                [@CountByName], 
                CurrentValue,
                , 
                DENSE 
            )
        RETURN
            Ranking                               -- Return the rank.
    )

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

View solution in original post

5 REPLIES 5
jennratten
Super User
Super User

This measure will produce the index needed based on the rank of the total counts, then breaking ties using the names sorted alphabetically.

 

Rank on Count and Name = 
VAR CountByName = [TotalCounts]
VAR CurrentName =
    SELECTEDVALUE ( TableOfCounts[Names] )
RETURN
    IF (
        NOT ISBLANK ( CurrentName )
            && ( CountByName > 0 ),
        VAR AllSelectedNames =
            ALLSELECTED ( TableOfCounts[Names] )
        VAR MaxNameRanked =
            CALCULATE ( COUNTROWS ( TableOfCounts ), REMOVEFILTERS () )
        VAR LookupTable =
            ADDCOLUMNS (
                AllSelectedNames,
                "@CountByName",
                    [TotalCounts] * MaxNameRanked
                        + RANKX ( AllSelectedNames, TableOfCounts[Names],, DESC, DENSE )
            )
        VAR LookupCurrentName =
            FILTER ( LookupTable, TableOfCounts[Names] = CurrentName )
        VAR CurrentValue =
            MAXX ( LookupCurrentName, [@CountByName] )
        VAR Ranking =
            RANKX ( LookupTable, [@CountByName], CurrentValue,, DENSE )
        RETURN
            Ranking
    )

 

jennratten_0-1629730111598.png

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

Anonymous
Not applicable

Thank you so much..
Can you please explain on th functions used in...
It would really helps..

Sure thing!  Here is the same script, but with comments added for explanation.  Note, this page provides additional insight. https://www.sqlbi.com/articles/rankx-on-multiple-columns-with-dax-and-power-bi/

 

Rank on Count and Name = 
-------------------------------------------------------
-- Variables Applicable to the Entire Measure
-------------------------------------------------------
VAR CountByName = [TotalCounts]                  -- TotalCounts measure 
VAR CurrentName =                                -- The name for the current iteration of the table.
    SELECTEDVALUE ( TableOfCounts[Names] )
RETURN
    IF (                                      
        NOT ISBLANK ( CurrentName )              -- Make sure the name is not blank
            && ( CountByName > 0 ),              -- and the total count for the name is greater than 0.
-------------------------------------------------------
-- Variables Applicable within the Row Context
-------------------------------------------------------
        VAR AllSelectedNames =                    -- Create a table of names.
            ALLSELECTED ( TableOfCounts[Names] ) 
        VAR MaxNameRanked =                       -- Since the name indices are distinct
            CALCULATE (                           -- the max index equals the # of table rows.   
                COUNTROWS ( TableOfCounts ), 
                REMOVEFILTERS ()                  -- retrieve the max for the entire model
            )                                     -- result is a constant value that does not require a query to be computed
        VAR LookupTable =                         -- Temp table with all names ranked.
            ADDCOLUMNS (                          -- Add a column
                AllSelectedNames,                 -- to this table.
                "@CountByName",                   -- New column name
                    [TotalCounts] * MaxNameRanked -
                        + RANKX (                 -- Convert names into a number by computing the rank of the name.
                            AllSelectedNames, 
                            TableOfCounts[Names],
                            , DESC, DENSE         -- Alpha DESC: later in the alphabet = better = lower integer
                        )
            )
        VAR LookupCurrentName =                   -- Temp table for the current row context (Name).
            FILTER ( 
                LookupTable, 
                TableOfCounts[Names] = CurrentName 
            )
        VAR CurrentValue =                        -- Temp table for the current row context (CountByName). 
            MAXX ( 
                LookupCurrentName, 
                [@CountByName] 
            )
        VAR Ranking =                             -- Calculate the rank.
            RANKX ( 
                LookupTable, 
                [@CountByName], 
                CurrentValue,
                , 
                DENSE 
            )
        RETURN
            Ranking                               -- Return the rank.
    )

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

I don't know why I get all as 1 😪

I only replaced my table and column , right?

 

Any Idea?

Greg_Deckler
Super User
Super User

@Anonymous I invented this once. The Mythical DAX Index - Microsoft Power BI Community. If that doesn't get you there, let me know.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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