Thep Excel

Table.AddIndexColumn – เพิ่มคอลัมน์ดัชนี (ลำดับที่)

เพิ่มคอลัมน์เลขเรียงลำดับ (Index) ให้กับตาราง

Table.AddIndexColumn จะสร้างคอลัมน์ใหม่โดยใส่ตัวเลขเรียงลำดับเริ่มต้นจาก initialValue และเพิ่มขึ้นทีละ increment ให้กับทุกแถวในตาราง

=Table.AddIndexColumn(table as table, newColumnName as text, optional initialValue as number, optional increment as number, optional columnType as nullable type) as table

By ThepExcel AI Agent
3 December 2025

Syntax & Arguments

=Table.AddIndexColumn(table as table, newColumnName as text, optional initialValue as number, optional increment as number, optional columnType as nullable type) as table

Argument Type Required Default Description
table table Yes ตารางที่ต้องการเพิ่ม Index
newColumnName text Yes ชื่อคอลัมน์ใหม่
initialValue number Optional 0 ค่าเริ่มต้น (ถ้าไม่ระบุคือ 0)
increment number Optional 1 ค่าที่เพิ่มขึ้นในแต่ละแถว (ถ้าไม่ระบุคือ 1)
columnType type Optional null ชนิดข้อมูลของคอลัมน์ (เช่น Int64.Type)

How it works

สร้าง Running Number

เพิ่มเลขลำดับ 1, 2, 3… เพื่อใช้อ้างอิงแถว

คืนค่าลำดับเดิมหลัง Sort

เพิ่ม Index ก่อน Sort เพื่อให้สามารถ Sort กลับมาเป็นลำดับเดิมได้ในภายหลัง

สร้าง Key สำหรับ Relationship

สร้าง Surrogate Key ให้กับ Dimension Table ที่ไม่มี ID

Examples

ตัวอย่างที่ 1: Index เริ่มจาก 0 (Default)

เพิ่มคอลัมน์ Index เริ่มต้นที่ 0 และเพิ่มทีละ 1
Power Query Formula:

=Table.AddIndexColumn(Source, "Index")

Result:

0, 1, 2, ...

ตัวอย่างที่ 2: Index เริ่มจาก 1

เพิ่มคอลัมน์ No. เริ่มต้นที่ 1 และเพิ่มทีละ 1 (เหมาะสำหรับแสดงลำดับที่)
Power Query Formula:

=Table.AddIndexColumn(Source, "No.", 1, 1)

Result:

1, 2, 3, ...

ตัวอย่างที่ 3: Index เพิ่มทีละ 10

เริ่มต้นที่ 10 และเพิ่มทีละ 10
Power Query Formula:

=Table.AddIndexColumn(Source, "ID", 10, 10)

Result:

10, 20, 30, ...

FAQs

Index จะเปลี่ยนเมื่อรีเฟรชข้อมูลไหม?

Index จะถูกสร้างใหม่ทุกครั้งที่รีเฟรช ตามลำดับข้อมูลที่เข้ามาในขั้นตอนนี้ ถ้าข้อมูลต้นทางเปลี่ยนลำดับ Index ก็จะเปลี่ยนตาม

Resources & Related

Additional Notes

ฟังก์ชัน Table.AddIndexColumn ใน Power Query ใช้สำหรับเพิ่มคอลัมน์ใหม่ที่มีค่าเป็นตัวเลขเรียงลำดับ (Index) ให้กับแต่ละแถวในตาราง เช่น 0, 1, 2… หรือ 1, 2, 3… เหมาะสำหรับสร้าง Primary Key หรือลำดับที่

Leave a Reply

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