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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
tw15ns
Frequent Visitor

Top n Values

I am tryng to use Power BI in a school setting and want to be able to get the sum of the top 3 results for each student. I have tried TOPN but the problem is that if a student's top results are say 4 grade 8s then TOPN would return 32 rather than the 24 I want. Any ideas as how to resolve this would be appreciated.  

1 ACCEPTED SOLUTION
Anonymous
Not applicable

 

[TOPN Sum] =
var topn_ = 3
return
SUMX(
    DISTINCT( Result[ID] ),
    CALCULATE(    
        var VisibleSubjects =
            DISTINCT( Result[Subject] )
        var SubjectsOrdered =
            ADDCOLUMNS(
                VisibleSubjects,
                "@SubjectNumber",
                    RANKX(
                        VisibleSubjects,
                        Result[Subject],,
                        ASC,
                        // this option
                        // does not matter
                        // here
                        DENSE 
                    )
            )
        var SubjectNumberMagnitude =
            LEN(
                MAXX(
                    SubjectsOrdered,
                    [@SubjectNumber]
                )
            )
        var ResultWithSubject =
            SUMMARIZE(
                Result,
                Result[Result],
                Result[Subject]
            )
        var Result = 
            SUMX(
                TOPN(topn_,
                    NATURALINNERJOIN(
                        ResultWithSubject,
                        SubjectsOrdered
                    ),
                    Result[Result]
                        * 10^SubjectNumberMagnitude
                        + [@SubjectNumber]
                ),
                Result[Result]
            )
        return
            Result
    )
)

 

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

 

[TOPN Sum] =
var topn_ = 3
return
SUMX(
    DISTINCT( Result[ID] ),
    CALCULATE(    
        var VisibleSubjects =
            DISTINCT( Result[Subject] )
        var SubjectsOrdered =
            ADDCOLUMNS(
                VisibleSubjects,
                "@SubjectNumber",
                    RANKX(
                        VisibleSubjects,
                        Result[Subject],,
                        ASC,
                        // this option
                        // does not matter
                        // here
                        DENSE 
                    )
            )
        var SubjectNumberMagnitude =
            LEN(
                MAXX(
                    SubjectsOrdered,
                    [@SubjectNumber]
                )
            )
        var ResultWithSubject =
            SUMMARIZE(
                Result,
                Result[Result],
                Result[Subject]
            )
        var Result = 
            SUMX(
                TOPN(topn_,
                    NATURALINNERJOIN(
                        ResultWithSubject,
                        SubjectsOrdered
                    ),
                    Result[Result]
                        * 10^SubjectNumberMagnitude
                        + [@SubjectNumber]
                ),
                Result[Result]
            )
        return
            Result
    )
)

 

WOW!

 

I have no idea how this works but it does the job. Thanks for all your help.

Anonymous
Not applicable

Very simple ideas. Use TOPN wrapped in DISTINCT.

How about trying to get a top ten by client, and by master client? Something like the below.

 

When I try to do this in BI using TopN on client name by sales amount, it only filters client name, i.e. if sub client 1 has 300k, and sub client 2 has 200k, it skips to the next master client that has that 200k.

 

bengrove_0-1630438553803.png

 

Thanks for the reply. I understand the logic of your solution unfortunately I cannot get it to work.

I write the measure as below but it still seems to be totalling up all instances of the top values e.g. the third student has top results of two at 8.5 and 3 at 8 hence a total of 41. Am I doing something stupid!!

tw15ns_0-1624967346538.png

Thanks again

Anonymous
Not applicable

 

[TOPN Sum] =
var topn_ = 3
var Result =
    SUMX(
        DISTINCT( Result[ID] ),
        CALCULATE(
            SUMX(
                TOPN(topn_,
                    DISTINCT( Result[Result] ),
                    Result[Result],
                    DESC
                ),
                Result[Result]
            )
        )
    )
return
    Result

 

Works correctly also on the Total row...

 

Another way of calculation:

[TOPN Sum] =
var topn_ = 3
var IDsWithTopNResults = 
    GENERATE(
        DISTINCT( Result[ID] ),
        CALCULATETABLE(
            TOPN(topn_,
                DISTINCT( Result[Result] ),
                Result[Result],
                DESC
            )
        )
    )
var Result =
    SUMX(
        IDsWithTopNResults,
        Result[Result]
    )
return
    Result
Anonymous
Not applicable

Then do it the other way round: first DISTINCT, then TOPN. That should definitely get you what you need.

Thanks and sorry for being a pain but your solution works too well. With the list below it gives a result of 21 (sum of the top 3 distinct values), but what I need is to add together the two 8s and  then only one of the 7s. I can do this in Excel with the LARGE function but not in DAX.

  • Art - 8
  • Maths - 8
  • Science - 7
  • Spanish - 7
  • English - 6
  • Music - 5

Thanks again

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors