Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
paulvans182
Helper III
Helper III

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)

Game Data 2.PNG

 

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:

Results.PNG

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:

Results 2.PNG

 

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

 

Thank you very much

 

Kind regards,

Paul

 

1 ACCEPTED SOLUTION

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] )
        )
    )

7 wonder2.PNG

 

Best Regards,

Icey

 

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

View solution in original post

6 REPLIES 6
Icey
Community Support
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:

7 wonder.gif

 

Best Regards,

Icey

 

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

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

Incrementing.PNG

 

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,

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] )
        )
    )

7 wonder2.PNG

 

Best Regards,

Icey

 

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

Hi @Icey ,

 

Thank you.  It's perfect.  I really appreciate your help with this problem!

 

Kind regards,

 

Paul

Icey
Community Support
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.

 

Good morning,

 

Thank you so much for your response but unfortunately, this does not appear to work.  All the results are just equal to 1.

 

I have uploaded the pbix and raw xlsm file here for you to see. Please let me know whether you can access it?

 

https://www.dropbox.com/s/4m602pufxae34oj/7%20Wonders.pbix?dl=0

https://www.dropbox.com/s/f2lit0kylfnp2ur/7%20Wonders.xlsm?dl=0

 

Thanks again for your help with this matter.

 

Kind regards,

 

Paul

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.