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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Matthew9900
New Member

Count number of consecutive times

I have this table that has the information about the result of an event that can be "Won" and "Lost".

I would like a measure in DAX that calculates the number of consecutive times that "Won" is achieved without "Lost" appearing.

In this example it would be 4

Result
Won
Won
Lost
Won
Lost
Won
Won
Won
Won
Lost
Lost
1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @Matthew9900 ,

 

I suggest you to add an index column and then add a group column in Power Query Editor.

List.Count(
let
_Index = [Index]
in
Table.SelectRows(#"Added Index",each [Index]<=_Index and [Result] = "Lost")[Result])

New Table:

vrzhoumsft_0-1681809894899.png

Measure:

Consecutive win times = 
VAR _SUMMARIZE =
    SUMMARIZE (
        FILTER ( 'Table', 'Table'[Result] = "Won" ),
        'Table'[Group],
        "Count", COUNT ( 'Table'[Index] )
    )
RETURN
    MAXX ( _SUMMARIZE, [Count] )

Result is as below.

vrzhoumsft_1-1681810618695.png

 

Best Regards,
Rico Zhou

 

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
ribisht17
Super User
Super User

Hi @Matthew9900 

 

The solution given by @v-rzhou-msft  is great.

 

You can also do the 1st step with DAX as follows -create a Group COLUMN

Group = CALCULATE( COUNTX( 'Table','Table'[Index]),
                                       
                                        FILTER(ALL('Table'),'Table'[Index] <= EARLIER('Table'[Index])
                                       
                                               ),'Table'[Result]="Lost"
                            )
                               
                       
 
Regards,
Ritesh
v-rzhou-msft
Community Support
Community Support

Hi @Matthew9900 ,

 

I suggest you to add an index column and then add a group column in Power Query Editor.

List.Count(
let
_Index = [Index]
in
Table.SelectRows(#"Added Index",each [Index]<=_Index and [Result] = "Lost")[Result])

New Table:

vrzhoumsft_0-1681809894899.png

Measure:

Consecutive win times = 
VAR _SUMMARIZE =
    SUMMARIZE (
        FILTER ( 'Table', 'Table'[Result] = "Won" ),
        'Table'[Group],
        "Count", COUNT ( 'Table'[Index] )
    )
RETURN
    MAXX ( _SUMMARIZE, [Count] )

Result is as below.

vrzhoumsft_1-1681810618695.png

 

Best Regards,
Rico Zhou

 

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

Ahmedx
Super User
Super User

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
https://1drv.ms/u/s!AiUZ0Ws7G26Rhxiw0R7SH-9Yuurj?e=cK4yaP
Screen Capture #848.pngScreen Capture #849.png

ribisht17
Super User
Super User

@Matthew9900 

 

Please check https://community.powerbi.com/t5/DAX-Commands-and-Tips/DAX-How-to-Count-the-Consecutive-Occurrences/...

 

I also tried it with this DAX function (hopefully easier)

 

Step1

Count Cons Won = if(CALCULATE(max('Table'[Result]),'Table'[Index]=max('Table'[Index])+1 ||'Table'[Index]=max('Table'[Index])-1)="Won",1,0)

 

Step 2
Cumulative ADDITION = CALCULATE( SUMX(
                                        SUMMARIZE(
                                                   'Table',
                                                    'Table'[Index],
                                                    "CUMULATIVE ADD",
                                                     [Count Cons Won]
                                                 ),[CUMULATIVE ADD]),
                                        FILTER(ALL('Table'),'Table'[Index] <= MAX('Table'[Index])
                                       
                                               )
                                       
                                )
                       
 
ribisht17_0-1680926017486.png

 

 

Regards,

Ritesh

Thank you very much ribisht17

The solution is working but it is not adequate. The objective of this measurement is to find the longest winning streak, in this example it is giving me 6, but the correct answer should be 4.


Captura de pantalla 2023-04-08 125831.png

That part is there in the link that I shared with you

 

Regards,

Ritesh

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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