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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
AJTK
Frequent Visitor

Help calculating a "Streak" from a Date Column

Can anyone help me with a measure? I want to calculate the longest streak (in days) between IncidentType O or P (so ignoring all other incident types). Thanks so much! 

 

Table Name: IncidentTable

IncidentDateIncidentType

11/15/2023O
8/21/2023M
7/19/2023N
6/29/2023S
6/20/2023S
5/10/2023S
4/21/2023N
4/11/2023P
4/10/2023M
4/21/2023M
3/16/2023O
3/3/2023M
3/2/2023S
1 ACCEPTED SOLUTION

@AJTK 

Create this measure:

LongestStreak = 
VAR __FilteredTable = FILTER( Table05 , Table05[IncidentType] IN {"O","P"} )
VAR __StreakCountTable = 
ADDCOLUMNS(
	__FilteredTable,
	"@Days", 
		VAR __CurrentDate = Table05[IncidentDate]
		VAR __PreviousDate = OFFSET( -1 , ORDERBY( Table05[IncidentDate] ) )
		VAR __Result = 
			IF( NOT ISBLANK(__CurrentDate) && NOT ISBLANK( __PreviousDate ),
				INT( __CurrentDate - __PreviousDate )
			)
		RETURN
			__Result
)
VAR ____LongestStreak = MAXX( __StreakCountTable , [@Days])
RETURN
	____LongestStreak

Fowmy_1-1705091874787.png

 

Fowmy_0-1705091858857.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

5 REPLIES 5
Fowmy
Super User
Super User

@AJTK 

What would be the expected result based on your sample data?

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

AJTK
Frequent Visitor

218 days (4/11/23 - 11/15/23). The longest streak between O&P, P&P, or O&O. 

@AJTK 

Create this measure:

LongestStreak = 
VAR __FilteredTable = FILTER( Table05 , Table05[IncidentType] IN {"O","P"} )
VAR __StreakCountTable = 
ADDCOLUMNS(
	__FilteredTable,
	"@Days", 
		VAR __CurrentDate = Table05[IncidentDate]
		VAR __PreviousDate = OFFSET( -1 , ORDERBY( Table05[IncidentDate] ) )
		VAR __Result = 
			IF( NOT ISBLANK(__CurrentDate) && NOT ISBLANK( __PreviousDate ),
				INT( __CurrentDate - __PreviousDate )
			)
		RETURN
			__Result
)
VAR ____LongestStreak = MAXX( __StreakCountTable , [@Days])
RETURN
	____LongestStreak

Fowmy_1-1705091874787.png

 

Fowmy_0-1705091858857.png

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

AJTK
Frequent Visitor

Thanks, but I get an error on this part: 

ORDERBY( Table05[IncidentDate] ) )

It says "Parameter is not the correct type." I just replaced "Table05" with my actual table name & column [IncidentDate]

@AJTK 

Sharing a dummy Power BI file representing your scenario would be beneficial. You can save the Power BI file on Google Drive or any other cloud storage platform and provide the link here. Kindly ensure that permission is granted to open the file.

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors