Forum Discussion
Attendance Streaks
Happy New Year to you all.
Could one of you clever people point me to understanding how I can write a dax measure that would count the number of absence streaks a pupil has and reseting the counter until it starts again.
The column DAX Measure is what I am looking to add to the table
| id | date | mark | statistical meaning | DAX Measure |
| 1 | 1/9/2024 | / | Present | |
| 1 | 1/9/2024 | I | Absent | 1 |
| 1 | 2/9/2024 | I | Absent | 2 |
| 1 | 2/9/2024 | \ | Present | |
| 1 | 3/9/2024 | / | Present | |
| 1 | 3/9/2024 | L | Absent | 1 |
| 1 | 4/9/2024 | L | Absent | 2 |
| 1 | 4/9/2024 | L | Absent | 3 |
| 1 | 5/9/2024 | \ | Present | |
| 2 | 1/9/2024 | L | Absent | 1 |
| 2 | 1/9/2024 | L | Absent | 2 |
| 2 | 2/9/2024 | L | Absent | 3 |
| 2 | 2/9/2024 | L | Absent | 4 |
| 2 | 3/9/2024 | L | Absent | 5 |
| 2 | 3/9/2024 | L | Absent | 6 |
| 2 | 4/9/2024 | / | Present | |
| 2 | 4/9/2024 | \ | Present | |
| 2 | 5/9/2024 | l | Absent | 1 |
Many thanks
Hi CEllis
To count absence streaks for each pupil in DAX, you can create a measure that:
Tracks consecutive absences (marked as "I" or "L").
Resets the count when a "Present" mark ("/" or "") is encountered.
The measure uses CALCULATE and FILTER to count rows for consecutive absences and reset the count when a present mark appears, ensuring that the absence streaks are tracked for each pupil individually.
DAX:
Absence Streaks = VAR CurrentStudent = 'Table'[id] VAR CurrentDate = 'Table'[date] VAR CurrentMark = 'Table'[mark] VAR PreviousAbsence = CALCULATE( MAXX( FILTER( 'Table', 'Table'[id] = CurrentStudent && 'Table'[date] < CurrentDate && ('Table'[mark] = "I" || 'Table'[mark] = "L") ), 'Table'[date] ) ) VAR AbsenceStreak = IF( CurrentMark = "I" || CurrentMark = "L", IF( ISBLANK(PreviousAbsence), 1, 1 + CALCULATE( COUNTROWS('Table'), FILTER( 'Table', 'Table'[id] = CurrentStudent && 'Table'[date] > PreviousAbsence && 'Table'[mark] = "I" || 'Table'[mark] = "L" ) ) ), BLANK() ) RETURN AbsenceStreakDid I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos" πKind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
3 Replies
- Poojara_D12Super User
Hi CEllis
To count absence streaks for each pupil in DAX, you can create a measure that:
Tracks consecutive absences (marked as "I" or "L").
Resets the count when a "Present" mark ("/" or "") is encountered.
The measure uses CALCULATE and FILTER to count rows for consecutive absences and reset the count when a present mark appears, ensuring that the absence streaks are tracked for each pupil individually.
DAX:
Absence Streaks = VAR CurrentStudent = 'Table'[id] VAR CurrentDate = 'Table'[date] VAR CurrentMark = 'Table'[mark] VAR PreviousAbsence = CALCULATE( MAXX( FILTER( 'Table', 'Table'[id] = CurrentStudent && 'Table'[date] < CurrentDate && ('Table'[mark] = "I" || 'Table'[mark] = "L") ), 'Table'[date] ) ) VAR AbsenceStreak = IF( CurrentMark = "I" || CurrentMark = "L", IF( ISBLANK(PreviousAbsence), 1, 1 + CALCULATE( COUNTROWS('Table'), FILTER( 'Table', 'Table'[id] = CurrentStudent && 'Table'[date] > PreviousAbsence && 'Table'[mark] = "I" || 'Table'[mark] = "L" ) ) ), BLANK() ) RETURN AbsenceStreakDid I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos" πKind Regards,
Poojara
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS