如果另一列中的前一行相同,则累积和

Cumulative Sum if previous row in another column is the same

我正在尝试做一些在 excel 中很简单的事情,但我需要将其添加到我的查询中。如果名称列等于前一行,我正在尝试在一列的新时间值列中创建累积和。下面的前后示例

数据输入Table

ID Time
A 2
B 3
C 1
D 0.5
E 1
E 3
E 5
F 2
G 3
G 4
H 1

Table查询后

ID Time BeforeStart
A 2 0
B 3 0
C 1 0
D 0.5 0
E 1 0
E 3 1
E 5 4
F 2 0
G 3 0
G 4 3
H 1 0

基本上,如果列 ID 等于其上方的行,则将时间和其上方的 BeforeStart 行相加,否则为 0。

试试这个功能:

=IF(COUNTIF($A2:$A,$A2)=1,0,SUMPRODUCT((A2:$A=A2)*B2:$B)-B2)

在 power query 中,尝试下面并将表 2 替换为您的源数据的名称 table

let 
  xFunction = (xTable as table) as table => let
  #"Added Index" = Table.AddIndexColumn(xTable, "Index", 1, 1, Int64.Type),
  #"Added Custom" = Table.AddColumn(#"Added Index", "Running Total", each List.Sum(List.FirstN(#"Added Index"[Time],[Index]-1)))
  in  #"Added Custom",

Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Time", type number}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"data", each  xFunction(_), type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Time", "Running Total"}, {"Time", "Running Total"})
in #"Expanded data"