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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
mati_gouda
Frequent Visitor

Top N categories within selected date but display values monthly even if other category is larger

Hello,

 

I tried every possible option I was able to find and somehow still does have some issues and errors. 
Case scenario is: we have a table with Month Year (like FEB-2024), some sub-category and simple number of cases. Like below

 

mati_gouda_0-1711521579165.png

unnamed (1).png

I have a slicer on the page with dates, so assume that we have selected dates: 01/02/2024–01/03/2024. 
What I want to do is take the 3 subcategories with the highest number of cases within those dates (so in those two months). SubCategories are Test Date Transfer, Certificate Enquiry and Payment.

 

mati_gouda_3-1711521962139.png

And I want to show only them and their values on a table (or line chart or whatever) even though those values may not be the highest per month (like Payment Sub Category → it's in the top 3 in February but not in March - but that does not matter because in the whole period it's in the top 3). 

 

So essentially I want to take top 3 subCategories for the whole period and display only those SubCategories month by month (and it's values of course) even thought they may not be in top categories per one month. 

 

I wrote the measure, which I understand, but I have an impression that ALLSELECTED on dates behaves weirdly (while ALLSELECTED on categories seems to be correct). 

 

Case Count by Sub Category RANK TEST =
VAR _rank =
    RANKX (
        CALCULATETABLE (
            VALUES ( Dataset_Cases[SubCategory] ),
            ALLSELECTED ( Dataset_Cases[SubCategory] ),
            ALLSELECTED ( Calendar_Table[Month & Year - short] ),
            ALLSELECTED ( Dataset_Cases[DateOpened] )
        ),
        CALCULATE ( COUNT ( Dataset_Cases[CaseID] ) ),
        ,
        DESC,
        DENSE
    )
RETURN
    _rank

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @mati_gouda 

 

Please try this:

Here I create a set of sample for your reference:

vzhengdxumsft_0-1711607158211.png

Then add a calculate column to rank the values:

 

Rank =
RANKX (
    ALL ( 'Table' ),
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Sub Category] = EARLIER ( 'Table'[Sub Category] )
        )
    ),
    ,
    DESC,
    DENSE
)

 

vzhengdxumsft_1-1711607258849.png

Next, create a measure:

 

SumValue =
VAR _currentRank =
    MAX ( 'Table'[Rank] )
RETURN
    IF (
        _currentRank <= 3,
        SUMX (
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Rank] = _currentRank ),
            'Table'[Value]
        )
    )

 

The result is as follow:

vzhengdxumsft_2-1711607360775.pngvzhengdxumsft_3-1711607367236.png

 

Best Regards

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

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @mati_gouda 

 

Please try this:

Here I create a set of sample for your reference:

vzhengdxumsft_0-1711607158211.png

Then add a calculate column to rank the values:

 

Rank =
RANKX (
    ALL ( 'Table' ),
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Sub Category] = EARLIER ( 'Table'[Sub Category] )
        )
    ),
    ,
    DESC,
    DENSE
)

 

vzhengdxumsft_1-1711607258849.png

Next, create a measure:

 

SumValue =
VAR _currentRank =
    MAX ( 'Table'[Rank] )
RETURN
    IF (
        _currentRank <= 3,
        SUMX (
            FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Rank] = _currentRank ),
            'Table'[Value]
        )
    )

 

The result is as follow:

vzhengdxumsft_2-1711607360775.pngvzhengdxumsft_3-1711607367236.png

 

Best Regards

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

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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