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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
soldous
Advocate II
Advocate II

Count rows for top n categories

Hi all,

 

I need to count rows in the table which is filtered only for top n most frequent categories.

 

I know how to get these categories:

TOPN (
        5, 
        VALUES ( Table[Category Level 1] ), 
        RANKX( ALL( Table[Category Level 1] ),COUNTROWS(Table),,ASC)
    )

But I don't know how to use this as i filter for CALCULATE(COUNTROWS(Table),..)

I was thinking about using IN operator and something like this:

CALCULATE(COUNTROWS(Table),FILTER(Table,Table[Category Level 1] IN CALCULATETABLE(TOPN (
        5, 
        VALUES ( Table[Category Level 1] ), 
        RANKX( ALL( Table[Category Level 1] ),COUNTROWS(Table),,ASC)
    ))))

but it doesn't work.

 

Could someone help?

The solution without FILTER would be much appreciated because the table is quite huge and the FILTER function is incredibly slow.

 

Thanks a lot.

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Hi, @soldous , a table itself can be used as filter in CALCULATE. You may want to try a measure in this pattern,

 

Measure =
VAR __topn =
    TOPN (
        5,
        VALUES ( Table[Category Level 1] ),
        RANKX ( ALL ( Table[Category Level 1] ), COUNTROWS ( Table ),, ASC )
    )
RETURN
    CALCULATE ( COUNTROWS ( Table ), __topn )

It's a general idea; you're supposed to base the measure on specific context of your data model.

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

Hi, @soldous , a table itself can be used as filter in CALCULATE. You may want to try a measure in this pattern,

 

Measure =
VAR __topn =
    TOPN (
        5,
        VALUES ( Table[Category Level 1] ),
        RANKX ( ALL ( Table[Category Level 1] ), COUNTROWS ( Table ),, ASC )
    )
RETURN
    CALCULATE ( COUNTROWS ( Table ), __topn )

It's a general idea; you're supposed to base the measure on specific context of your data model.

 

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Is there a way to connect this to a slicer?

@CNENFRNL Thank you very much!

There is always something new to learn 🙂

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors