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
VidyaSagarMalla
New Member

Creating Dense Ranks for Multiple Categories using M Query or Dax

Hello All,

I am trying to figure how the acheive the Dense Ranks for the below sample data. I was able to get the Rank right for category level per Year but when i am comibing for other columns ranks reuslts are NOT coming right. 

 

Expression used in M : = Table.Group(#"Removed Columns", {"Category","Years"}, {{"All", each Table.AddRankColumn(_,"Fixed Rank",{"Price",Order.Ascending},[RankKind = RankKind.Dense])}})

 

I would love to see whats the best approach to get the below result. Thanks in Advance for looking into this.

 

Actual DataTransformation Columns in Power BI
IndexClassCategroyProductYear Price Product Rank Per YearRank by Category Per YearRank by  Class Per YearRank by Index Per Year
ActiveEquityFruitsApple Gala2022 $                1.001121
ActiveEquityFruitsApple Pink2022 $                2.002121
ActiveEquityFruitsGrapes2022 $                3.003121
ActiveEquityVegCarrots2022 $                2.001221
ActiveEquityVegonions2022 $                3.002221
ActiveEquityVegBrocli2022 $                4.003221
ActiveFixed IncomeBeveragesPepsi2022 $                3.001311
ActiveFixed IncomeBeveragesDew2022 $                5.002311
ActiveFixed IncomeBeveragescoke2022 $                6.003311
1 ACCEPTED SOLUTION
v-achippa
Community Support
Community Support

Hi @VidyaSagarMalla,

 

Thank you for reaching out to Microsoft Fabric Community.

 

The power query method using Table.AddRankColumn only works per one grouping at a time. So if you want multiple dense rank columns we have to repeat the grouping and ranking logic separately for each combination. So use DAX calculated columns in power bi.

 

Rank by Category Per Year =

RANKX(

    FILTER('YourTable',

        'YourTable'[Year] = EARLIER('YourTable'[Year]) &&

        'YourTable'[Categroy] = EARLIER('YourTable'[Categroy])

    ),

    'YourTable'[Price], ,

    ASC,

    DENSE

)

 

You can simply apply similar formulas for other combinations like Product, Class, and Index.

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

 

Thanks and regards,

Anjan Kumar Chippa

View solution in original post

6 REPLIES 6
techies
Solution Sage
Solution Sage

Hi @VidyaSagarMalla you can add measures like this

 

techies_0-1747890270227.png

 

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
v-achippa
Community Support
Community Support

Hi @VidyaSagarMalla,

 

Thank you for reaching out to Microsoft Fabric Community.

 

The power query method using Table.AddRankColumn only works per one grouping at a time. So if you want multiple dense rank columns we have to repeat the grouping and ranking logic separately for each combination. So use DAX calculated columns in power bi.

 

Rank by Category Per Year =

RANKX(

    FILTER('YourTable',

        'YourTable'[Year] = EARLIER('YourTable'[Year]) &&

        'YourTable'[Categroy] = EARLIER('YourTable'[Categroy])

    ),

    'YourTable'[Price], ,

    ASC,

    DENSE

)

 

You can simply apply similar formulas for other combinations like Product, Class, and Index.

 

If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

 

Thanks and regards,

Anjan Kumar Chippa

Hi @VidyaSagarMalla,

 

As we haven’t heard back from you, we wanted to kindly follow up to check if the solution I have provided for the issue worked? or let us know if you need any further assistance.
If my response addressed, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

 

Thanks and regards,

Anjan Kumar Chippa

Ashish_Excel
Resolver V
Resolver V

Hi,

Would you be OK with a measure based solution?

VidyaSagarMalla
New Member

Thank for looking into this @pankajnamekar25 . But looks like you have provided the same query from my question.  

pankajnamekar25
Memorable Member
Memorable Member

Hello @VidyaSagarMalla 

You can try for this in PQ

But I will suggest use DAX measure

 

Table.Group(#"Previous Step", {"Category","Year"}, {

    {"All", each Table.AddRankColumn(_, "CategoryYearRank", {"Price", Order.Ascending}, [RankKind = RankKind.Dense])}

})

Thanks,
 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

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.