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

Sum by categories but group all sums of less than x into new category

Hi everyone,

 

Would really appreciate help with this.

 

(using powerpivot and pivottable not PowerBI)

 

I have multiple category columns, and 2 key data columns, cost and count. I would like to sum the data and the count by category (easy enough), but if the count of either country or sub category is less than 10 (or x), it would create a new category 'other'. 

 

This would reduce the 'tail of a large data set by grouping all small values together (but based on total count, not the total sum), but the numbers would still add up to the correct number. 

 

The data will get summarised by country, and sub category therefore ideally the measure would work if used in either scenario.

 

Example of base data and desired output below (had tried to attach a file but unable)

 

Base data:   
    
CountrySub CategoryCostCount
Country 1Sub 1                         10,000               5
Country 1Sub 2                      200,000124
Country 2Sub 1                           4,00080
Country 2Sub 2                         75,0001
Country 3Sub 1                         20,0003
Country 4Sub 2                      600,00015
Country 5Sub 2                           7,5001
                        916,500229
    
Output report:   
 CostCount 
Country 1              210,000              129 
Country 2                79,00081 
Country 4              600,00015 
Other                27,5004 
Total             916,500229 

 

Many thanks for any advice in advance.

 

(cant seem to attach a file)

 

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi, @AlexB88 

 

Please check the below link, that is to the sample pbix file that I have created by using your sample data.

the result is shown as per your request.

 

One of the four measures that I have written below, which is for Cost total, including Other.

You can check other measures in the pbix file.

Cost Total =
VAR newtable =
ADDCOLUMNS (
SUMMARIZE ( ALL ( Data ), Data[Country] ),
"@cost", [Cost],
"@count", [Count]
)
VAR countrycountmorethanten =
FILTER ( newtable, [@count] > 10 )
VAR result =
IF (
SELECTEDVALUE ( Data[Country] ) = "Other",
SUMX ( newtable, [@cost] ) - SUMX ( countrycountlessmorethanten, [@cost] ),
IF ( [Count] <= 10, BLANK (), [Cost] )
)
RETURN
result

 

https://www.dropbox.com/s/sbykhkltc3s6xkq/AlexB88.pbix?dl=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.

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.


Go to My LinkedIn Page


Hi Jihwan, many thanks for your response!

 

That seems to work well in PowerBI, is there any way to use the measure in PowerPivot? 

 

Thanks,

 

Alex

Hi, @AlexB88 

Thank you for your feedback.

I think Power Pivot also uses DAX. But I only tried a very simple one before.

I think it should work in the same way.

 

And, if it is in Power Pivot, that means it is in an EXCEL environment, and I think it might be easier to create the above.

 

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.

 

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.


Go to My LinkedIn Page


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