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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

How to rank one column with 2 categories?

Hey,

 

Could someone please help me out with the following problem please?

 

I have 2 x categories with products in each and I want to rank product on value but if it is a new category I want this ranking to also start at 1.

From that I also want to add my respective categories to that product list and rank each category as 1.
 
This is so that I can plot, salad, fruit and their respective products on a graph. Then if I filter on top 3 for example it will show my desired output.
 
Table
CategoryProductValue
FruitApple9
FruitBanana8
FruitOrange7
SaladLettuce4
SaladTomato1
   
Desired Output
NEW ProductValueRank
Apple91
Banana82
Orange73
Lettuce41
Tomato12
Fruit241
Salad51
Does anyone have any ideas please?
 
Thank you in advance,
 
Lauren.
2 ACCEPTED SOLUTIONS
AllisonKennedy
Super User
Super User

You could try:

Use SUMMARIZECOLUMNS to create a new table of your desired output:

Table2=UNION(
SUMMARIZECOLUMNS(Table[Product], "Value", SUM(Table[Value]), "Rank",
RANKX(
ALL('Table'[Category]) ,
CALCULATE(
SUM('Table'[Value])
)
)),
SUMMARIZECOLUMNS(Table[Category], "Value", SUM(Table[Value]), "Rank", 1)

https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures

Or this will help get the ranks for the products only:

You could add a column to your table using RANKX, similar to this
Rank all rows as Column (Country) =
RANKX(
FILTER(
‘Table’,
‘Table'[Category] = EARLIER(‘Table'[Category])
),
‘Sales Table'[Value]
)
https://databear.com/how-to-use-the-dax-rankx-function-in-power-bi/

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

harshnathani
Community Champion
Community Champion

Hi @Anonymous ,

 

A little modification to the formula by @AllisonKennedy 

 

New Table =
UNION (
    SUMMARIZECOLUMNS (
        'Table'[Product],
        "Value", SUM ( 'Table'[Value] ),
        "Rank", IF (
            MAX ( 'Table'[Product] )
                <> BLANK (),
            RANKX (
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[Category]
                        = MAX ( 'Table'[Category] )
                ),
                CALCULATE (
                    SUM ( 'Table'[Value] )
                )
            )
        )
    ),
    SUMMARIZECOLUMNS (
        'Table'[Category],
        "Value", SUM ( 'Table'[Value] ),
        "Rank", IF (
            MAX ( 'Table'[Product] )
                <> BLANK (),
            RANKX (
                ALL ( 'Table'[Category] ),
                CALCULATE (
                    SUM ( 'Table'[Value] )
                )
            )
        )
    )
)

 

1.jpg

 

 

Regards,

Harsh Nathani


Appreciate with a Kudos!! (Click the Thumbs Up Button)

Did I answer your question? Mark my post as a solution!

View solution in original post

3 REPLIES 3
FrankAT
Community Champion
Community Champion

Hi @Anonymous,

what about the following solution:

 

05-07-_2020_23-33-54.jpg

 

Regards FrankAT

harshnathani
Community Champion
Community Champion

Hi @Anonymous ,

 

A little modification to the formula by @AllisonKennedy 

 

New Table =
UNION (
    SUMMARIZECOLUMNS (
        'Table'[Product],
        "Value", SUM ( 'Table'[Value] ),
        "Rank", IF (
            MAX ( 'Table'[Product] )
                <> BLANK (),
            RANKX (
                FILTER (
                    ALL ( 'Table' ),
                    'Table'[Category]
                        = MAX ( 'Table'[Category] )
                ),
                CALCULATE (
                    SUM ( 'Table'[Value] )
                )
            )
        )
    ),
    SUMMARIZECOLUMNS (
        'Table'[Category],
        "Value", SUM ( 'Table'[Value] ),
        "Rank", IF (
            MAX ( 'Table'[Product] )
                <> BLANK (),
            RANKX (
                ALL ( 'Table'[Category] ),
                CALCULATE (
                    SUM ( 'Table'[Value] )
                )
            )
        )
    )
)

 

1.jpg

 

 

Regards,

Harsh Nathani


Appreciate with a Kudos!! (Click the Thumbs Up Button)

Did I answer your question? Mark my post as a solution!

AllisonKennedy
Super User
Super User

You could try:

Use SUMMARIZECOLUMNS to create a new table of your desired output:

Table2=UNION(
SUMMARIZECOLUMNS(Table[Product], "Value", SUM(Table[Value]), "Rank",
RANKX(
ALL('Table'[Category]) ,
CALCULATE(
SUM('Table'[Value])
)
)),
SUMMARIZECOLUMNS(Table[Category], "Value", SUM(Table[Value]), "Rank", 1)

https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures

Or this will help get the ranks for the products only:

You could add a column to your table using RANKX, similar to this
Rank all rows as Column (Country) =
RANKX(
FILTER(
‘Table’,
‘Table'[Category] = EARLIER(‘Table'[Category])
),
‘Sales Table'[Value]
)
https://databear.com/how-to-use-the-dax-rankx-function-in-power-bi/

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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