根据另一个索引列计算每行的累积值

问题描述 投票:0回答:2
给定一个表:

索引价值12.627.635.646.1
我想计算每行的累积总和,其中总和是通过对索引等于或大于当前行的

Index值的Value列求和来计算的,这样:

索引价值价值_暨12.621.927.619.335.611.746.16.1
在Excel中使用Power Query M语言。

为了更好地说明逻辑,使用 Excel 表语法通过以下公式计算了
Value_Cum 列:
=SUM(FILTER([Value];[Index]>=[@Index]))


编辑:

为了响应 @horseyride 提出的解决方案,它还必须适用于存在多个相同的

Index 值以及 Index 未排序的情况。 附加示例,其中具有累积值的源表如下:

索引价值价值_暨72.326.129.956.76329.132.546.8109.223.8105.923.83846.847.236.3108.723.8
并且如果我使用以下公式创建带有电源查询的

Custom 列:
= Table.AddColumn(#"Sorted Rows", "Custom", each List.Sum(List.RemoveFirstN(Source[Value],[Value]-1)))


它会导致错误,请查看结果(空值是错误):

索引价值价值_暨定制29.956.73846.815.932.546.847.236.36329.144.572.326.1108.723.8109.223.8105.923.8
excel powerbi powerquery m
2个回答
1
投票

let a = Table.SelectRows(#"Changed Type", (x)=> x[Index] >= [Index]), b = List.Sum(a[Value]) in b
    

1
投票
在 powerquery 中,添加列..自定义列...使用公式:

= List.Sum(List.RemoveFirstN(Source[Value],[Index]-1))

对于大型数据集,首先缓冲列

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], stored = List.Buffer(Source[Value]), #"Added Custom" = Table.AddColumn(Source, "Cumulative", each List.Sum(List.RemoveFirstN(stored,[Index]-1))) in #"Added Custom"
    
© www.soinside.com 2019 - 2024. All rights reserved.