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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
MagnoDemarqui
Frequent Visitor

Count quantity of months by last status

Hi Community!

I'm trying to count how much months (or times) a product is being showned on my database, based on the last status.
I already get a way to isolate the last status on a variable, but I'm not knowing how to count how much times this status appeared consecutively.
My actual var to isolate the last status is this:  

VAR Last_Status = CALCULATE(Count(Table'[Status),LASTDATE('Table'[Date])

Do someone know how to achieve this count?

My table is:
ProductDateStatus
Shampoo01/01/2023OK
Soap01/01/2023OK
Toothpaste01/01/2023Out of stock
Shampoo01/02/2023Out of stock
Soap01/02/2023OK
Toothpaste01/02/2023OK
Shampoo01/03/2023Out of stock
Soap01/03/2023Out of stock
Toothpaste01/03/2023OK

The result of the expected count:
ProductStatusMonths
ShampooOut of Stock2
SoapOut of Stock1
ToothpasteOK2

 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You can create a couple of measures like

Last status = IF(
	ISINSCOPE( 'Table'[Product] ),
	SELECTCOLUMNS(
		INDEX(
			1,
			'Table',
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		),
		"@value", 'Table'[Status]
	)
)

Consecutive same status = IF(
	ISINSCOPE( 'Table'[Product] ),
	VAR LastEntry =
		INDEX(
			1,
			'Table',
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		)
	VAR LastStatus =
		SELECTCOLUMNS( LastEntry, "@value", 'Table'[Status] )
	VAR LastStatusDate =
		SELECTCOLUMNS( LastEntry, "@value", 'Table'[Date] )
	VAR PrevEntry =
		INDEX(
			1,
			FILTER(
				'Table',
				'Table'[Date] < LastStatusDate
					&& 'Table'[Status] <> LastStatus
			),
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		)
	VAR PrevDate =
		SELECTCOLUMNS( PrevEntry, "@value", 'Table'[Date] )
	VAR Result = DATEDIFF( PrevDate, LastStatusDate, MONTH )
	RETURN
		Result
)

View solution in original post

3 REPLIES 3
johnt75
Super User
Super User

You can create a couple of measures like

Last status = IF(
	ISINSCOPE( 'Table'[Product] ),
	SELECTCOLUMNS(
		INDEX(
			1,
			'Table',
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		),
		"@value", 'Table'[Status]
	)
)

Consecutive same status = IF(
	ISINSCOPE( 'Table'[Product] ),
	VAR LastEntry =
		INDEX(
			1,
			'Table',
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		)
	VAR LastStatus =
		SELECTCOLUMNS( LastEntry, "@value", 'Table'[Status] )
	VAR LastStatusDate =
		SELECTCOLUMNS( LastEntry, "@value", 'Table'[Date] )
	VAR PrevEntry =
		INDEX(
			1,
			FILTER(
				'Table',
				'Table'[Date] < LastStatusDate
					&& 'Table'[Status] <> LastStatus
			),
			ORDERBY( 'Table'[Date], DESC ),
			PARTITIONBY( 'Table'[Product] ),
			MATCHBY( 'Table'[Product], 'Table'[Date] )
		)
	VAR PrevDate =
		SELECTCOLUMNS( PrevEntry, "@value", 'Table'[Date] )
	VAR Result = DATEDIFF( PrevDate, LastStatusDate, MONTH )
	RETURN
		Result
)

Hello @johnt75, thanks for your reply!

I've tested the second measure, and it worked when I put product and the measure in a table, but when I tryied to add the status it turned all blank

MagnoDemarqui_0-1689601341261.png

In this visualization I will really need to show the status and also make possible to filter the dates, do know some way?

You can use the first measure to show the status. It should also work with date filters or slicers.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.