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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Select values based on distinct columns and ordering by date

I need help creating a DAX command:

 

Sample Data:

CoverWorkcaseUserIdProvidersCountWorkcase Update DateCover Update Date
123459875xxx12371/22/2020 
123459875xxx123101/20/2020 
123459875xxx456111/25/2020 
67890 xxx12310 2/20/2020
678901234xxx12362/25/2020 

 

I want a measure to calculate ProvidersCount for each unique cover, workcase, userID combination. But it needs to meet this criteria:

- If cover, workcase, userID combination happens more than once, select the ProvidersCount value from the record with the most recent workcase update date. If the workcase update date is empty, then use the most recent cover update date. Then sum the providerscount values that were selected.

 

So if I put it into a table I want to see:

UserID          ProvidersCount

xxx123          23 (23 comes from 7 + 10 + 6)

xxx456          11

 

 

Thanks!

3 REPLIES 3
Anonymous
Not applicable

Hi @Anonymous ,

There must be a more elegant (daxier) solution, but this is my two cents: 

 

For me this measure show the desired result: 

 

CPC = 
var v_UserId = min(covers[UserID])
return
row("x",
SUMX(
ADDCOLUMNS(
groupby (
'Covers',
Covers[Cover],
Covers[Workcase],
Covers[UserID]
),
"yy",
var v1 = Covers[Cover]
var v2 = Covers[Workcase]
var v3 = covers[UserID]
var v4 = CALCULATE(max(Covers[WorkcaseUpdateDate]))
var v5 = CALCULATE(max(Covers[CoverUpdateDate]))
RETURN CALCULATE(
sum(Covers[ProvidersCount]),
Covers,
Covers[Cover]=v1,
Covers[Workcase] = v2,
Covers[UserID]= v3,
Filter(Covers,
(NOT ISBLANK(v4) && Covers[WorkcaseUpdateDate] = v4) ||
(ISBLANK(v4) && Covers[CoverUpdateDate] = v5)
))
),
if (Covers[UserID] = v_UserId, [yy],0))
)

 

 2020-03-03 07_21_02-Window.png

 

Hope this helps.

 

Jan 

if this is a solution for you, don't forget to mark it as such. thanks

Anonymous
Not applicable

Hi Jan,

This did help. However, I also need to do this for a different set of criteria as well. Instead of Cover, Workcase, UserID combinations, I need to do it for Cover, Workcase, Workgroup (another field - several users can be in one workgroup and if multiple users in the same workgroup have worked on the same cover/workcase combo the providerscount should only be counted at the most recent date). I created the same formula for workgroup and it seems like it would work but it takes a very long time to load in the table. In fact, it typically times out and breaks the table.

 

I was wondering if there would be a way to change this formula to just pair up unique Cover/Workcase combinations so that when I place it into a table broken down at UserID level it will compute using UserID and when I place it into a table broken down at workgroup level it will compute using Workgroup. 

 

I tried just removing everything related to UserID, but SUMX expects an expression at the end, and I'm not sure what that expression would be for it to be dynamic like that. I am also not sure if it will speed up the processing time by making this change, but I thought it would be worth a shot. 

Anonymous
Not applicable

[Provider Count] =
var __infoData =
	ADDCOLUMNS(
		SUMMARIZE(
			Data,
			Data[Cover],
			Data[Workcase],
			Data[UserId]
		),
		"RowCount",
			CALCULATE( COUNTROWS( Data ) ),
		"LatestWorkcaseUpdateDate",
			CALCULATE( MAX( Data[Workcase Update Date] ) ),
		"LatestWorkcaseUpdateDateIsBlank",
			ISBLANK(
				CALCULATE( MAX( Data[Workcase Update Date] ) )
			),
		"LatestCoverUpdateDate",
			CALCULATE( MAX( Data[Cover Update Date] ) )
	)
var __resultForRowCountEqual1 =
	CALCULATE(
		SUM( Data[ProvidersCount] ),
		filter(
			__infoData,
			[RowCount] = 1
		)
	)
var __resultForRowCountMoreThan1AndWorkcaseUpdateDate =
	CALCULATE(
		SUM( Data[ProvidersCount] ),
		TREATAS(
			SELECTCOLUMNS(
				filter(
					__infoData,
					[RowCount] > 1
					&& not( [LatestWorkcaseUpdateDateIsBlank] )
				),
				"A", Data[Cover],
				"B", Data[Workcase],
				"C", Data[UserId],
				"D", [LatestWorkcaseUpdateDate]
			),
			Data[Cover],
			Data[Workcase],
			Data[UserId],
			Data[Workcase Update Date]
		)
	)
var __resultForRowCountMoreThan1AndNoWorkcaseUpdateDate =
	CALCULATE(
		SUM( Data[ProvidersCount] ),
		TREATAS(
			SELECTCOLUMNS(
				filter(
					__infoData,
					[RowCount] > 1
					&& [LatestWorkcaseUpdateDateIsBlank]
				),
				"A", Data[Cover],
				"B", Data[Workcase],
				"C", Data[UserId],
				"D", [LatestCoverUpdateDate]
			),
			Data[Cover],
			Data[Workcase],
			Data[UserId],
			Data[Cover Update Date]
		)
	)
var __result =
	__resultForRowCountEqual1
		+ __resultForRowCountMoreThan1AndWorkcaseUpdateDate
		+ __resultForRowCountMoreThan1AndNoWorkcaseUpdateDate
RETURN
	__result

 

Will the above be better in terms of speed?

 

Best

D

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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