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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
nish18_1990
Helper III
Helper III

Optimize the dax query for TOP N parameter

Hi ,

I want to have ranking based on TOPN parameter selection . If i select 3 in Top N RES Parameter then it should show be top 3 Responsible Legal Entity based on measure [#OnlyOpen]. The below calculation is working perfectly fine but the issue i am facing is performance . If i select any other number in the parameter TOP N RES then the table is loading for data for more than 1 min . How can i optimize this dax calculation for increasing the performance . 

 

TopN RES =
Var Ranks =
CALCULATE(
    RANKX(
    ALL('My_Main_Table'),
    CALCULATE(
    [#OnlyOpen],
        ALLEXCEPT('My_Main_Table',My_Main_Table[Resp Risk Unit Level 2 Name])),,
    DESC,
   Dense
        )
    )
RETURN
    IF (
        ISBLANK ( 'Top N RES Parameter'[Top N RES Parameter Value] ),
        ranks,
        IF ( Ranks <= 'Top N RES Parameter'[Top N RES Parameter Value] , [#OnlyOpen], BLANK () )
    )
 
 
 
Data is something like this structure  :
Responsible Legal Entity Only Open 
ABC4
CDE5
FGH7
IJK6
MNO9
PQR10
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @nish18_1990 ,

Please try below measures.

Measure 1: Top N Responsible (Boolean filter).


Top N RES Flag =
VAR N =
SELECTEDVALUE ( 'Top N RES Parameter'[Top N RES Parameter Value], 0 )
VAR TopNTable =
TOPN (
N,
ALL ( 'My_Main_Table'[Resp Risk Unit Level 2 Name] ),
[#OnlyOpen], DESC
)
RETURN
IF (
MAX ( 'My_Main_Table'[Resp Risk Unit Level 2 Name] ) IN TopNTable,
1,
0
)

 

Measure 2: Final Result


Top N RES =


IF ( [Top N RES Flag] = 1, [#OnlyOpen] )

 

If my response as resolved your issue please mark it as solution and give kudos.

 

Thanks,

Dinesh

View solution in original post

6 REPLIES 6
v-priyankata
Community Support
Community Support

Hi @nish18_1990 

Thank you for reaching out to the Microsoft Fabric Forum Community.

@parry2k @Anonymous Thanks for the inputs.

I hope the information provided by users was helpful. If you still have questions, please don't hesitate to reach out to the community.

 

Hi @nish18_1990 

Hope everything’s going smoothly on your end. I wanted to check if the issue got sorted. if you have any other issues please reach community.

 

parry2k
Super User
Super User

Yes you replace _Top variable with your dynamic selected top value, something like this

 

__Top = [Dynamic Top Measure]



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Hi @nish18_1990 ,

Please try below measures.

Measure 1: Top N Responsible (Boolean filter).


Top N RES Flag =
VAR N =
SELECTEDVALUE ( 'Top N RES Parameter'[Top N RES Parameter Value], 0 )
VAR TopNTable =
TOPN (
N,
ALL ( 'My_Main_Table'[Resp Risk Unit Level 2 Name] ),
[#OnlyOpen], DESC
)
RETURN
IF (
MAX ( 'My_Main_Table'[Resp Risk Unit Level 2 Name] ) IN TopNTable,
1,
0
)

 

Measure 2: Final Result


Top N RES =


IF ( [Top N RES Flag] = 1, [#OnlyOpen] )

 

If my response as resolved your issue please mark it as solution and give kudos.

 

Thanks,

Dinesh

parry2k
Super User
Super User

@nish18_1990 try this:

 

Top N = 
VAR __Top = 5
RETURN
CALCULATE ( 
    SUM ( TopTest[Only Open ] ),
    KEEPFILTERS ( 
        TOPN ( 
            __Top, 
            ALLSELECTED ( TopTest ),
            CALCULATE ( SUM ( TopTest[Only Open ] ) )
        )
    )
)


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Hi , 

I need dynamic parameter selection for Top N . Like if i select 3 then show me 3 res , if i select 5 then show me 5 res 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.