求 MAX 值的累积和 [英] Finding cummulative sum of MAX values

查看:14
本文介绍了求 MAX 值的累积和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算每个时期(或每个类别)的最大值的累积总和.查看嵌入的图像.

所以,首先,我需要找到每年每个类别/月的最大值.然后我想计算这些最大值的累积和.我通过设置最大度量值进行了尝试(第一步工作正常 - 找到给定年份的每个类别/月的最大值),但后来我找不到找到累积 SUM 的解决方案(找到累积最大值很容易,但它不是我要找的东西).

表 1

Year MonthlyValue MaxPerYear2016 年 1 月 10 日 15 日2016 年 2 月 15 日 15 日2016 年 3 月 12 日 15 日2017 年 1 月 22 日 22 日2017 年 2 月 19 日 22 日2017 年 3 月 12 日 22 日2018 年 1 月 5 日 17 日2018 年 2 月 16 日 17 日2018 年 3 月 17 日 17 日

期望的输出

年累计2016 152017 372018 54

解决方案

这个有点类似

如果你展开到月份级别,那么它看起来像这样:

<小时>

请注意,如果您只需要小计工作,则将每行保留为最大值 (15, 22, 17, 54) 而不是最大值的累积总和 (15, 37, 54,54),那么您可以使用更简单的方法:

MaxSum =总和(值(表 1 [年份]),计算(最大值(表 1[月值])))

这会分别计算每年的最大值,然后将它们相加.

<小时>

外部参考:

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 per year. Then I want to calculate the cumulative SUM of these max values. I tried it by setting up max measure (which works fine for the first step - finding max per category/month for a given year) but then I fail at finding a solution to finding cumulative SUM (finding the cumulative Max is easy, but it is not what I'm looking for).

Table1

Year  Month  MonthlyValue  MaxPerYear
2016  Jan    10            15
2016  Feb    15            15
2016  Mar    12            15
2017  Jan    22            22
2017  Feb    19            22
2017  Mar    12            22
2018  Jan     5            17
2018  Feb    16            17
2018  Mar    17            17

Desired Output

Year  CumSum
2016  15
2017  37
2018  54

解决方案

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:

这篇关于求 MAX 值的累积和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