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
samirfarid
Frequent Visitor

Show latest 3 exam scores per student in a table/matrix

Dear community, I have been searching for an answer to this for a couple of hours but I can't really find a solution/starting point.

 

I'm trying to show the latest 3 known scores of every student in a table/matrix. The data is as follows: 

 

Exams

StudentIDExamScoreExamDate
11001/13/2019
2901/13/2019
1802/16/2019
1953/10/2019
1704/12/2019
3603/10/2019

 

Students

StudentIDStudentName
1Jack
2John
3Mary
4Stevano
5Jamal

 

The goal of the visual is to inform a teacher/coach about the latest 3 scores of each student, so the teacher for example knows whether a student needs extra attention. I had the following table / matrix visual in mind:

 

StudentScore 1Score 2Score 3
Jack809570
John  90
Mary  60

 

I really appreciate all the help.

1 ACCEPTED SOLUTION
AnthonyTilley
Solution Sage
Solution Sage

So i would do this by first adding a grouped rank colunm to the exam table that will give a number in decending order to each exam split by student ordered by date desc

 

Rank = RANKX(FILTER(Exams,Exams[StudentID]=EARLIER(Exams[StudentID])),Exams[ExamDate],Exams[ExamDate],DESC,Dense)

 

i would then add three colunms to the student table that calulcate the score based on the id and the rank number from above

 

Score 1 =
--set the rank to your required score
Var r = 1
-- get the student id from the row
var sid = Students[StudentID]
--calculate the sum of the score where student id equals Var sid and rank equals Var r
var lu = CALCULATE(sum(Exams[ExamScore]),Exams[StudentID]=sid,Exams[Rank]=r)
--Return the calculated score
return lu

 

you can repeat this formula three times to givce you the 3 required scores by changing the value for R

 

scores.png

 

i have made a PBIX file that should do what you are asking for 

PBIX FILE





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

6 REPLIES 6
AnthonyTilley
Solution Sage
Solution Sage

So i would do this by first adding a grouped rank colunm to the exam table that will give a number in decending order to each exam split by student ordered by date desc

 

Rank = RANKX(FILTER(Exams,Exams[StudentID]=EARLIER(Exams[StudentID])),Exams[ExamDate],Exams[ExamDate],DESC,Dense)

 

i would then add three colunms to the student table that calulcate the score based on the id and the rank number from above

 

Score 1 =
--set the rank to your required score
Var r = 1
-- get the student id from the row
var sid = Students[StudentID]
--calculate the sum of the score where student id equals Var sid and rank equals Var r
var lu = CALCULATE(sum(Exams[ExamScore]),Exams[StudentID]=sid,Exams[Rank]=r)
--Return the calculated score
return lu

 

you can repeat this formula three times to givce you the 3 required scores by changing the value for R

 

scores.png

 

i have made a PBIX file that should do what you are asking for 

PBIX FILE





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@AnthonyTilley Sorry, back again 😞 The customer requirements changed a bit and now he wants to just show the latest 3 exam scores in a year so the result should be as follows:

 

StudentExam 1 scoreExam 2 scoreExam 3 score
Jack709580
John90  
Mary60  

 

As the filter should take year into account I created a column for Year in the exam table and changed the RankX to be:

 

Rank = 
RANKX(
    FILTER(
        Exams, 
        Exams[StudentId]=EARLIER(Exams[StudentId]) && 
        Exams[ExamDateYear]=EARLIER(Exams[ExamDateYear])
    ),
    Exams[ExamDate],Exams[ExamDate],ASC,Dense)

But I can't seem to figure out how to proceed from here to get the required results.

you will need to make the following changes

 

in the exam table you need a year colunm and change the rank formula 

Rank =

RANKX(
FILTER(Exams,
Exams[StudentID]=EARLIER(Exams[StudentID])
&&
Exams[year]=EARLIER(Exams[year])
),Exams[ExamDate],Exams[ExamDate],DESC,Dense)
 
second in your students table you will need to ad a year and change the Scores measures to include a filter on the year
 
Score 1 =
Var y = VALUE(Students[Year])
Var r = 1
var sid = Students[StudentID]
var lu = CALCULATE(sum(Exams[ExamScore]),Exams[StudentID]=sid,Exams[Rank]=r,Exams[Year]=y)

return lu

make sure you change all three measures 
 
i have amended the PBIX file i shared to meet your new spec
 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




@AnthonyTilleyThanks you for your reply, but how can I change the formula to show this instead:

 

StudentScore 1Score 2Score 3
Jack809570
John90  
Mary60  

 

So the logic should select the last 3 exams based on the date and then show the first exam of the last 3 in the first column the second last in the second column and the last in the last column.

Updeted the file again 

 

there may be an easier way to do this that im not seeing but for me i would introduce a second rank colunm in the exam table that will filp the rank after it is calculated.

 

in this file there is a rank and rank2 colunm

and the colunms in students now pull rank2

 

PBIX File





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Works like a charm! Thank you so much for taking the effort and time to help out Anthony!

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.