cancel
Showing results for
Did you mean:
Frequent Visitor

## Count Distinct by Grouping

Hi,

This is a very simple thing but I cant seem to get to the bottom of it.

I need to create a measure that gives me the count distinct by a grouping.

For example

TableA

Group   Attributes

1               A

1               B

2               C

2               C

3               D

So the measure should say,

Group   Attributes    Measure (distinct values)

1               A                       2

1               B                       2

2               C                       1

2               C                       1

3               D                       1

So not that dissimilar to what a count distinct and group by clause would do in SQL.

Thanks a bunch!

1 ACCEPTED SOLUTION
Super User

calculate(distinctcount(Table[Attributes]), filter(allselected(Table), Table[Group] = max(Table[Group] ) ) )

Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
!! Subscribe to my youtube Channel !!
4 REPLIES 4
Super User

``````Measure2 =
CALCULATE (
COUNTX ( SUMMARIZE ( t2, t2[Group], t2[Attribute] ), t2[Group] ),
ALLEXCEPT ( t2, t2[Group] )
)
Measure3 =
CALCULATE (
SUMX ( VALUES ( t2[Group] ), CALCULATE ( DISTINCTCOUNT ( t2[Attribute] ) ) ),
ALLEXCEPT ( t2, t2[Group] )
)``````

nothing wrong with @amitchandak 's solution. But for large tables I find ALLSELECTED not to be performant and I would only use that where it can't be avoided.

Proud to be a Super User!

New Animated Dashboard: Sales Calendar

Super User

@smpa01 Is there a reason for the iterators rather than writing this?

``CALCULATE ( DISTINCTCOUNT ( t2[Attributes] ), ALLEXCEPT ( t2, t2[Group] ) )``

Super User

@AlexisOlson  great..skipped my mind.

Proud to be a Super User!

New Animated Dashboard: Sales Calendar

Super User

calculate(distinctcount(Table[Attributes]), filter(allselected(Table), Table[Group] = max(Table[Group] ) ) )

Dashboard of My Blogs !! Connect on Linkedin
Want To Learn Power BI
Learn Power BI Beginners !! Advance Power BI Concepts !! Power BI For Tableau User !! Learn Power BI in Hindi !!
Proud to be a Super User!
!! Subscribe to my youtube Channel !!

Announcements

#### Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

#### Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!

#### What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

#### Check it out!

Mark your calendars and join us on Thursday, May 26 at 11a PDT for a great session with Ted Pattison!

Top Solution Authors
Top Kudoed Authors