Forum Discussion

Falongi_82's avatar
Falongi_82
Helper I
5 years ago
Solved

Dynamic Top N - Help

Hi All,   Based on the following example (https://community.powerbi.com/t5/Community-Blog/Top-N-with-Slicer-and-Steps/ba-p/952240), I try to implement it on my report but it doesn't work. I can't ...
  • v-deddai1-msft's avatar
    5 years ago

    Hi Falongi_82 ,

     

    In DIM_DUMP table, you have duplicated MATERIAL_CODE, so you can't use countrows ,you should use distinctcount.

     

    I did some little modification on your measures:

     

    Top X Steps =
    VAR TopNumber =
        MAX ( 'TopN Steps'[TopN] )
    VAR Rest =
        DISTINCTCOUNT ( DIM_DUMP[MATERIAL_CODE] ) - TopNumber
    RETURN
        CALCULATE (
            [M_TOT_LINE],
            FILTER (
                DIM_DUMP,
                DIM_DUMP[MATERIAL_CODE] = MAX ( 'TopN Union'[Key values] )
                    && CONTAINS (
                        TOPN (
                            TopNumber,
                            ADDCOLUMNS (
                                ALL ( 'TopN Union'[Key values] ),
                                "Tot Lines",
                                    CALCULATE (
                                        [M_TOT_LINE],
                                        FILTER ( DIM_DUMP, DIM_DUMP[MATERIAL_CODE] = EARLIER ( [Key values] ) )
                                    )
                            ),
                            [M_TOT_LINE], DESC
                        ),
                        'TopN Union'[Key values], MAX ( 'TopN Union'[Key values] )
                    )
            )
        )
    
    Other Steps =
    VAR TopNumber =
        MAX ( 'TopN Steps'[TopN] )
    VAR Rest =
        DISTINCTCOUNT ( DIM_DUMP[MATERIAL_CODE] ) - TopNumber
    RETURN
        IF (
            VALUES ( 'TopN Union'[Display values] ) = "Others",
            SUMX (
                TOPN (
                    Rest,
                    ADDCOLUMNS ( VALUES ( DIM_DUMP[MATERIAL_CODE] ), "Tot Lines", [M_TOT_LINE] ),
                    DIM_DUMP[M_TOT_LINE], ASC
                ),
                DIM_DUMP[M_TOT_LINE]
            )
        )
    

     

    If the issue is still existing. you'd better show us the sample pbix.

     

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

     

    Best Regards,

    Dedmon Dai