Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
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
Solved! Go to 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] )
)
)
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 @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.
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:
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] )
)
)
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
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
User | Count |
---|---|
125 | |
106 | |
99 | |
63 | |
62 |
User | Count |
---|---|
135 | |
116 | |
101 | |
71 | |
61 |