Thep Excel

List.Accumulate – วนลูปสะสมค่า

วนลูปคำนวณและสะสมค่าจากสมาชิกใน List

List.Accumulate จะรับ List และค่าเริ่มต้น (Seed) จากนั้นจะส่งค่าเข้าไปในฟังก์ชัน Accumulator ทีละรอบ เพื่อนำค่าปัจจุบัน (Current) ไปคำนวณร่วมกับค่าสะสม (State) และส่งต่อผลลัพธ์ไปรอบถัดไป

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

By ThepExcel AI Agent
3 December 2025

Syntax & Arguments

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

Argument Type Required Default Description
list list Yes List ที่ต้องการนำมาวนลูป
seed any Yes ค่าเริ่มต้น (Initial Value) สำหรับเริ่มการสะสม
accumulator function Yes ฟังก์ชันที่กำหนดวิธีการคำนวณ (รับค่า state, current)

How it works

คำนวณยอดคงเหลือสะสม

วนลูปยอดรายรับ-รายจ่าย เพื่อหายอดเงินคงเหลือสุดท้าย

แทนที่คำหลายคำพร้อมกัน

วนลูป List ของคำที่ต้องการเปลี่ยน เพื่อแทนที่คำเหล่านั้นในข้อความหลักทีละคำ (Bulk Replace)

รวมข้อความแบบมีเงื่อนไข

รวมข้อความจาก List เข้าด้วยกันโดยใส่ตัวคั่นเฉพาะจุดที่ต้องการ

Examples

ตัวอย่างที่ 1: ผลรวมสะสม (Sum)

เริ่มที่ 0 แล้วบวกค่าปัจจุบันเข้าไปเรื่อยๆ (0+1=1, 1+2=3, 3+3=6, …)
Power Query Formula:

=List.Accumulate({1, 2, 3, 4, 5}, 0, (state, current) => state + current)

Result:

15

ตัวอย่างที่ 2: แทนที่คำหลายคำ

วนลูป List ของคู่คำศัพท์ เพื่อแทนที่คำว่า "Old" เป็น "New" และ "Bad" เป็น "Good" ในข้อความตั้งต้น
Power Query Formula:

=List.Accumulate({{"Old", "New"}, {"Bad", "Good"}}, "This is Old and Bad", (state, current) => Text.Replace(state, current{0}, current{1}))

Result:

This is New and Good

ตัวอย่างที่ 3: สร้าง List ของเลขคูณ 2

เริ่มด้วย List ว่าง {} แล้วนำค่าปัจจุบันคูณ 2 ไปต่อท้าย List เดิมทีละตัว
Power Query Formula:

=List.Accumulate({1, 2, 3}, {}, (state, current) => state & {current * 2})

Result:

{2, 4, 6}

FAQs

List.Accumulate เหมือนกับ List.Sum ไหม?

List.Sum หาผลรวมตัวเลขได้อย่างเดียว แต่ List.Accumulate สามารถทำอะไรก็ได้ที่ต้องการวนลูป (เช่น รวมข้อความ, สร้าง List ใหม่)

Resources & Related

Additional Notes

ฟังก์ชัน List.Accumulate ใน Power Query ใช้สำหรับวนลูป (Loop) ผ่านสมาชิกแต่ละตัวใน List เพื่อทำการคำนวณและสะสมค่าไปเรื่อยๆ (Accumulate) จนได้ผลลัพธ์สุดท้าย คล้ายกับฟังก์ชัน REDUCE ใน Excel

Leave a Reply

Your email address will not be published. Required fields are marked *