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

Count distinct values in one column, grouped by another column

I'm quite new to Power BI and I think this is probably very easy, but I've been trying to work out a solution for several days an I can't work it out.

 

My data consists of a top-level category (unlimited in number) and a limited set of sub-categories, similar to this:

 

 

MakeType
FordSUV
FordHatchback
FordConvertible
FordPickup
VWSUV
VWHatchback
NissanPickup
NissanSUV
NissanHatchback
JaguarConvertible
JaguarSUV

 

I want to add a column that simply shows how many 'types' each manufacturer offers, so the output would be:

 

MakeTypeCount of Type
FordSUV4
FordHatchback4
FordConvertible4
FordPickup4
VWSUV2
VWHatchback2
NissanPickup3
NissanSUV3
NissanHatchback3
JaguarConvertible2
JaguarSUV2

 

as I want to be able to filter on (for example) manufacturers offering more than 2 types of vehicle. 

I can create a simple stacked column chart with Make onthe x-axis and Type on the y-axis (with Type as the legend) to get:

 

SteveIves_0-1672756680160.png

but if I filter this visual on 'Count of Type is greater than 2', it shows nothing because for each individual row, the count of Type is 1.

 

How can I create a column that shows the number of distinct Types by Make, or am I looking at this incorrectly?

 

Thanks

1 ACCEPTED SOLUTION
Pragati11
Super User
Super User

HI @SteveIves ,

 

You can create a calculated column in Power BI using the following DAX measure:

Count of Type by Make = // calculated column to get count of Type by Make
CALCULATE(
    COUNT('Model Data'[Type]), // counting the Type
    FILTER(
        'Model Data', // filtering the table to get the count of type groupoed by Make
        'Model Data'[Make] = EARLIER('Model Data'[Make])
    )
)

You will get the result as follows:

Pragati11_0-1672757658773.png

Hope this helps.

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

View solution in original post

3 REPLIES 3
Meaker
New Member

I've tried to apply this logic to my requirement, but am returning an error.  The difference being that I want to count the number of times an individual item reference appears within a set of data:

Part NoDate Received
CXV115/02/2024
XC33302/11/2023
XC33301/11/2023
CXV105/10/2023
ABX12306/09/2023
ABX12304/09/2023
XC33311/06/2023
CXV131/05/2023
XC33305/05/2023
ABX12301/04/2023
ABX12317/03/2023
CXV103/03/2023
ABX12302/02/2023

 

I'm receiving an error message:

DAX comparison operations do not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values.
Pragati11
Super User
Super User

HI @SteveIves ,

 

You can create a calculated column in Power BI using the following DAX measure:

Count of Type by Make = // calculated column to get count of Type by Make
CALCULATE(
    COUNT('Model Data'[Type]), // counting the Type
    FILTER(
        'Model Data', // filtering the table to get the count of type groupoed by Make
        'Model Data'[Make] = EARLIER('Model Data'[Make])
    )
)

You will get the result as follows:

Pragati11_0-1672757658773.png

Hope this helps.

 

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Perfect! Thanks Pragati 😀

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.