Forum Discussion

newpbiuser01's avatar
newpbiuser01
Helper V
3 years ago
Solved

Ranking over multiple categories

Hello,

 

I have a table that breaks down sales for different stores to multiple categories/subcategories. I am trying to find the top two category/subcagories for each store by spend and also calculate the % of spend over each category.

 

If it was just the stores, I'd do Group By in Power Query, and do sum of spend, sort it in descending order and then create an index column. But I'm not sure how to do it with the nested categories/subcategories within the Stores. Also I'm not sure if the % of Spend over each store is possible in power query or do I do it in DAX. My table is as follows: 

 

Main Table: 

StoreCategoriesSubcategorySpend
AClothingMen47
AClothingWomen26
AShoesKids75
AHousewaresDishes17
BBooksFiction64
BStationaryPaper72
BBagsHandbags95
BBaby ItemsToys22
CBakery ItemsBread31
CBeveragesWater63
CHousewaresCandles22
CProduceFruits74
CProduceVegetables70
DBakery ItemsCakes54
DProduceFruits81
DProduceVegetables29
DBakery ItemsBread52
EBooksFiction28
EStationaryPens11
EStationaryPencils57
EStationaryMarkers38
EStationaryPaper99
FProduceFruits83
FDairyMillk40
FDairyCheese79
FProduceVegetables89

 

I'd like to first calculate the rank: 

StoreCategoriesSucategorySpendRank
AClothingMen472
AClothingWomen263
AShoesKids751
AHousewaresDishes174
BBooksFiction643
BStationaryPaper722
BBagsHandbags951
BBaby ItemsToys224
CBakery ItemsBread314
CBeveragesWater633
CHousewaresCandles225
CProduceFruits741
CProduceVegetables702
DBakery ItemsCakes542
DProduceFruits811
DProduceVegetables294
DBakery ItemsBread523
EBooksFiction284
EStationaryPens115
EStationaryPencils572
EStationaryMarkers383
EStationaryPaper991
FProduceFruits832
FDairyMillk404
FDairyCheese793
FProduceVegetables891

 

And the final result would be as follows: 

StoreCategorySubcategorySpendRank% of Spend
AShoesKids75145%
AClothingMen47228%
BBagsHandbags95138%
BStationaryPaper72228%
CProduceFruits74128%
CProduceVegetables70227%
DProduceFruits81138%
DBakery ItemsCakes54225%
EStationaryPaper99142%
EStationaryPencils57224%
FProduceVegetables89131%
FProduceFruits83229%

 

Would anyone know how I could do this? I'd really appreciate any help. 

 

Thank you!

  • newpbiuser01 

     

    Create 2 calculated column as below...

     

    Rank = RANKX(FILTER('Table', 'Table'[Store] = EARLIER('Table'[Store])), 'Table'[Spend])
     
    % Spent = 
    DIVIDE(
        'Table'[Spend],
        SUMX(FILTER('Table', 'Table'[Store] = EARLIER('Table'[Store])), 'Table'[Spend])
    )
     
     

2 Replies

  • ddpl's avatar
    ddpl
    Solution Sage

    newpbiuser01 

     

    Create 2 calculated column as below...

     

    Rank = RANKX(FILTER('Table', 'Table'[Store] = EARLIER('Table'[Store])), 'Table'[Spend])
     
    % Spent = 
    DIVIDE(
        'Table'[Spend],
        SUMX(FILTER('Table', 'Table'[Store] = EARLIER('Table'[Store])), 'Table'[Spend])
    )