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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
BUserTG
Frequent Visitor

Group by and count by the maximum attribute from the group

I am looking to write a DAX calculation that allows me to count by the number of groupings based on the max of one of the attributes of the group. Example data below where there's a group with items inside of it and each item has its own value. I want to count the unique groups that fall into each attribute, based on what the max attribute is of the group. All three below are columns.

GroupItemAttributeValue
Group1A3
Group1B3
Group1C2
Group2X1
Group2Y2
Group2Z3
Group3Q1
Group3A1

 

Then the result I want to produce is a distinct count of groups for each attribute. For example, in the above there are two groups which would fall into Attribute "3" and one group would fall into Attribute "1". Group 1s max is 3, Group 2s max is 3, and Group 3s max is 1. So the visual I need to create would be:

AttributeGroupCount
11
20
32

 

Effectively, I need to be able to bucket each of the groups by the MAX(Attribute) and then count them plus do % of total visualizations so I can see the distribution of the groups by their max item. This also needs to be dynamic as all the columns can be filtered and the count would need to be re-calculated.

 

Thanks.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi, @BUserTG 

Please check the below picture and the sample pbix file's link down below.

 

Picture2.png

Group Count =
VAR maxattribute =
MAX ( 'Table'[AttributeValue] )
VAR grouptable =
GROUPBY (
ALL ( 'Table' ),
'Table'[Group],
"@maxattribute", MAXX ( CURRENTGROUP (), 'Table'[AttributeValue] )
)
VAR filtergrouptable =
FILTER ( grouptable, [@maxattribute] = maxattribute )
RETURN
COALESCE ( COUNTROWS ( filtergrouptable ), 0 )
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi, @BUserTG 

Please check the below picture and the sample pbix file's link down below.

 

Picture2.png

Group Count =
VAR maxattribute =
MAX ( 'Table'[AttributeValue] )
VAR grouptable =
GROUPBY (
ALL ( 'Table' ),
'Table'[Group],
"@maxattribute", MAXX ( CURRENTGROUP (), 'Table'[AttributeValue] )
)
VAR filtergrouptable =
FILTER ( grouptable, [@maxattribute] = maxattribute )
RETURN
COALESCE ( COUNTROWS ( filtergrouptable ), 0 )
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thanks @Jihwan_Kim . This does work except in the case of filtering. For example, if I applied a filter on the Item column, I would want the results to dynamically change. So if I filtered [Item] = 2 then the resultant table should be:

AttributeValue  GroupCount
1                        0
2                        1
3                        0

I've tried using an ALLEXCEPT() in the GROUPBY() but that did not work. Any idea on how to make the calculation resilient to filtering on the source table?

Thanks.

ALLSELECTED() should do the trick, seems to be working. Thanks.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

August Carousel

Fabric Community Update - August 2024

Find out what's new and trending in the Fabric Community.