Forum Discussion

TheChaks's avatar
TheChaks
Frequent Visitor
7 years ago
Solved

RUNNING SUMS USING A RANKING CALCULATED MEASURE

Hello Guyz,   I made a ranking using Rankx based on total sales per client :   Rang CA/CLT = rankx(ALL(V_CUBE[Client]);CALCULATE(sum(V_CUBE[CA HT]));;DESC)   The calculated measure works well ...
  • TomMartens's avatar
    TomMartens
    7 years ago

    Hey TheChaks,

     

    first I created a simple measure for the column "CA HT", the final measure will reference this base measure. Besides the somewhat shorter writing, it also ensures the implicit Context Transition (from Filter Context to Row Context), this is the base measure:

    ms CA HT = SUM(V_Cube[CA HT])

     

    The final measure looks like this:

    ms CA HT **bleep** = 
    var rankofcurrentrow = [Rang CA/CLT]
    return
    IF(
        HASONEVALUE('V_Cube'[Client])
        ,// one value client
        GROUPBY(
            FILTER(
                ADDCOLUMNS(
                    ALL(V_Cube[Client])    
                ,"theRank", [Rang CA/CLT]
                ,"theAmount", [ms CA HT]
                )
                ,[theRank] <= rankofcurrentrow
            )
            ,"value", SUMX(CURRENTGROUP(), [theAmount])
        )
        ,// total
        GROUPBY(
     //       FILTER(
                ADDCOLUMNS(
                    ALL(V_Cube[Client])    
                ,"theRank", [Rang CA/CLT]
                ,"theAmount", [ms CA HT]
                )
     //           ,[theRank] <= rankofcurrentrow
     //       )
            ,"value", SUMX(CURRENTGROUP(), [theAmount])
        )
    )

    It may look like more complex than it actually is, this is just because I repeat almost the same part for the Total Row, this can be much more simplified (at least I'm sure) depending on your requirements and your data model. Here I omitted the filtering, this allows showing the correct value for the Total row.

     

    Nevertheless, using the final measure in a table will create this:

     

    Hopefully, this is what you are looking for!

     

    Regards,

    Tom