Udostępnij przez


List.Accumulate

Składnia

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

Informacje

Gromadzi wartość podsumowania z elementów na określonej liście przy użyciu akumulatora.

  • list: lista do iterowania.
  • seed: początkowa skumulowana wartość.
  • accumulator: funkcja, która przyjmuje bieżący stan i bieżący element i zwraca nowy stan.

Przykład 1

Gromadzi wartość sumaryczną z elementów na liście.

użycie

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

wyjściowe dane

15

Przykład 2

Połącz każde słowo na liście z spacją między, ale nie dołączaj spacji na początku.

użycie

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

Wyjście"The quick brown fox jumps over the lazy dog."

Przykład 3

Utwórz listę czasów ukończenia procesu od daty rozpoczęcia i listy czasów wykonywania procesu.

użycie

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"

wyjściowe dane

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