cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BogdanP Frequent Visitor
Frequent Visitor

Calculating SUM of TOPN rows

Hello everyone! I am trying to compute SUM of TOP N values in a table in a time period specified via slicers using following measures:

 

1. VolumeSum = CALCULATE(SUM(Volumes[Volume]); Volumes[Dimension]="mln l")

 

2. VolumeRanks2 = RANKX(ALL(Producers[Producer Eng]); CALCULATE(SUM(Volumes[Volume]); Volumes[Dimension]="mln l"); ;DESC;Dense)

 

3. Selected TopN = MIN('TopN'[TopN])

 

4. TopVolume = CALCULATE([VolumeSum]; FILTER(Volumes; [VolumeRanks2]<=[Selected TopN]))

 

When I try to add a card with a TopVolume measure it gives me the following result:

 

 

NewAttemptTopVolume.PNG

 

 Note: there is a visual level filter on chart to show only [Selected TOPN] values.

 

As you can see, it sums volumes of every producer, not the ones with rank <= 3. I've already tried different approaches (calculated columns, TOPN function etc), however, I was unable to get desired result.

 

Here is my data model:

Data_Scheme.PNG

 

 

Any help is appreciated and thank you all in advance.

 

Best Regards,

Bogdan

1 ACCEPTED SOLUTION

Accepted Solutions
KGrice Established Member
Established Member

Re: Calculating SUM of TOPN rows

Hi @BogdanP. Here's how I modified your TopVolume formula to get it to work with some made up data:

 

TopVolume = CALCULATE(
SUMX(VALUES(Producers[Producer Eng]), [VolumeSum]),
FILTER(ALL(Producers[Producer Eng]), [VolumeRanks2]<=[Selected TopN])
)

 

 

And a screenshot of it in action:

 

TopN.PNG

 

Hopefully this works for your model. If not, please include some sample data. I know enough about DAX to figure it out, but it takes some puzzling first. I'll give an explanation a shot, and a more seasoned pro can correct or add more if needed Smiley Happy

 

As it was, there was nothing in the formula to summarize the values at the Producer Eng level, which made the rank of no real effect within the formula. Essentially, you got the sum of Volumes for everyone, because every row was evaluated against itself and got a rank of 1.

 

Adding in the SUMX tells the formula that we want to first get the VolumeSum amounts for each Producer Eng, then rank them at that level and determine if the rank is less than or equal to the Selected TopN.

 

This article is where I got my start with SUMX. It's a bit dated, but I still refer to it for a simple breakdown.

 

You'll also notice I changed the FILTER context from using the Volumes table to the ALL(Producers[Producers Eng]) column. Because RANKX is working on that column, it's what we need to include all of for the RANKX to function like we want.

5 REPLIES 5
KGrice Established Member
Established Member

Re: Calculating SUM of TOPN rows

Hi @BogdanP. Here's how I modified your TopVolume formula to get it to work with some made up data:

 

TopVolume = CALCULATE(
SUMX(VALUES(Producers[Producer Eng]), [VolumeSum]),
FILTER(ALL(Producers[Producer Eng]), [VolumeRanks2]<=[Selected TopN])
)

 

 

And a screenshot of it in action:

 

TopN.PNG

 

Hopefully this works for your model. If not, please include some sample data. I know enough about DAX to figure it out, but it takes some puzzling first. I'll give an explanation a shot, and a more seasoned pro can correct or add more if needed Smiley Happy

 

As it was, there was nothing in the formula to summarize the values at the Producer Eng level, which made the rank of no real effect within the formula. Essentially, you got the sum of Volumes for everyone, because every row was evaluated against itself and got a rank of 1.

 

Adding in the SUMX tells the formula that we want to first get the VolumeSum amounts for each Producer Eng, then rank them at that level and determine if the rank is less than or equal to the Selected TopN.

 

This article is where I got my start with SUMX. It's a bit dated, but I still refer to it for a simple breakdown.

 

You'll also notice I changed the FILTER context from using the Volumes table to the ALL(Producers[Producers Eng]) column. Because RANKX is working on that column, it's what we need to include all of for the RANKX to function like we want.

BogdanP Frequent Visitor
Frequent Visitor

Re: Calculating SUM of TOPN rows

Thank you @KGrice so much for such a detailed answer! I suspected that the problem lies exactly in evaluating rows all by itself, however, i went wrong way trying to use SUMMARIZE by [Producer Eng]. I've even tried SUMX but never used VALUES.

 

I am quite new to DAX and PowerBI and I will definitely dig more into basics like SUMX, AVERAGEX etc to avoid bothering people with dumb questionsSmiley Happy 

Thank you once again!

KGrice Established Member
Established Member

Re: Calculating SUM of TOPN rows

You're welcome @BogdanP! And keep posting the "dumb" questions, since tyring to figure them out is the only way I learn. Not sure what that says about me.

Highlighted
alexSPY Regular Visitor
Regular Visitor

Re: Calculating SUM of TOPN rows

Hi @KGrice,

 

I'm also having a challenge with getting the sum of a rank. I've tried your solution, but something in my data model won't correctly compute the solution. I have several fitlers applied to my page, so I'm guessing that has something to do with it, but curious if may have any insight.

 

Here's my ranking formula:

 

RankFormula.JPGRankFormula

RankExample.JPGExample of rank working correctly

AggregateCalc.JPGAggregation calculation

As you can see, the ranking works correctly, showing the correct Qty totals based on my page filters. However, my aggregation formula result is over 25,000. Clearly, not correct. Any ideas?

Re: Calculating SUM of TOPN rows

@alexSPY Did you get the answer for your question? I am having the same issue. 

 

Helpful resources

Announcements
GregDeckler

How to Get Your Question Answered Quickly

Power BI Super User, Greg Deckler, explains

Summit North America

Power Platform Summit North America

Register by September 5 to save $200

Back to School Contest

Back to School Contest

Engage and empower students with Power BI!

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Ideas
Users Online
Currently online: 227 members 2,992 guests
Please welcome our newest community members: