List.Accumulate

Syntax

List.Accumulate(
    list as list,
    seed as any,
    accumulator as function
) as any

About

使用累加器从指定列表中的项累积摘要值。

  • list:要遍历的列表。
  • seed:初始累积值。
  • accumulator:一个函数,它接收当前状态和当前项,并返回经过处理的新状态。

示例 1

汇总列表中各项的值。

用法

let
    Source = List.Accumulate(
        {1, 2, 3, 4, 5},
        0,
        (runningSum, nextNumber) => runningSum + nextNumber
    )
in
    Source

输出

15

示例 2

将列表中的每个单词与空格连接在一起,但不包括开头的空格。

用法

let
    Source = List.Accumulate(
        {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."},
        null,
        (fullTextSoFar, nextPart) =>
            Text.Combine({fullTextSoFar, nextPart}, " ")
    )
in
    Source

输出"The quick brown fox jumps over the lazy dog."

示例 3

从开始日期生成进程完成时间列表和进程运行时间列表。

用法

let
    #"Process Duration" = 
    {
        #duration(0,1,0,0),
        #duration(0,2,0,0),
        #duration(0,3,0,0)
    },
    #"Start Time" = #datetime(2025, 9, 8, 19, 0, 0),
    #"Process Timeline" = List.Accumulate(
        #"Process Duration",
        {#"Start Time"},
        (accumulatedTimes, nextDuration) => 
            accumulatedTimes & {List.Last(accumulatedTimes) + nextDuration}
    )
in
    #"Process Timeline"

输出

{
    #datetime(2025, 9, 8, 19, 0, 0),
    #datetime(2025, 9, 8, 20, 0, 0),
    #datetime(2025, 9, 8, 22, 0, 0),
    #datetime(2025, 9, 9, 1, 0, 0)
}