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
legendsfan
Frequent Visitor

Distinct Count if item Is This

I have a data set with Owner, Collaborator Type, and Folder.   Owners can own multiple folders and each folder has a Collaborator Type of external or managed.  I'd like to be able to get a distinct count of the number of owners 3 different ways.

  1. Owners who only have Managed folders
  2. Owners who only have External folders
  3. Owners who have both types

 

Right now I can sum the Distinct value of Owner+Managed or Owner+External but no idea how to count the sum of Owners that ONLY do one type or the other.  Any help is greatly appreciated.

1 ACCEPTED SOLUTION

@legendsfan

It looks like you've added a calculated column with that code.

The code is actually a measure definition, and won't make sense in a calculated column.

 

Here's a sample pbix file using your sample table

Capture.png

 

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

6 REPLIES 6
OwenAuger
Super User
Super User

Hi @legendsfan

 

So you want a distinct count of Owners that have exactly one Collaborator Type and that Collaborator Type = "Managed" (and the same for "External")?

 

My first thought would be to do something like this:

DistinctCount of Owners with Only Managed = 
CALCULATE (
    DISTINCTCOUNT ( Data[Owner] ),
    FILTER (
        VALUES ( Data[Owner] ),
        AND (
            CALCULATE ( HASONEVALUE ( Data[Collaborator Type] ) ),
            FIRSTNONBLANK ( Data[Collaborator Type], 0 ) = "Managed"
        )
    )
)

or equivalently

DistinctCount of Owners with Only Managed = 
COUNTROWS (
    FILTER (
        VALUES ( Data[Owner] ),
        AND (
            CALCULATE ( HASONEVALUE ( Data[Collaborator Type] ) ),
            FIRSTNONBLANK ( Data[Collaborator Type], 0 ) = "Managed"
        )
    )
)

Let me know if that is of any use.

 

Owen 🙂


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

I don't believe this code is quite right based on the sample table I did a test on.  In this case I'd expect the number to be 2 since only Daisy and Edward have folders that are exclusively Managed.  Bob has Managed folders but he also has an External folder so he should not be counted.  Thoughts?

Capture.PNG

 

 

 

 

@legendsfan

It looks like you've added a calculated column with that code.

The code is actually a measure definition, and won't make sense in a calculated column.

 

Here's a sample pbix file using your sample table

Capture.png

 

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

This is really helpful.  Can you/anybody please help me with a similar but slightly different scenario where I want to identify (for simplicity's sake I will use the same example) the number of unique Owner's who have both a "Managed" AND "External" Collaborator Type? Thanks in advance - I'm very new to PowerBI! Cheers.

This was so helpful.  Any chance you could explain the logic to me in simpleton terms?  I'm new(ish) to reporting and trying to wrap my head around this.  In this case I couldn't even sketch it up how I'd retrieve this data.

 

Bonus question.  Is there an easy way to combine two measures and just use the item(s) that appear in both results?

@legendsfan

Sure. It's probably easier to use the first measure:

 

DistinctCount of Owners with Only Managed = 
CALCULATE (
    DISTINCTCOUNT ( Data[Owner] ),
    FILTER (
        VALUES ( Data[Owner] ),
        AND (
            CALCULATE ( HASONEVALUE ( Data[Collaborator Type] ) ),
            FIRSTNONBLANK ( Data[Collaborator Type], 0 ) = "Managed"
        )
    )
)

In words, the measure is calculating a distinct count of Owner, just for those Owners that

  1. Have a single Collaborator Type
  2. And that Collaborator Type is "Managed"

The way this is achieved in DAX is:

  1. We use FILTER to iterate through the list of Owners in the current filter context, i.e. VALUES ( Data[Owner] ), and keep only those Owners that meet two conditions:
  2. The two conditions are contained in the AND function:
    • CALCULATE ( HASONEVALUE ( Data[Collaborator Type] ) ): The owner must have exactly one Collaborator Type.
      HASONEVALUE does what it sounds like: returns TRUE if the column specified has one value in the current filter context. Technical point: we need to wrap it in a CALCULATE in order to convert the row context (current row's Owner) to filter context (if we didn't do that, it would look at the entire table in the original filter context, without filtering based on the current row's Owner).
    • FIRSTNONBLANK ( Data[Collaborator Type], 0 ) = "Managed": The first Collaborator Type found for the current Owner must be "Managed".
      FIRSTNONBLANK is a handy function for getting a single "first" value present in a column in the current filter context (based on lexical/numerical ordering). Since we only care about cases where there is a single Collaborator Type, we might as well just get the first Collaborator Type we find and see if it is "Managed".
      See this page for more description of this use of FIRSTNONBLANK.
      Note: CALCULATE is not required for FIRSTNONBLANK as it has built in context transition.
    • If the above two conditions are met, the current Owner is kept, otherwise it is rejected.
  3. Now that FILTER has reduced the list of Owners down to just those meeting the two conditions, CALCULATE overwrites the filter context on the Owner column to this filtered list of Owners.
  4. The first argument of CALCULATE, i.e. DISTINCTCOUNT ( Data[Owner] ), is then evaluated in this modified context. In fact it is just the number of Owners in the filtered list.

On your bonus question:

Where you have two 'conditions' within existing measures, and you want to use the intersection of them as a filter argument in another measure, you could rewrite both conditions as separate arguments within CALCULATE in the new measure. Multiple filter arguments within CALCULATE are always intersected.
(Note: You could also combine the conditions using other functions, such as within a single FILTER or using the INTERSECT function, if the conditions relate to the same column or set of columns.)

 

Unfortunately there isn't a simple way (without rewriting) of simply taking two existing measures and automatically combining filter contexts that occur within the evaluation of the original measures.

 

As a dummy example, let's say we want to filter Owners as in the above measure, but include them only if they are in the top 3 in terms of row count in the table. For those owners, we then want to calculate the total row count in the table.

 

The measure could be:

 

 

Row Count of Owners with Only Managed who are also in Top 3 by Row Count =
CALCULATE (
    COUNTROWS ( Data ),

// First condition (list of Owners) FILTER ( VALUES ( Data[Owner] ), AND ( CALCULATE ( HASONEVALUE ( Data[Collaborator Type] ) ), FIRSTNONBLANK ( Data[Collaborator Type], 0 ) = "Managed" ) ),

// Second condition (list of Owners) TOPN ( 3, VALUES ( Data[Owner] ), CALCULATE ( COUNTROWS ( Data ) ) ) )

 

Cheers,

Owen 🙂

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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.