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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
lee_hawthorn
Frequent Visitor

Rank with filter

I have table :

 

Sales

 

ProductCat    ProductGroup   Sales

1                    1                        100

2                    1                        105

3                    2                         34

4                    2                         23

5                    5                        1000

 

I need to filter out Product Cat 5 and then add a rank column, partitioned by  ProductGroup i.e. :

 

ProductCat    ProductGroup   Sales   Rank

1                    1                        100      2

2                    1                        105      1

3                    2                         34       1

4                    2                         23       2 

 

I can do the rank with :

 

ADDCOLUMNS('Sales', "Rank",  RANKX(CALCULATETABLE('Sales', ALLEXCEPT('Sales', 'Sales[ProductGroup]), 'Sales'[Sales])

 

One thing I'm struggling with filtering out ProductCat 5 before running the RANKX.   I tried adding it into CALCULATETABLE as an additional filter but this has no effect due to the ALLEXCEPT.  I've tried a nested CALCULATETABLE with the filter but this has no effect either.

 

Any ideas how I can apply a filter prior to CALCULATETABLE?

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@lee_hawthorn 

Easy answer is to try this: 

SalesRank = ADDCOLUMNS(FILTER(Sales,Sales[ProductGroup]<>5),"Rank", RANKX(ALLSELECTED(Sales), [TotalSales]))
So basically filter the Sales table before you add columns to it.
 
I wonder though, is there a reason you're using DAX ADDCOLUMNS to create a new table? How do you need to use this in the report and should the rank update if you filter out another product category.

 

One thing you can do is use a MEASURE instead. That will enable you to create a table visualization that can have the measure added to it. Then you can filter that visual to exclude category 5 and the rankings will update. 

Rank = RANKX(ALLEXCEPT(Sales,Sales[Sales],Sales[ProductGroup]), [TotalSales])
 

Has this post solved your problem? Please mark it as a 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.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


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

4 REPLIES 4
amitchandak
Super User
Super User
AllisonKennedy
Super User
Super User

@lee_hawthorn 

Easy answer is to try this: 

SalesRank = ADDCOLUMNS(FILTER(Sales,Sales[ProductGroup]<>5),"Rank", RANKX(ALLSELECTED(Sales), [TotalSales]))
So basically filter the Sales table before you add columns to it.
 
I wonder though, is there a reason you're using DAX ADDCOLUMNS to create a new table? How do you need to use this in the report and should the rank update if you filter out another product category.

 

One thing you can do is use a MEASURE instead. That will enable you to create a table visualization that can have the measure added to it. Then you can filter that visual to exclude category 5 and the rankings will update. 

Rank = RANKX(ALLEXCEPT(Sales,Sales[Sales],Sales[ProductGroup]), [TotalSales])
 

Has this post solved your problem? Please mark it as a 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.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


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

mahoneypat
Microsoft Employee
Microsoft Employee

Just to clarify.  Are you trying to make a measure? calculated column? or calculated table?  Depending on what your goal, I would suggest a simpler DAX expression to achieve your goal.

 

FYI that you can just add a term to your Calculatetable( ) expression to filter out ProductCat <> 5 after the Allexcept() clause.  Note there seems to be a missing parentheses after that anyway.

 

Regards,

Pat

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


I'm sticking with table at the moment as there's more processing to do.  We can't add filters in CALCULATE as filter context is being amended by allselected().   

I will try applying the filter in an outer calculate.

 

This article explains it all

 

https://www.sqlbi.com/articles/order-of-evaluation-in-calculate-parameters/

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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