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
Solle
Helper III
Helper III

Cumulative sum on summarize table

Hi All, 

I have a summarize function from the below code:

VAR v1 = 
SUMMARIZE(
    FILTER(
        FactSales,
        FactSales[AsOfDate] = CALCULATE(MAX(FactSales[AsOfDate]), ALL(DimDate))
    ),
    DimDate[Year],
    "Last As Of Date", MAX(FactSales[AsOfDate]),
    "Last Sales", LASTNONBLANK(FactSales[Sales], 0)
)



Which provides this output

YearLast As Of DateLast Sales
202328-02-2023 00:00:001037467263,3053
202231-12-2022 00:00:002132113394,1337
202131-12-2021 00:00:002726595255,5036
202031-12-2020 00:00:004503000000
201931-12-2019 00:00:004372000000
201831-12-2018 00:00:003985000000
201731-12-2017 00:00:003681000000
201631-12-2016 00:00:003101000000
201531-12-2015 00:00:002607000000
201431-12-2014 00:00:002245000000
201331-12-2013 00:00:002144000000
201231-12-2012 00:00:001443300000
201131-12-2011 00:00:001470900000
201031-12-2010 00:00:003486451000
200931-12-2009 00:00:003520772000

 

What I then need is a cumulative sale based on the above table, but I cannot figure it out. I have tried using the SUMX function but it does not seem to work. 

Can someone help me out on this?

Best Regards,
Solle

8 REPLIES 8
Wilson_
Solution Sage
Solution Sage

Hello Jeppe,

 

By cumulative sum, do you mean you would like 2009 to show 2009 sales, 2010 to show 2009-2010 sales, 2011 to show 2009-2011 sales, etc.?

 

If so, knowing nothing else about your model beyond this table (and assuming you would like this as a calculated column), you can try something like:

Cumulative Sales =

-- This variable filters for only the rows that are before or equal to the date in the table row
VAR FilteredTable =
FILTER (
    Table1,
    Table1[Last Date] <= EARLIER ( Table1[Last Date] )
)

-- This variable then takes only the filtered rows from the above table and sums up all the sales values
VAR Result =
SUMX (
    FilteredTable,
    [Last Sales]
)

RETURN Result

 

Please let me know if you were looking for something different, Jeppe! 😄

Hi @Wilson_ 

Thank you for taking your time to respond - I see that my code snippet from the summarize function didnt show properly, I have just updated my current summarize function, which I prefer to evolve into a cumulative DAX measure. 

Yes I mean cumulative like you show above. Your code unfortunately does not apply as expected due to my summarize function I assume. Do you have any idea on how to fix the cumulative function?

Best Regards,
Solle

Hi @Wilson_ 

I think your solution using SUMX correctly cumulates the data, but how can I avoid it from being affect from a date slicer, which is on my page? As what I want to accomplish is to always show the total no matter what dates are selected. 

VAR v1 = 
SUMMARIZE(
    FILTER(
        FactSales,
        FactSales[AsOfDate] = CALCULATE(MAX(FactSales[AsOfDate]), ALL(DimDate))
    ),
    DimDate[Year],
    "Last As Of Date", MAX(FactSales[AsOfDate]),
    "Last Sales", LASTNONBLANK(FactSales[Sales], 0)
)

VAR Result =
SUMX(v1,
    [Last Sales]
)

RETURN
Result



Best Regards,

Hi Solle,

 

Can you help clarify your request? If you want one cumulative total amount that ignores the date slicer, are you not just looking for total sales? If that's not what you're looking for, what number are you expecting this measure to return, based on the results of the original post's table? 

Hi @Wilson_ ,

I am actually getting what I want but not just for the subtotals, which was the one I was focussing on, I didnt realize that the subtotal could sum to something different than the above rows. 

So I am achieving what I seek but for the totals/subtotals in my table I am getting some very weird values, which does not correspond to the rows above, do you know how to fix this?

Product CategorySub Product CategoryCumulative Sales
11927
127.953
13 
14245
15 
161.216
1Subtotal7.953
2Subtotal792
3Subtotal2.761
Total 7.953


I have attached the results from my matrix where the correct subtotal for product category 1 should be 10.341.

Best Regards,
Solle

Hey Solle,

 

I think at this point, it might be easier for me if you could provide your pbix file (or a sanitized/smaller version) so I can look under the hood. 😄

andhiii079845
Super User
Super User

Try this:

Measure = 
 VAR _Currentyear = SELECTEDVALUE('sales'[Year])
 RETURN IF(HASONEFILTER(sales[Year]),sumx(FILTER(ALLSELECTED('sales'),'sales'[Year]<=_Currentyear),'sales'[Last Sales]),sum(sales[Last Sales]))
andhiii079845_1-1680445973078.png

 


 

 
 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @andhiii079845 
Thank you for taking your time to respond. I have jut tried your code, but as my summarize code didnt show properly, I am unfortunate that your code snippet does not work, do you have any better suggestion? 

Best Regards,
Solle

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.

Top Solution Authors