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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
SoupyNoodles14
Frequent Visitor

Create Dynamic Measure of Distinct Counts

Hello! I know this theme is coming up a lot in this forum and I apologize I have been struggling for weeks on this. My data is structured as follows.

 

I built the following table to show a pairing of each customer visit to a store AND their prior service history. For instance, at visit 1, person A has two prior services as of 1/1/2021, but at their second visit in 1/1/2023, they have four prior services done.

(Note, dates are in MM/DD/YY format.) 

 

PersonVisitIDVisitDatePriorServiceIDPriorServiceTypePriorServiceDate
AV11/1/21S1High1/1/03
AV11/1/21S2Medium1/2/03
AV21/1/23S1High1/1/03
AV21/1/23S2Medium1/2/03
AV21/1/23S3Low1/3/22
AV21/1/23S4Low7/3/22
BV31/1/23NULLNULLNULL
CV41/3/23S5Low1/18/22
CV51/3/24S5Medium1/18/22
CV51/3/24S6High7/17/23

 

I have loaded this data in power BI, where this table above (say, Priors) is linked to a separate visitID-level table (Visits). There are slicers on visit date, prior service type, and prior service date. 

 

I want to use Measures in Power BI to obtain the following output using measures (NOT calculated columns). 

(V3 is with 0 prior services, so the first entry in the output below is 0. V4 is associated with 1 prior service. V1 and V5 are both associated with 2 prior services, so that makes the third entry. The final entry is for V2.)

 

DESIRED OUTPUT USING MEASURES:

# Prior Services# Unique VisitIDs
01
11
22
41

 

I want to be able to slice this so that if I were to select only prior services performed in 2003 on a slicer, I will obtain:

 

# Prior Services# Unique VisitIDs
22

 

Similarly, I want to be able to slice by the prior service type and the most severe prior service type associated with each visit. 

 

So far, I can achieve the desired output WITHOUT slicers easily by building a calculated table uses the SUMMARIZE function. Naturally calculated columns cannot be re-calculated after I select a slicer which is of crucial importance. I have used many combinations of ALLEXCEPT, ALL, COUNTX, COUNTDISTINCTNOBLANK (...) + 0, and FILTER. I know that one of them has to be successful, but at this point I'd appreciate any assistance you can offer. Thank you in advance. 

2 ACCEPTED SOLUTIONS
Rupak_bi
Responsive Resident
Responsive Resident

Hi, Hope this will solve your purpose

Rupak_bi_1-1724659362894.pngRupak_bi_2-1724659379238.png

Approach I have used: 

1. Created an axis table with nubler 1,2,3,4 using manual entry.
2. created Measure to switch values to the axis as below

Measure =

Var Tab = SUMMARIZE('Table','Table'[VisitID],"Count",max(0,countx(filter('Table','Table'[PriorServiceID]<>"NULL"),'Table'[PriorServiceID])))

return
countx(FILTER(Tab,[Count]=max('Table (2)'[axis])),[Count])
This is working with slicing as well. If this works, accept as solution else let me know the issue.
Thanks..

 

View solution in original post

Hi @SoupyNoodles14 , change the Dax as below and check.

VAR Tab = SUMMARIZE ('Table', 'Table'[VisitID], "MinSeverity", MIN('Table'[SeverityID] ) )

View solution in original post

8 REPLIES 8
SoupyNoodles14
Frequent Visitor

Hi @Rupak_bi  and all. Sorry to keep bugging you, I have a final follow-up question on this. I have successfully created measures and charts to indicate counts of visits for both greatest prior service severity AND for # prior services. I have lookup tables for both the prior service severity and for the # of prior services which now serve as slicers in the report (this is essentially Table (2) in Rupak's post). These slicers are being sliced by my other slicers but they alone do not slice anything. Is there any way to use them to slice other data in the report? For instance, if we can use the "greatest severity of prior service" to slice on the prior service date. Thank you in advance. 

Rupak_bi
Responsive Resident
Responsive Resident

Hi, Hope this will solve your purpose

Rupak_bi_1-1724659362894.pngRupak_bi_2-1724659379238.png

Approach I have used: 

1. Created an axis table with nubler 1,2,3,4 using manual entry.
2. created Measure to switch values to the axis as below

Measure =

Var Tab = SUMMARIZE('Table','Table'[VisitID],"Count",max(0,countx(filter('Table','Table'[PriorServiceID]<>"NULL"),'Table'[PriorServiceID])))

return
countx(FILTER(Tab,[Count]=max('Table (2)'[axis])),[Count])
This is working with slicing as well. If this works, accept as solution else let me know the issue.
Thanks..

 

Thank you Rupak! This works PERFECTLY and is a huge relief toward helping organize a very large dataset. I have one follow-up question. 

The last part of my post above asks about slicing the most severe type of prior service. That would mean the measure I'm thinking of would identify # of visits by "at least one high-level prior service", "at least one medium-level prior service", "at least one low-level prior service", and "no prior services", where the counts would change based on other selections in the report. Just like you created the axis table above, I'm thinking about listing these string values out and then calculating the relevant counts in a measure. Is there any way to accomplish this? 

You can do it same way as previous one. Let me know if you stuck.

Hi Rupak - I am achieving partially correct results. It is correct for "no prior services" but it is not correctly identifying the severity for the others. 

Here is what my approach is. 

1. I created a separate lookup table with each severity and I loaded it into my dataset. 

SeverityLiteralSeverityID
High1
Medium2
Low3
None4

 

This severityID is also loaded into my main table, which you had labelled 'Table'. 

2. I wrote the following DAX measure based on yours:

Measure for most severe prior service =

VAR Tab = SUMMARIZE ('Table', 'Table'[VisitID], "MinSeverity", MINX ( 'Table', 'Table'[SeverityID] ) )

 

RETURN COUNTX ( FILTER ( Tab , [MinSeverity] = MIN ( 'SeverityLU'[SeverityID] ) ) , [MinSeverity] )

 

The idea is that instead of using some DAX version of complex EXISTS statements in SQL, I just select the min severity ID of prior services for each VisitID. If it's 1, it's 1, if it's 2, it's 2. Then I want to perform a distinct count based on that. The result is faulty for "at least one high", "at least one medium", and "at least one low", but it is correct for "no prior services". Please advise. Thank you again!

Hi @SoupyNoodles14 , change the Dax as below and check.

VAR Tab = SUMMARIZE ('Table', 'Table'[VisitID], "MinSeverity", MIN('Table'[SeverityID] ) )

Unfortunately, @Rupak_bi  that is giving the same incorrect results. What's happening is that PowerBI is somehow assigning more VisitIDs to 2 ("at least one medium") and 3  ("at least one low") than 1 or 2. When it comes to "none" there is nothing to choose from so it's correctly telling methe right number of visitIDs for that. Can you please advise. 

Nevermind - that message above is an oversight on my part. I had some hidden filters messing with my dashboard. This works perfectly, thank you again and have a great day!

Helpful resources

Announcements
September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.