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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TheChaks
Frequent Visitor

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 and for each client now I have his total sales and his rank
 
Now I want to make a running sum on the total sales using the rank I computed previously.
 
I applied couple of solutions I found on the forum with NO result.
 
Can anyone help?
 
Thanks in advance
1 ACCEPTED SOLUTION

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:

image.png

 

Hopefully, this is what you are looking for!

 

Regards,

Tom

 

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

 

Please share a dataset and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hey,

 

Thanks for helping.

 

Here is what I have:

 

 Sans titre.jpg

 

The ranking you see come from the measure I showed in my previous message.

 

The expected result is as follow;

 

Sans titreé.png

Hi,

 

Write these measures

 

Revenue = SUM(Data[Total Sales])
Rank = if(HASONEVALUE(Data[Client]),RANKX(ALL(Data[Client]),[Revenue]),BLANK())
Cumulative revenue = if(HASONEVALUE(Data[Client]),SUMX(TOPN([Rank],CALCULATETABLE(VALUES(Data[Client]),ALL(Data[Client])),[Revenue]),[Revenue]),BLANK())
 
Drag Client. Revenue and Cumulative revenue to the visual and sort the Revenue field in descending order.
 
Hope this helps.
 
 

Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

FYI the data source table is V_CUBE and the sales column is CA_HT

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:

image.png

 

Hopefully, this is what you are looking for!

 

Regards,

Tom

 

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Hey Tom !

 

Your solution works perfect ! Thanks

 

As a beginner, it's indeed quite difficult for me to understand it all.... I'll do my best 🙂

 

Is there any other simple way to get the same result?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.