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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Power-BI4CPF
New Member

Cumulative sum on a ranked table

I'm trying to extract the top % of products by sales for each customer. the script below returns the cumulative percentage but not in order. What I want is to sort them from high to low and then run the code below.

 
CumulativePercentage =
VAR _TotalSales =
    CALCULATE(
        [Sales_kgs],
        ALL('Product Lookup')
    )
VAR _CumSales =
        CALCULATE(
            SUM(Inventory[monthly_wholesale_kg]),
            FILTER(
                ALLSELECTED(Inventory),
                Inventory[product_sku] <= MAX(Inventory[product_sku])
            )
)

RETURN
    _CumSales / _TotalSales
1 ACCEPTED SOLUTION

@Power-BI4CPF 

output

Daniel29195_0-1707550363179.png

 

 

measures : 
ranking measure : 

rnk = 
RANKX(
    ALLSELECTED(tbl1[Product]),CALCULATE(sum(tbl1[monthly sales])),,DESC)

    

 

 

measure cumul : 

cumulative sum = 
var current_ranking  = [rnk]


var s = 
CALCULATE(
    SUM(tbl1[monthly sales]),
    FILTER(
        ALLSELECTED(tbl1[Product]),
        [rnk] <=current_ranking
    )

)

return s

 

 

 

cumult%

cumult % = 
[cumulative sum] 
/ 
CALCULATE(SUM(tbl1[monthly sales]), ALLSELECTED(tbl1[Product])
)

 

 

 

 

 

if my ansswer helped sort things out for you, i would appreciate a thumbs up 👍 and mark it as the solution
It makes a difference and might help someone else too. Thanks for spreading the good vibes! 🤠

 

View solution in original post

4 REPLIES 4
Daniel29195
Super User
Super User

@Power-BI4CPF 

 
 
please if possible, share some sample data, and display the desired output. 
 
 

CustomerProductmonthly salesSales%Comulative%
CustAProd123622%22%
CustAProd218517%39%
CustAProd318117%55%
CustAProd414213%68%
CustAProd513612%81%
CustAProd611511%91%
CustAProd7959%100%
CustBProd19219%19%
CustBProd28618%36%
CustBProd37916%53%
CustBProd47916%69%
CustBProd57816%85%
CustBProd67515%100%

 

I cant share teh data model. the data is sensitive. But this is more or less what i have. I want to calculate the running total for all product for each customer sorted from high to low

@Power-BI4CPF 

output

Daniel29195_0-1707550363179.png

 

 

measures : 
ranking measure : 

rnk = 
RANKX(
    ALLSELECTED(tbl1[Product]),CALCULATE(sum(tbl1[monthly sales])),,DESC)

    

 

 

measure cumul : 

cumulative sum = 
var current_ranking  = [rnk]


var s = 
CALCULATE(
    SUM(tbl1[monthly sales]),
    FILTER(
        ALLSELECTED(tbl1[Product]),
        [rnk] <=current_ranking
    )

)

return s

 

 

 

cumult%

cumult % = 
[cumulative sum] 
/ 
CALCULATE(SUM(tbl1[monthly sales]), ALLSELECTED(tbl1[Product])
)

 

 

 

 

 

if my ansswer helped sort things out for you, i would appreciate a thumbs up 👍 and mark it as the solution
It makes a difference and might help someone else too. Thanks for spreading the good vibes! 🤠

 

PowerBI4CPF_1-1707515259573.png

 

This is a list of all the product for a certain customer. The sales column is already sorted from high to low. I want the cumlutavie% column to be the running sum of Sale%. right not it is calculating the running sum but in a different order. In the end I want to be able to filter products by the top % (i.e. top50% or top 80%)

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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