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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
tjalleph
Frequent Visitor

Dynamic and categorized distinct counting

Hi all,

 

I'm fairly new to Power BI and trying to do something that could be called dynamic and categorized distinct couting. 

 

A simplified sample of my data would like this:

IDCategoryTypeCoded_answer
#1AX0
#1BX0
#1CX1
#1DX2
#1EX0
#2AY0
#2CY0
#2DY0
#3AX0
#3BX0
#3CX(null)

#3

DX2
#4AX1
#4BX0
#4CX0

 

Then I'd want to a distinct couting of ID's and categorize them on the basis of the maximum of Coded_answer in that group. 

It would need to do so for many different slices of the data. Resulting tables for this dataset would have to look like:

Resulting tables  
 max = 0 max = 1max = 2
Total112
    
Categoriesmax = 0 max = 1max = 2
Cat A310
Cat B300
Cat C210
Cat D102
Cat E100
    
Typemax = 0 max = 1max = 2
X102
Y100

 

I've been trying to create two measures (see below) that do a distinct count based on the condition where the maximum is 2. That does work, because it's the highest value in the coded_answer. However, if I create a measure that does the same a distinct count where the maximum is 1, it would also count the ID #1, while there's also 2 in this ID. The reason to me seems that I'm only applying a filter. 

 

Could someone point me in the right direction?

 

Kind regards,

 

Tjalle

 

*measure: 

# max:2 = calculate(DISTINCTCOUNT(My_data[ID]), filter(My_data, [max_coded_answer]=2))
max_coded_answer = maxx(values(My_data[ID]), max(My_data[Coded_answer]))
1 ACCEPTED SOLUTION
sturlaws
Resident Rockstar
Resident Rockstar

Hi @tjalleph ,

 

you can write your measures like this:

m0 =
COUNTROWS (
    FILTER (
        ADDCOLUMNS (
            VALUES ( 'Table'[ID] );
            "maxCodedAnswer"; CALCULATE ( MAX ( 'Table'[Coded_answer] ) )
        );
        [maxCodedAnswer] = 0
    )
)

 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

View solution in original post

5 REPLIES 5
v-eachen-msft
Community Support
Community Support

Hi @tjalleph ,

 

I found that there is a "null" value in your table. You could add CALCULATE(MAX('Table'[Coded_answer]),FILTER('Table','Table'[Coded_answer]>=0)) to your filter.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
sturlaws
Resident Rockstar
Resident Rockstar

Hi @tjalleph ,

 

you can write your measures like this:

m0 =
COUNTROWS (
    FILTER (
        ADDCOLUMNS (
            VALUES ( 'Table'[ID] );
            "maxCodedAnswer"; CALCULATE ( MAX ( 'Table'[Coded_answer] ) )
        );
        [maxCodedAnswer] = 0
    )
)

 

Cheers,
Sturla

If this post helps, then please consider Accepting it as the solution. Kudos are nice too.

Works like magic! Thanks again.

To make sure i understand the concept: am I right when saying that we’re creating a temporary table with the distinct IDs and the maxCodedAnswer, that is subsequently filtered on the given maximum (in the example: 0)?

-Tjalle

Correct

Thanks! First results seems to be like I would expect them to be. I'm gonna try it on an additional dataset. Will come back early next week with the results.

 

Kind regards,

 

Tjalle

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

Check out the February 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.