Forum Discussion

paulvans182's avatar
paulvans182
Helper III
6 years ago
Solved

Calculated Column: Consecutive Wins Column

Good afternoon,

 

I am trying to create a column that calculates the consecutive wins by a player. 

I have the following columns in my data: 

Game ID, Player, Game Board, Results (W/L), Category, Points, Category Result (W/L)

 

I have initially tried to manipulate the code that I found in the following thread
https://community.powerbi.com/t5/Desktop/DAX-formula-for-consecutive-wins/td-p/537369

 

My code for my calculated column is now:

Consecutive Wins = 
        CALCULATE(
            MIN('Game Data'[Game ID]),
            FILTER(
                'Game Data',
                'Game Data'[Game ID] > EARLIER('Game Data'[Game ID])
                && 'Game Data'[Results] = "W"
            )
        ) - 1

Consecutive Wins = 
    VAR Ref_Game_ID = 
        CALCULATE(
            MIN('Game Data'[Game ID]),
            FILTER(
                'Game Data',
                'Game Data'[Player] = EARLIER('Game Data'[Player])
                && 'Game Data'[Game ID] > EARLIER('Game Data'[Game ID])
                && 'Game Data'[Results] = "W"
            )
        )
    VAR Ref = 
        IF(Ref_Game_ID = BLANK(),1000,Ref_Game_ID)
Return
    If(
        'Game Data'[Results] = "W",
        CALCULATE(
            DISTINCTCOUNT('Game Data'[Results]),
            FILTER('Game Data',
                'Game Data'[Player] = EARLIER('Game Data'[Player])
                && 'Game Data'[Game ID] >= EARLIER('Game Data'[Game ID])
          //      && 'Game Data'[Game ID] < Ref
            )
        )
    )

 

However, the results are certainly not correct.

 

For example, in Game ID 27 (the first recorded game), the Consecutive count is 2

 

For Sarah... her results appear as follows:

As you can see, sometimes it appears to work correctly (Game 33, 34), but at other times, starts the count at 2 (Game ID 36).  


I have also noticed that for Game ID 61, 62, 63 where she wins three in a row the results are not incrementing to 3, instead:

 

Please could someone help me debug my code, or alternatively offer an alternate solution?

 

Thank you very much

 

Kind regards,

Paul

 

  • Icey's avatar
    Icey
    6 years ago

    Hi paulvans182 ,

    I find that!😉 Please try this:

    Consecutive Wins =
    VAR Ref_Game_ID =
        CALCULATE (
            MAX ( 'Game Data'[Game ID] ),
            FILTER (
                'Game Data',
                'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                    && 'Game Data'[Game ID] < EARLIER ( 'Game Data'[Game ID] )
                    && 'Game Data'[Results] <> "W"
            )
        )
    VAR Ref =
        IF ( Ref_Game_ID = BLANK (), 'Game Data'[Game ID] - 1, Ref_Game_ID )
    RETURN
        IF (
            'Game Data'[Results] = "W",
            DIVIDE (
                COUNTROWS (
                    FILTER (
                        'Game Data',
                        'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                            && 'Game Data'[Game ID] <= EARLIER ( 'Game Data'[Game ID] )
                            && 'Game Data'[Game ID] > Ref
                    )
                ),
                DISTINCTCOUNT ( 'Game Data'[Category] )
            )
        )

     

    Best Regards,

    Icey

     

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

6 Replies

  • Icey's avatar
    Icey
    Community Support

    Hi paulvans182 ,

    I modify your DAX expression like so:

    Consecutive Wins = 
    VAR Ref_Game_ID =
        CALCULATE (
            MIN ( 'Game Data'[Game ID] ),
            FILTER (
                'Game Data',
                'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                    && 'Game Data'[Game ID] > EARLIER ( 'Game Data'[Game ID] )
                    && 'Game Data'[Results] <> "W"
            )
        )
    VAR Ref =
        IF ( Ref_Game_ID = BLANK (), 1000, Ref_Game_ID )
    RETURN
        IF (
            'Game Data'[Results] = "W",
            DIVIDE (
                COUNTROWS (
                    FILTER (
                        'Game Data',
                        'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                            && 'Game Data'[Game ID] >= EARLIER ( 'Game Data'[Game ID] )
                            && 'Game Data'[Game ID] < Ref
                    )
                ),
                DISTINCTCOUNT ( 'Game Data'[Category] )
            )
        )

    And get something below:

     

    Best Regards,

    Icey

     

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

    • paulvans182's avatar
      paulvans182
      Helper III

      Hi Icey ,

       

      Thank you so much for your response.  The solution that you have provided is almost working perfectly, however, I am getting the following issue:

      1. The consecutive wins for some reason are in reverse order

       

      I literally have copy/pasted your DAX expression from your response. Could you please let me know if there is an additional modification I need to make?

       

      Thank you once again for your help with this problem 

       

      Kind regards,

      • Icey's avatar
        Icey
        Community Support

        Hi paulvans182 ,

        I find that!😉 Please try this:

        Consecutive Wins =
        VAR Ref_Game_ID =
            CALCULATE (
                MAX ( 'Game Data'[Game ID] ),
                FILTER (
                    'Game Data',
                    'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                        && 'Game Data'[Game ID] < EARLIER ( 'Game Data'[Game ID] )
                        && 'Game Data'[Results] <> "W"
                )
            )
        VAR Ref =
            IF ( Ref_Game_ID = BLANK (), 'Game Data'[Game ID] - 1, Ref_Game_ID )
        RETURN
            IF (
                'Game Data'[Results] = "W",
                DIVIDE (
                    COUNTROWS (
                        FILTER (
                            'Game Data',
                            'Game Data'[Player] = EARLIER ( 'Game Data'[Player] )
                                && 'Game Data'[Game ID] <= EARLIER ( 'Game Data'[Game ID] )
                                && 'Game Data'[Game ID] > Ref
                        )
                    ),
                    DISTINCTCOUNT ( 'Game Data'[Category] )
                )
            )

         

        Best Regards,

        Icey

         

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

  • Icey's avatar
    Icey
    Community Support

    Hi paulvans182 ,

    Try this:

    Consecutive Wins Measure = 
        VAR Ref_Game_ID = 
            CALCULATE(
                MIN('Game Data'[Game ID]),
                FILTER(
                    'Game Data',
                    'Game Data'[Player] = MAX('Game Data'[Player])
                    && 'Game Data'[Game ID] > MAX('Game Data'[Game ID])
                    && 'Game Data'[Results] = "W"
                )
            )
        VAR Ref = 
            IF(Ref_Game_ID = BLANK(),1000,Ref_Game_ID)
    Return
        If(
            MAX('Game Data'[Results]) = "W",
            CALCULATE(
                DISTINCTCOUNT('Game Data'[Results]),
                FILTER('Game Data',
                    'Game Data'[Player] = MAX('Game Data'[Player])
                    && 'Game Data'[Game ID] >= MAX('Game Data'[Game ID])
              //      && 'Game Data'[Game ID] < Ref
                )
            )
        )

     This is a measure, not a calculated column.

    If it doesn't work, please share me more sample data.

     

    Best Regards,

    Icey

     

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