Forum Discussion

BogdanP's avatar
BogdanP
Frequent Visitor
10 years ago
Solved

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[Dimen...
  • KGrice's avatar
    10 years ago

    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:

     

     

    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 :)

     

    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.