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
DemoFour
Responsive Resident
Responsive Resident

Distinct Count of Category based on client ID

Hi, 

 

I have a questionnaire that asks multpul questions. The questions are related to set categories. Clients do not have to answer all questions, so I filter out questions not answered. 

What I am trying to achive is to get a distinct count of each catagory, for each client who answered the questionnaire.
Therefore if Category 1 has 3 questions and the client answers 2 of the 3 questions I only want to count this Category Once.

Data example 

 

Client NumberQuestion IDQuestion TextQuestion CategoryDate
11 Healthy Living01/01/20
12 Healthy Living01/01/20
13 Exercise01/01/20
14 Food01/01/20
15 Food01/01/20
21 Healthy Living02/01/20
23 Exercise02/01/20
32 Healthy Living05/01/20
34 Food05/01/20
41 Healthy Living10/01/20
44 Food10/01/20
45 Food10/01/20



The desired out put would be 

 

Health Living = 4

Exersize = 2

Food = 3

 

I am using a measure so that I can put this into a Table visual that will filter on date

 

I am nearly there with the following code, but it counts each question and not the category! 

 

 

 

Category Count = 
CALCULATE(
    DISTINCTCOUNTNOBLANK( 'Support Need'[Client Number] ),
    FILTER(
        'Support Need' ,
        CONTAINSSTRING( 'Support Need'[Question Category] , SELECTEDVALUE( 'Support Need References (STATIC)'[Question Category] )
        )
    )
)

 

 

 

 

 

Any pointers to help me get over the last hurdle would be much apprectiated. 

 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @DemoFour 

1. Place Question category in a table visual

2. Place this measure in the visual

 

Measure = 
COUNTROWS( DISTINCT( Table1[Client Number] ) )

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

 

View solution in original post

7 REPLIES 7
PaulDBrown
Community Champion
Community Champion

@DemoFour

I think here's what you're looking for:

result.JPG

achieved using this measure:

Distinctcount by categories =
COUNTROWS (
    CALCULATETABLE (
        VALUES ( 'DataTable'[Client Number] ),
        ALLEXCEPT ( 'DataTable', 'DataTable'[Question Category] )
    )
)




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






@PaulDBrown 

 

Thanks for your suggestion, this also works and is a variation on my other attempt below. 

Just goes to show that DAX can be as complicated or as simple as we make it! 

Thank you for your help @PaulDBrown 

 

 

S3 Count y = 
CALCULATE(
    DISTINCTCOUNT( 'Support Needs'[Client Number] ),
    ALLEXCEPT(
        'Support Needs' ,
        'Support Needs'[Question Category],
        'Support Needs'[Client Number]
    )
)

 

AlB
Super User
Super User

Hi @DemoFour 

1. Place Question category in a table visual

2. Place this measure in the visual

 

Measure = 
COUNTROWS( DISTINCT( Table1[Client Number] ) )

 

Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

Cheers 

 

SU18_powerbi_badge

 

DemoFour
Responsive Resident
Responsive Resident

@AlB 

I'm wrong, you are correct! I have a large data set and just taken it back to excel to check and the numbers are correct for each category. . . 

I can't believe it, after all the attempt's and complex DAX it was so simple. I did not believe it as my original code gave the same results, but they seemed so high, i doubted them! 

DemoFour
Responsive Resident
Responsive Resident

Hi @AlB 

 

That does not give me a distinct count of the category, it gives me the count of all the questions answered by clients.

What I am after, is a count of the categories that the questions are in. 


I have used this in my actual report and it it is not giving the correct results

PaulDBrown
Community Champion
Community Champion

@DemoFour 

Can you post the data in a table by columns? (It's easier to copy and paste)





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






@PaulDBrown 

 

Sorry, I have corrected now! 

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.