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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
TBSST
Frequent Visitor

Count if X ocures 3 times in a row

Hi All, 

 

I think i have reached a point in PowerBI capabilities, but hopeing a smart person here can say otherwise!

 

I am trying to get a count of when an event happens 3 times in a row. 

 

IDDateValue
a1/01/20231
a2/01/20231
a3/01/20232
a4/01/20231
b1/01/20231
b2/01/20231
b3/01/20231
b4/01/20232
c1/01/20231
c2/01/20232
c3/01/20231
c4/01/20231
c5/01/20231

 

I would like to say the max 3 dates = 1, then count 1.  So with the above, it would only count ID c, becasue the last 3 dates are 1 and ignore a and b because they do not have 3 in a row with the most recent dates for the ID. 

 

I would like like to add in a table a count of how many '1' an ID is up to. So for example, client a would be at 1, client b would be ar 0, and client c would be at 3. 

 

Any help would be appreciated. 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @TBSST 

Here's how I would tackle it (PBIX attached):

Latest Run Length = 
VAR MinDate = MIN ( Data[Date] )
VAR MaxDate = MAX ( Data[Date] )
VAR MaxDateBefore1 =
    CALCULATE ( MAX ( Data[Date] ), Data[Value] <> 1 )
VAR MaxDateBefore1_Adj =
    COALESCE ( MaxDateBefore1, MinDate - 1 )
RETURN
    INT ( MaxDate - MaxDateBefore1_Adj )
[# ID with Latest Run Length >= 3] = 
COUNTROWS (
    FILTER ( VALUES ( Data[ID] ), [Latest Run Length] >= 3 )
)

I added a 4th ID "d" with five 1s in a row.

OwenAuger_0-1696840394293.png

The above measures operate within the current filter context. If there is any special logic regarding date filtering or handling non-consecutive dates, they would need to be adjusted.

 

Does something like this work for you?

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

Hi @TBSST 

Here's how I would tackle it (PBIX attached):

Latest Run Length = 
VAR MinDate = MIN ( Data[Date] )
VAR MaxDate = MAX ( Data[Date] )
VAR MaxDateBefore1 =
    CALCULATE ( MAX ( Data[Date] ), Data[Value] <> 1 )
VAR MaxDateBefore1_Adj =
    COALESCE ( MaxDateBefore1, MinDate - 1 )
RETURN
    INT ( MaxDate - MaxDateBefore1_Adj )
[# ID with Latest Run Length >= 3] = 
COUNTROWS (
    FILTER ( VALUES ( Data[ID] ), [Latest Run Length] >= 3 )
)

I added a 4th ID "d" with five 1s in a row.

OwenAuger_0-1696840394293.png

The above measures operate within the current filter context. If there is any special logic regarding date filtering or handling non-consecutive dates, they would need to be adjusted.

 

Does something like this work for you?

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Thank you so much! This worked great 🙂

ValtteriN
Super User
Super User

Hi,

Here is one way to do this:


Dax: (last 3 are 1)

Measure =
var _latestdate = CALCULATE(MAX('Table'[Date]),ALL('Table'))
var _currentdate = MAX('Table'[Date])

var _current = MAX('Table'[Value])
var _previous =
CALCULATE (
            MAX('Table'[Value]),
            OFFSET (
                -1,ALLSELECTED('Table'[Date]),ORDERBY(MAX('Table'[Date]))
            ))
var _2previous = CALCULATE (
            MAX('Table'[Value]),
            OFFSET (
                -2,ALLSELECTED('Table'[Date])
                ,ORDERBY(MAX('Table'[Date]))
            ))
return

IF(_current=_previous && _current = _2previous && _latestdate=_currentdate,1,0)



Dax: (how many 1 up to):

Repeat1Count =
var _id = MAX('Table'[ID])
var _Mdate = CALCULATE(MAX('Table'[Date]),ALL('Table'),_id='Table'[ID],'Table'[Value]<>1) //lates date for non 1 value
var _filteredcountof1 =  COUNTROWS(FILTER(ALL('Table'),'Table'[ID]=_id && 'Table'[Date]>_Mdate && 'Table'[Value]=1))
return
_filteredcountof1




End result:

ValtteriN_0-1696840217339.png

 

The values are as expected.

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/









Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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