Forum Discussion

SoupyNoodles14's avatar
SoupyNoodles14
Frequent Visitor
2 years ago
Solved

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. 

  • Hi, Hope this will solve your purpose

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

     

  • Rupak_bi's avatar
    Rupak_bi
    2 years ago

    Hi SoupyNoodles14 , change the Dax as below and check.

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

8 Replies

  • Hi, Hope this will solve your purpose

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

     

    • SoupyNoodles14's avatar
      SoupyNoodles14
      Frequent Visitor

      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? 

      • Rupak_bi's avatar
        Rupak_bi
        Super User

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

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