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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
olimilo
Responsive Resident
Responsive Resident

Top N + Other

Using the following DAX code to get the Top N + Other, but this seems to fail when I have a Page-level filter applied. Is there any way I can get the desired output with filters on?

 

Top Violations = 
IF (
    CONTAINS (
        TOPN (
            10,
            SUMMARIZE(
				'Violations',
				'Violations'[Violation], 
				"x", 
				COUNTROWS('Violations') + 0
			),
            [x]
        ),
        'Violations'[Violation],
		'Violations'[Violation]
    ),
    'Violations'[Violation],
    "All other violations"
)

For example, if I had the following data:

 

ViolationSectionViolationNoOfViolations
CCE88
AAE84
BBE76
CCA48
CCB47
AAA37
BBB33
CCC24
CCD23
AAD22
BBD21
AAC20
AAB19
BBC18
BBA17


And then tried to get the Top 3 for ViolationSection B, the code above will only output BE and then place everything else under others (right chart). The intended output is BE, BB, BD and Others (left chart).

 

2017-08-24 16_13_05-RSWA Data.xlsx - Excel.png

1 REPLY 1
Eric_Zhang
Employee
Employee

@olimilo

You may create two calculated columns

rank = RANKX (
 FILTER( Table1,Table1[ViolationSection]=EARLIER(Table1[ViolationSection])),
CALCULATE(SUM(Table1[NoOfViolations]),ALLEXCEPT(Table1,Table1[ViolationSection],Table1[Violation])),
,
DESC
)
violation_ = IF(Table1[rank]>3, "Other",Table1[Violation])

Capture.PNG

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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