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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

Top Kudoed Authors