Forum Discussion
Expand count() to allow if statement to return data from different table
Hi,
I'm trying to work out the year grade for enrolled students. We have two tables, one where we store the pupil data and another storing their school history, sometimes for future leavers the staff enter their leaving grade early so I need to use the current YG and if that is blank then I need to use their leaving YG.
Pupil Table
| PupilID | Forename | Surname | CurrentYG | |
| Pupil1 | John | Doe | YR04 | |
| Pupil2 | Jane | Doe | YR05 | |
| Pupil3 | Simon | Brown |
School History
| PupilID | StartDate | LeavingDate | LeavingYG |
| Pupil1 | 01/09/2016 | 01/12/2017 | YR03 |
| Pupil1 | 01/09/2018 | ||
| Pupil2 | 01/09/2016 | ||
| Pupil3 | 01/09/2016 | 21/07/2018 | YR02 |
The query below returns the current grade for enrolled student.
| PupilID | Forename | Surname | If on-Roll, CurrentYG | if Left, leaving YG | Expected YG Output |
| Pupil1 | John | Doe | YR04 | YR04 | |
| Pupil2 | Jane | Doe | YR05 | YR05 | |
| Pupil3 | Simon | Brown | YR02 | YR02 |
3 Replies
- v-piga-msft
Resident Rockstar
Hi MBegum ,
I've tried to expand the COUNT() part with an IF statement but that just gives me an error so not sure what I'm doing wrong.Current Grade= CALCULATE(COUNT(if(Pupil[CurrentYG]=Blank(), RELATED(SchoolHistory[LeavingYG]),FILTER(SchoolHistory, SchoolHistory[StartDate] <= TODAY() && PupilSession[LeavingDate] = BLANK() ||PupilSession[StartDate] <= TODAY() && PupilSession[LeavingDate] >= TODAY()))Based on your formula, it seems that the syntax of this measure is incorrect.
I'm afraid that we cannot use IF function like that. You should use IF function outside of calculate function, maybe like below.
Current Grade = IF ( Pupil[CurrentYG] = BLANK (), CALCULATE ( COUNT ( Pupil[CurrentYG] ), FILTER ( SchoolHistory, SchoolHistory[StartDate] <= TODAY () && PupilSession[LeavingDate] = BLANK () || PupilSession[StartDate] <= TODAY () && PupilSession[LeavingDate] >= TODAY () ) ) )If you still need help, please share your desired output so that we can help further on it.
Best Regards,
Cherry
- MBegumFrequent Visitor
Thanks for your feedback. I tried your suggestion but couldn't get it working. I've added an edited section with my expected output.
- v-piga-msft
Resident Rockstar
Hi MBegum ,
Sorry for the delay.
However, what is the PupilSession[LeavingDate]? It seems that you have the third table PupilSession?
If it is convenient, could you also share the sample data of the table PupilSession?
Best Regards,
Cherry