Forum Discussion

abhi201002's avatar
abhi201002
Regular Visitor
5 months ago
Solved

Unable to filter Top-N using parameters.

Let me give you some background of data and work i have done till now.   In data in have one column name Customer ID and a measure Total Sum. I want to get top-N Customers based on their Total sums...
  • v-echaithra's avatar
    5 months ago

    Hi abhi201002 ,

    Thanks for the clarification, what you’re seeing is due to how evaluation context is affecting your measures. The main issue is the use of ALLSELECTED() in your RANKX, which makes the ranking dependent on the current filter context rather than the full customer set. As a result, the rank gets recalculated inconsistently (e.g., collapsing to only 1 customer at 100%, all 0s below that, and partial results above 100%).
    Additionally, using a measure (Dynamic Filter) that depends on rank as a visual-level filter creates a feedback loop where the filter and ranking influence each other. Your expectation (Top 5 for 5%, Top 50 for 50%) is absolutely valid, but it requires ranking over a stable, unfiltered dataset. You can partially improve this by replacing ALLSELECTED with ALL, but for consistent and scalable results, the TOPN-based approach is recommended as it avoids these context issues entirely.

    The TOPN based approach works by calculating the TopN customers in a separate virtual table, instead of relying on ranking inside the visual. First, you convert your percentage slicer into a number of customers:

    Top N Customers =
    VAR Pct = SELECTEDVALUE('%Top Customer'[%Top Customer], 1)
    VAR TotalCust = DISTINCTCOUNT(Orders[Customer ID])
    RETURN
    MAX(1, INT(Pct * TotalCust))

    Then, you build a virtual table of the top customers using TOPN:

    TopN Filter =
    VAR N = [Top N Customers]
    VAR TopTable =
    TOPN(
    N,
    ADDCOLUMNS(
    VALUES(Orders[Customer ID]),
    "@Sales", [Total Sales]
    ),[@Sales], DESC)
    RETURN
    IF(
    ISINSCOPE(Orders[Customer ID]) &&
    Orders[Customer ID] IN TopTable,
    1,0)

    Finally, apply TopN Filter = 1 as a visual-level filter.



    If further assistance is still required. 

    Please provide the sample Pbix file. How to provide sample data in the Power BI Forum

    You can refer the following link to upload the file to the community.
    How to upload PBI in Community

    We are available to support you and are committed to helping you reach a resolution.

    Best Regards,
    Chaithra E.