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
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
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.

Top Solution Authors