Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

Issue in slicer on visual

There are two slicers. 1. Choose Top or Bottom 2.Choose value like 1,2,3 etc.

 
dynamic time period with top rank =
Var N = SELECTEDVALUE('Top N Value'[Top N Value])
VAR RankType = SELECTEDVALUE('TopBottom Selector'[RankType])
VAR Productrank1= RANKX(allselected(Products[ProductName]),[Dyanamic Time Period Measure],
,DESC)
VAR Productrank2= RANKX(allselected(Products[ProductName]),[Dyanamic Time Period Measure],
,ASC)
RETURN
SWITCH(RankType,"Top" ,
                       if(Productrank1 <=N,  [Dyanamic Time Period Measure]),
                       if(Productrank2 <=N,  [Dyanamic Time Period Measure]))

 

 

Can you tell where it is getting wrong?

 

  • Hi Anonymous ,
    Here is the updated code :

    Dax
    
    Dynamic Time Period with Top Rank =
    VAR N = SELECTEDVALUE('Top N Value'[Top N Value])
    VAR RankType = SELECTEDVALUE('TopBottom Selector'[RankType])
    VAR IsTop = RankType = "Top"
    VAR IsBottom = RankType = "Bottom"
    VAR BaseMeasure = [Dynamic Time Period Measure]
    
    // Create a table of products with non-blank measure values
    VAR ProductTable =
        FILTER(
            ALLSELECTED(Products[ProductName]),
            NOT ISBLANK(CALCULATE([Dynamic Time Period Measure]))
        )
    
    // Rank products based on the selected measure
    VAR ProductRankTop =
        RANKX(
            ProductTable,
            CALCULATE([Dynamic Time Period Measure]),
            ,
            DESC
        )
    
    VAR ProductRankBottom =
        RANKX(
            ProductTable,
            CALCULATE([Dynamic Time Period Measure]),
            ,
            ASC
        )
    
    RETURN
        SWITCH(
            TRUE(),
            IsTop && ProductRankTop <= N, BaseMeasure,
            IsBottom && ProductRankBottom <= N, BaseMeasure,
            BLANK()
        )

     
    Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
    💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
    🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore More]

