Forum Discussion

rynhowl's avatar
rynhowl
Microsoft Employee
3 years ago
Solved

Summarize a summarize

How do I do this SQL query in DAX? I've searched the forums; I don't know if I'm just searching the wrong terms or what, but I can't find anything that works.

 

SELECT

    AVG(SumOfMaxGrades) AS ClassAverageGrade

FROM (

    SELECT

        Student,

        SUM(MaxGrade) AS SumOfMaxGrades

    FROM (

        SELECT

            Student,

            Assignment,

            MAX(Grade) AS MaxGrade

        FROM TableA

        GROUP BY Student, Assignment

    ) B

    GROUP BY Student

) C

 

Additional context if needed: In the example, I have students who get mutliple attempts on each assignment and only the max score is kept for each one, then their final grade is the sum of all assignment grades, and I want to find the class average. (Please don't try to correct my grading logic here, this is a made up example, the actual dataset has nothing to do with grading.)

 

StudentAssignmentGrade
Bob190
Bob195
Bob285
Bob287
Sally192
Sally188
Sally270
Sally275
John145
John150
John165
John265
John270

 

StudentAssignmentMax Grade
Bob195
Bob287
Sally192
Sally275
John165
John270

 

Student

Sum of assignment grades

Bob182
Sally167
John135

 

Class Average Grade: 161.33

 

  • Hi rynhowl 

     

    Try this measure:

    Class Average Grade =
    VAR _A =
        SUMMARIZE (
            'Table',
            'Table'[Student],
            'Table'[Assignment],
            "Max Grade", MAX ( 'Table'[Grade] )
        )
    VAR _B =
        SUMX ( _A, [Max Grade] ) / COUNTROWS ( VALUES ( 'Table'[Student] ) )
    RETURN
        _B
    

     

    Output:

     

     

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

    Appreciate your Kudos!! 

    LinkedIn | Twitter | Blog | YouTube 

2 Replies

  • Hi rynhowl 

     

    Try this measure:

    Class Average Grade =
    VAR _A =
        SUMMARIZE (
            'Table',
            'Table'[Student],
            'Table'[Assignment],
            "Max Grade", MAX ( 'Table'[Grade] )
        )
    VAR _B =
        SUMX ( _A, [Max Grade] ) / COUNTROWS ( VALUES ( 'Table'[Student] ) )
    RETURN
        _B
    

     

    Output:

     

     

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

    Appreciate your Kudos!! 

    LinkedIn | Twitter | Blog | YouTube 

    • rynhowl's avatar
      rynhowl
      Microsoft Employee

      Thank you, this was a good idea 🙂