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
Han_Solo
Helper I
Helper I

Sum based on count of categories.

Hello,

I have a problem with sum by counting categories. 
Capture1.PNG

I have a few categories and further in hierarchy places for those. There are categories with multiple places (like Canada) and there are categories with one place. I want to calculate total SUM of value1 for each category but when category has only 1 place, sum them up - then its sum based on the count of category.  

 

Capture2.PNG

 so where there is AFRICA, ASIA, AUSTRALIA, SOUTH AMERICA I want to have 65,79 (10,92 + 21,69 + 28,98 + 4,20)

I tried different measures:
(i know that with one below there is no IF condition)

 

AVG_Category = 
VAR _Category = MAX(data[Category])
RETURN 
CALCULATE(SUM(data[Value1]), 
    FILTER(ALL(data[Category], data[Place]), 
        CALCULATE(COUNT(data[Category]), 
            ALL(data[Category], data[Place]), 
                data[Category] = _Category) = 1 ))

 

or this one

 

AVG_Category1 = 
VAR _cat = MAX(data[Category])
VAR _one = CALCULATE(COUNT(data[Category]), ALL(data[Category], data[Place]), data[Category] = _cat)
RETURN
IF( _one = 1, 
    CALCULATE(SUM(data[Value1]), 
        FILTER(ALL(data[Category], data[Place]), 
            CALCULATE(COUNT(data[Category]), ALL(data[Category], data[Place]), data[Category] = _cat) = 1 )),
    CALCULATE(SUM(data[Value1]), ALL(data), data[Category] = _cat))

 

 but without big success 

 

Capture3.PNG

but instead of 884,92 it should be 65,79. 

 

Do you have any idea for solution? Where I make a mistake?

 

You can download file  here

note worth mentioning:

this is sample dataset, the main file has live connection dataset and a lot of date so I tried to come up anything without sumx, calculated columns, calculated tables because need it to be fast (unless in SSAS)

Also it would be used on the stacked bar chart so on axis would be only places without category drill through (tricky way with dynamic axis). 

 

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@Han_Solo ,

Try like sumx(summarize(data,data[Category],"_max",max(data[Value1])),[_max])

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.

Top Solution Authors