Finding cummulative sum of MAX values

前端 未结 1 1466
逝去的感伤
逝去的感伤 2020-12-20 08:01

I need to calculate the cumulative sum of Max value per period (or per category). See the embedded image.

So, first, I need to find max value for each category/month

相关标签:
1条回答
  • 2020-12-20 08:10

    This is a bit similar to this question and this question and this question as far as subtotaling, but also includes a cumulative component as well.

    You can do this in two steps. First, calculate a table that gives the max for each year and then use a cumulative total pattern.

    CumSum = 
    VAR Summary =
        SUMMARIZE(
            ALLSELECTED(Table1),
            Table1[Year],
            "Max",
            MAX(Table1[MonthlyValue])
        )
    RETURN
        SUMX(
            FILTER(
                Summary, 
                Table1[Year] <= MAX(Table1[Year])
            ),
            [Max]
        )
    

    Here's the output:

    If you expand to the month level, then it looks like this:


    Note that if you only need the subtotal to work leaving each row as a max (15, 22, 17, 54) rather than as a cumulative sum of maxes (15, 37, 54, 54), then you can use a simpler approach:

    MaxSum =
        SUMX(
            VALUES( Table1[Year] ),
            CALCULATE( MAX( Table1[MonthlyValue] ) )
        )
    

    This calculates the max for each year separately and then adds them together.


    External References:

    • Subtotals and Grand Totals That Add Up “Correctly”

    • Cumulative Total - DAX Patterns

    0 讨论(0)
提交回复
热议问题