แชร์ผ่าน


List.Accumulate

วากยสัมพันธ์

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

ประมาณ

สะสมค่าสรุปจากรายการในรายการที่ระบุโดยใช้ตัวสะสม

  • 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)
}