Forum Discussion

donttakemyname's avatar
donttakemyname
Frequent Visitor
3 years ago
Solved

Count Most Recent Streak

I've been looking at the similar questions here and I just cannot get anything to work. The ultimate goal of this is to make a leader board of who has the longest streak. left hand side is names and...
  • Martin_D's avatar
    Martin_D
    3 years ago

    I'd recommend to create a date table with a week number, unique across all years, like 202252. Then the following code will give you the longest streak. The idea is:

    • Check for each person
    • what was the last week not included in a streak
    • count the weeks after which all belong to the streak
    • the longest streak is the maximum streak length for a single person

     

    Longest Streak = 
    // per person, calculate the latest week under 80, then counting the weeks after are the streak length
    VAR _PersonsAndStreakLength =
        ADDCOLUMNS ( 
            SUMMARIZECOLUMNS (
                'Name'[NameKey]
            ),
            "@StreakLength",
            // get the weeks and their WeekOver80 flag
            VAR _WeeksAndStreaks =
                ADDCOLUMNS (
                    SUMMARIZECOLUMNS ( 
                        'Date'[WeekKey]
                    ),
                    "@WekOver80",
                    CALCULATE ( COALESCE ( [WeekOver80], 0 ) )
                )
            VAR _LastWeekWithoutStreak = MAXX ( FILTER ( _WeeksAndStreaks, [@WekOver80] = 0 ), [WeekKey] )
    
            VAR _StreakLength = COUNTROWS ( FILTER ( _WeeksAndStreaks, [WeekKey] > _LastWeekWithoutStreak ) )
    
            RETURN
    
            _StreakLength
        )
    
    RETURN
    
    MAXX ( _PersonsAndStreakLength, [@StreakLength] )

     

    Check this file for details. Code to generate the date table in Power Query is included, if needed. The result looks like:
    Longes streak top/flop 20