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
MichaelF1
Helper II
Helper II

Dax - add column - group then rank

Hi everyone

 

I need to add a Rank Column to a table.

 

I have this:

 

DateProduct NameNum Sold
Date1Prod12
Date2Prod16
Date3Prod26
etcProd24
etcProd110
etcProd22
etcProd18
etcProd26
etcProd29

 

 

And I want to add a column that returns the Rank of the product, based on the total number sold for each product across all dates.

 

Like this:

 

DateProduct NameNum SoldProduct rank
Date1Prod122
Date2Prod162
Date3Prod261
etcProd241
etcProd1102
etcProd221
etcProd182
etcProd261
etcProd291

 

I've been trying this:

Product rank =

 

RANKX(

    SUMMARIZE(

        'MyTable',  

        'MyTable'[Product Name], "GroupedQty", SUM('MyTable'[Num Sold]))

    ,  

    [GroupedQty],  

    ,  

    ASC

)

 

But I get an error saying: "Column 'GroupedQty' cannot be found or may not be used in this expression"

 

Thanks in advance 🙂

 

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Write these calculated column formulas

Total of product = CALCULATE(SUM(Data[Num Sold]),FILTER(data,Data[Product Name]=EARLIER(Data[Product Name])))
Rank = CALCULATE(DISTINCTCOUNT(Data[Total of product]),FILTER(Data,Data[Total of product]>=EARLIER(Data[Total of product])))

Hope this helps.

Ashish_Mathur_0-1712454912989.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

8 REPLIES 8
AnalyticsWizard
Solution Supplier
Solution Supplier

@MichaelF1 

The error you’re encountering is because the SUMMARIZE function creates a new table, and the column GroupedQty is not recognized outside of this function.

To create a rank column based on the total number sold for each product across all dates, you can use the RANKX function in DAX. Here’s an example:

Product rank = 
RANKX(
    ALL('MyTable'[Product Name]),
    CALCULATE(SUM('MyTable'[Num Sold]))
)

This formula ranks the products based on the total number sold. The ALL function is used to consider all the products in the calculation, and the CALCULATE function changes the context in which the sum is computed.

Please replace 'MyTable' with the actual name of your table. This measure will create a new column in your table that contains the rank of each product based on the total number sold.

Remember, DAX operates on tables and columns, not on individual cells or rows. 

Hi @AnalyticsWizard , thanks for your post, but I don't seem to get the right result from this code:

 

MichaelF1_0-1712504002559.png

 

Ashish_Mathur
Super User
Super User

Hi,

Write these calculated column formulas

Total of product = CALCULATE(SUM(Data[Num Sold]),FILTER(data,Data[Product Name]=EARLIER(Data[Product Name])))
Rank = CALCULATE(DISTINCTCOUNT(Data[Total of product]),FILTER(Data,Data[Total of product]>=EARLIER(Data[Total of product])))

Hope this helps.

Ashish_Mathur_0-1712454912989.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, yes this worked, thank you. Although I'm surprised to see that you got the rank without using any of the Rank functions!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
lbendlin
Super User
Super User

Yes, RANKX expects a distinct list of category values.  You need to first aggregate your data, and then backport the ranking into the individual rows

 

 

lbendlin_0-1712440658862.png

 

Product Rank =
VAR p = [Product Name]
VAR b =
    ADDCOLUMNS (
        ALL ( MyTable[Product Name] ),
        "s",
            VAR p2 = [Product Name]
            RETURN
                SUMX ( FILTER ( MyTable, [Product Name] = p2 ), [Num Sold] )
    )
RETURN
    MAXX (
        FILTER ( ADDCOLUMNS ( b, "r", RANKX ( b, [s] ) ), [Product Name] = p ),
        [r]
    )

or if you want to live on the edge

 

Product Rank =
VAR p = [Product Name]
VAR b =
    SUMMARIZE ( ALL ( MyTable ), [Product Name], "s", SUM ( MyTable[Num Sold] ) )
RETURN
    MAXX (
        FILTER ( ADDCOLUMNS ( b, "r", RANKX ( b, [s] ) ), [Product Name] = p ),
        [r]
    )

 

Bipin-Lala
Responsive Resident
Responsive Resident

Hi @MichaelF1,

 

I was able to achieve your expected result by using the following DAX formulas below - 

 

Summing Product Numbers Sold

Total Units Sold = CALCULATE(SUM('Products'[Num Sold]), ALLEXCEPT(Products, Products[Product Name]))

 

 Calculating Rank of each product 

Product Rank = RANKX(ALL(Products[Product Name]), [Total Units Sold], , DESC, Dense)

BipinLala_0-1712438190699.png

 

Note: If you don't use ALLEXCEPT during summation, then RANX won't return the expect rank when you display multiple fields in the visual, like dates.

Total Units Sold = CALCULATE(SUM('Products'[Num Sold]))

Correct Rank

BipinLala_4-1712438355318.png

Incorrect Rank as soon as dates field is added

BipinLala_1-1712438311201.png

Let me know if this helps!

 

Hi @Bipin-Lala , Thanks for your post. Is your solution for adding columns to a visual? I really want to add the columns to a table. I seem to get the incorrect rank:

 

MichaelF1_0-1712504779402.png

 

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.