Forum Discussion

Solle's avatar
Solle
Helper III
3 years ago

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

  • Wilson_'s avatar
    Wilson_
    Memorable Member

    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! 😄

    • Solle's avatar
      Solle
      Helper III

      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

      • Solle's avatar
        Solle
        Helper III

        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,

  • 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]))

     


     

     
     
    • Solle's avatar
      Solle
      Helper III

      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