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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
MoonLightFlower
Regular Visitor

Need Help for a matrix with multiples Rankx

Dear BI community,
I need your help about  a matrix with multiple Rankx :


What i am trying to do is a matrix
with the rows :

  • country
  • Category (Take Top 10 Category with the higher SalesAmount)
  • SubCategory(Take Top 5Category with the higher Order Qty and only in the top 10 category)
  • Product (Take Top 5 product with the highest Order Qty, but it should be in the Top 5 sub category )
    Just a little remind the Top Category is ranked with the the Sales Amount
    and the Top SubCategory and Product use the  Order Qty

Measure :

  • Sum(AmountSales)
  • Sum(Qty)

For the sample i used the adventure Works DW 2022 :

MoonLightFlower_1-1718737893623.png



At first i plan to do 3 Rankx :

RankxCategory =
RANKX(ALLSELECTED(DimProductCategory[EnglishProductCategoryName]),CALCULATE(SUM(FactResellerSales[SalesAmount]),ALLSELECTED(DimProductSubcategory[EnglishProductSubcategoryName]
,DimProduct[EnglishProductName]
)),,DESC,Dense)


RankxSubCategory =
RANKX(ALLSELECTED(DimProductSubcategory[EnglishProductSubcategoryName]),CALCULATE(SUM(FactResellerSales[OrderQuantity])
,ALLSELECTED(DimProduct[EnglishProductName])
),,DESC,Dense)


RankxProduct =
RANKX(ALLSELECTED(DimProduct[EnglishProductName]),CALCULATE(SUM(FactResellerSales[OrderQuantit])))

and create some measure with if  :
TopNSalesAmout = IF(RankxCategory<10 && RankxSubCategory<5 && RankxProduct<5,SUM(FactResellerSales[SalesAmount])

TopNQty = IF(RankxCategory<10 && RankxSubCategory<5 && RankxProduct<5,,SUM(FactResellerSales[SalesAmount])


But i don't think it's the best solution. 
if anyone has some idea 🙂

2 REPLIES 2
Anonymous
Not applicable

Hi  @MoonLightFlower ,

I created some data:

vyangliumsft_0-1718775468176.png

Here are the steps you can follow:

1. Create calculated column.

 

Sum_Category =
 SUMX(
    FILTER(ALL('Table'),
    [country]=EARLIER('Table'[country])&&'Table'[Category]=EARLIER('Table'[Category])),
    [AmountSales])
Sum_SubCategory =
 SUMX(
    FILTER(ALL('Table'),   [country]=EARLIER('Table'[country])&&'Table'[Category]=EARLIER('Table'[Category])&&'Table'[SubCategory]=EARLIER('Table'[SubCategory])),
    [Qty])

 

vyangliumsft_1-1718775468177.png

2. Create measure.

This is a rule I created myself, and you can change the functions in it to the following form.

 

 

FILTER(
    _table1,
    [Rank_Category] <=10 && [Rank_SubCategory]<=5&&[Rank_Product]<=5)
Flag =
VAR _table1 =
    ADDCOLUMNS (
        ALL ( 'Table' ),
        "Rank_Category",
            RANKX (
                FILTER ( ALL ( 'Table' ), [country] = EARLIER ( 'Table'[country] ) ),
                [Sum_Category],
                ,
                DESC,
                DENSE
            ),
        "Rank_SubCategory",
            RANKX (
                FILTER (
                    ALL ( 'Table' ),
                    [country] = EARLIER ( 'Table'[country] )
                        && 'Table'[Category] = EARLIER ( 'Table'[Category] )
                ),
                [Sum_SubCategory],
                ,
                DESC,
                DENSE
            ),
        "Rank_Product",
            RANKX (
                FILTER (
                    ALL ( 'Table' ),
                    [country] = EARLIER ( 'Table'[country] )
                        && 'Table'[Category] = EARLIER ( 'Table'[Category] )
                        && 'Table'[SubCategory] = EARLIER ( 'Table'[SubCategory] )
                ),
                [Qty],
                ,
                DESC,
                DENSE
            )
    )
VAR _table2 =
    FILTER (
        _table1,
        [Rank_Category] <= 1
            && [Rank_SubCategory] <= 3
            && [Rank_Product] <= 5
    )
VAR _column =
    SELECTCOLUMNS ( _table2, "test", [Product] )
RETURN
    IF ( MAX ( 'Table'[Product] ) IN _column, 1, 0 )

 

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_0-1718775638797.png

4. Result:

vyangliumsft_1-1718775638801.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Thanks for your time Liu Yang,
I just have a issue with the solution :
-i want to be able to filter the country, Category, SubCategory, Product.
So if i remove the product A224, the ranking will be recalculated and i should still have 5 product.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.