跳至主要内容

List.Accumulate

根據清單中的項目累積摘要值。

Syntax

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

Remarks

使用累加器從指定清單中的項目累積摘要值。

  • list:要逐一查看的清單。
  • seed:初始累積值。
  • accumulator:接受目前狀態和目前項目並傳回新狀態的函式。

Examples

Example #1

根據清單中的項目累積摘要值。

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

Result:

15

Example #2

串聯清單中的每個字組與之間的空格,但開頭不包含空格。

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

Result:

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

Example #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"

Result:

{
#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)
}

Category

List.Transformation functions