cancel
Showing results 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

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.

 Group Item AttributeValue Group1 A 3 Group1 B 3 Group1 C 2 Group2 X 1 Group2 Y 2 Group2 Z 3 Group3 Q 1 Group3 A 1

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:

 Attribute GroupCount 1 1 2 0 3 2

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
Super User

Hi, @BUserTG

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

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.

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.

3 REPLIES 3
Super User

Hi, @BUserTG

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

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.

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.

Frequent Visitor

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.

Frequent Visitor

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

Announcements

#### 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.

#### Power BI Monthly Update - August 2024

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

#### Fabric Community Update - August 2024

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

Top Solution Authors
Top Kudoed Authors