Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
asmirnoffnorth
Frequent Visitor

Use of TOPN inside a measure

Hi all,

 

I have created a measure that calcualtes the differences between the two dates as defined by the slicer, 'between' category. The measure is below:

measureDiff = 

var _max = maxx(ALLSELECTED('Date'),'Date'[Date])
var _min = minx(ALLSELECTED('Date'),'Date'[Date])
var _sumMax = 
    SUMX(
        FILTER(Analysis, Analysis[Date] = _max),
        Analysis[pct]
    )
var _sumMin = 
    SUMX(
        FILTER(Analysis, Analysis[Date] = _min),
        Analysis[pct]
    )    
Return
_sumMax - _sumMin


What i am struggling with, is how to update the same measure so that i can create a table of top5/bottom5 by the different categories that I choose from my dataset. The key thing for this is for it to be dynamic, so i cna change my date ranges and pick different columns as I see fit.

3 REPLIES 3
asmirnoffnorth
Frequent Visitor

hey - I will give this a try later this week - apols for the delay and appreciate your assistance in the matter. 

Anonymous
Not applicable

Hi @asmirnoffnorth ,

 

We can create a measure.

DynamicRank =
VAR SelectedCategory = SELECTEDVALUE(YourCategoryTable[CategoryColumnName])
VAR RankValue =
    RANKX (
        ALLSELECTED ( YourCategoryTable[CategoryColumnName] ),
        CALCULATE (
            [measureDiff],
            YourCategoryTable[CategoryColumnName] = SelectedCategory
        )
    )
RETURN
    IF (
        RankValue <= 5 || RankValue >= COUNTROWS ( ALLSELECTED ( YourCategoryTable[CategoryColumnName] ) ) - 4,
        [measureDiff]
    )

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. Thank you.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Hey,

 

can you just explain what your measure does? it doesn't seem to return me a table of the top/bottom 5s, but the whole table like i've already got. i've used the "dynamicRange" in the "values" category under vizualizations. is that the right way to use this measure? thanks again!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.