Forum Discussion

tjalleph's avatar
tjalleph
Frequent Visitor
6 years ago
Solved

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

5 Replies

  • sturlaws's avatar
    sturlaws
    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.

    • tjalleph's avatar
      tjalleph
      Frequent Visitor

      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

    • tjalleph's avatar
      tjalleph
      Frequent Visitor
      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
  • v-eachen-msft's avatar
    v-eachen-msft
    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.