5 Replies

  • Hi Anonymous ,
    Looking at your question, it seems that you are facing following issues :

    When "Top" is selected:

    • Your matrix visual shows the Top 5 products based on the dynamic time period measure — working as expected.

    When "Bottom" is selected:

    • The matrix visual returns blank, including the total row, which indicates no data is being returned by the measure.

    🔍 Likely Cause

    Your current DAX logic only returns a value when ranking condition is met (Top or Bottom), but it does not handle the case when the slicer is not set properly or RankType is blank, or there's a logic flaw in the SWITCH/IF conditions.

    Also, you’re using SWITCH(RankType, ...), which won't handle "Bottom" properly unless the RankType slicer value is exactly "Bottom" — and if it's blank or not selected, it'll return nothing.

    We can fix this by following steps :

    Use SWITCH(TRUE( )) Instead

    Here’s the updated DAX measure that should fix your issue:

    DAX
    
    Dynamic Time Period with Top Rank =
    VAR N = SELECTEDVALUE('Top N Value'[Top N Value])
    VAR RankType = SELECTEDVALUE('TopBottom Selector'[RankType])
    
    VAR ProductRankTop =
        RANKX(
            ALLSELECTED(Products[ProductName]),
            [Dynamic Time Period Measure],
            ,
            DESC
        )
    
    VAR ProductRankBottom =
        RANKX(
            ALLSELECTED(Products[ProductName]),
            [Dynamic Time Period Measure],
            ,
            ASC
        )
    
    RETURN
        SWITCH(
            TRUE(),
            RankType = "Top" && ProductRankTop <= N, [Dynamic Time Period Measure],
            RankType = "Bottom" && ProductRankBottom <= N, [Dynamic Time Period Measure],
            BLANK()
        )

     

    What to Check if It’s Still Blank:

    1. Ensure your slicer values are exactly "Top" or "Bottom" (no spaces or typos).

    2. Confirm Dynamic Time Period Measure returns values across products (not blank for some).

    3. If needed, add a card visual showing SELECTEDVALUE('TopBottom Selector'[RankType]) to confirm what’s being captured.


    Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
    💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
    🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore More]

    • Anonymous's avatar
      Anonymous
      Not applicable

      Still not wokring and this is answer from CHATGPT and i have already tried this.

      • GrowthNatives's avatar
        GrowthNatives
        Icon for Super User rankSuper User

        Hi Anonymous ,
        Here is the updated code :

        Dax
        
        Dynamic Time Period with Top Rank =
        VAR N = SELECTEDVALUE('Top N Value'[Top N Value])
        VAR RankType = SELECTEDVALUE('TopBottom Selector'[RankType])
        VAR IsTop = RankType = "Top"
        VAR IsBottom = RankType = "Bottom"
        VAR BaseMeasure = [Dynamic Time Period Measure]
        
        // Create a table of products with non-blank measure values
        VAR ProductTable =
            FILTER(
                ALLSELECTED(Products[ProductName]),
                NOT ISBLANK(CALCULATE([Dynamic Time Period Measure]))
            )
        
        // Rank products based on the selected measure
        VAR ProductRankTop =
            RANKX(
                ProductTable,
                CALCULATE([Dynamic Time Period Measure]),
                ,
                DESC
            )
        
        VAR ProductRankBottom =
            RANKX(
                ProductTable,
                CALCULATE([Dynamic Time Period Measure]),
                ,
                ASC
            )
        
        RETURN
            SWITCH(
                TRUE(),
                IsTop && ProductRankTop <= N, BaseMeasure,
                IsBottom && ProductRankBottom <= N, BaseMeasure,
                BLANK()
            )

         
        Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
        💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
        🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore More]

  • Anonymous's avatar
    Anonymous
    Not applicable

    What if we need to display Top N/Bottom N products and regions both.

    It should show only top 5 or bottom 5 products and regions. Only 5 row table.

    Any change needs to be done in this measure also?
     
    Selected Measure top N rank =
    SWITCH(SELECTEDVALUE('Measure Selector'[Measure Name]),"Sales",[Total Sales],"Quantity",[Total Quantity],"GP",[Total GP])

     

    Like this? 

     

  • Hi 
    You would need to change your measure as per the steps below 
    Steps 
    1. Create a Combined Entity Table

    You need a table that contains both products and regions as rows. This can be done in Power Query or by using DAX to create a calculated table:

    CombinedEntityTable =
    UNION(
    SELECTCOLUMNS(Products, "EntityType", "Product", "EntityName", Products[ProductName]),
    SELECTCOLUMNS(Regions, "EntityType", "Region", "EntityName", Regions[RegionName])
    )

    2. Update the Measure for Dynamic Ranking

    DAX

    Dynamic Top Bottom N Entity =
    VAR N = SELECTEDVALUE('Top N Value'[Top N Value])
    VAR RankType = SELECTEDVALUE('TopBottom Selector'[RankType])
    VAR IsTop = RankType = "Top"
    VAR IsBottom = RankType = "Bottom"

    // Replace this SWITCH with your actual metric selection logic if needed
    VAR SelectedMeasure =
    [Dynamic Time Period Measure]

    // Build a table of all entities with non-blank values for the selected measure
    VAR EntityTable =
    FILTER(
    ALLSELECTED('CombinedEntityTable'[EntityName]),
    NOT ISBLANK(CALCULATE(SelectedMeasure))
    )

    // Calculate the Top and Bottom ranks
    VAR EntityRankTop =
    RANKX(
    EntityTable,
    CALCULATE(SelectedMeasure),
    ,
    DESC
    )

    VAR EntityRankBottom =
    RANKX(
    EntityTable,
    CALCULATE(SelectedMeasure),
    ,
    ASC
    )

    RETURN
    SWITCH(
    TRUE(),
    IsTop && EntityRankTop <= N, SelectedMeasure,
    IsBottom && EntityRankBottom <= N, SelectedMeasure,
    BLANK()
    )

    Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
    💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
    🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore More]