ตัวอย่างที่ 1: Index เริ่มจาก 0 (Default)
=Table.AddIndexColumn(Source, "Index")
0, 1, 2, ...
เพิ่มคอลัมน์เลขเรียงลำดับ (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
=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) |
เพิ่มเลขลำดับ 1, 2, 3… เพื่อใช้อ้างอิงแถว
เพิ่ม Index ก่อน Sort เพื่อให้สามารถ Sort กลับมาเป็นลำดับเดิมได้ในภายหลัง
สร้าง Surrogate Key ให้กับ Dimension Table ที่ไม่มี ID
=Table.AddIndexColumn(Source, "Index")
0, 1, 2, ...
=Table.AddIndexColumn(Source, "No.", 1, 1)
1, 2, 3, ...
=Table.AddIndexColumn(Source, "ID", 10, 10)
10, 20, 30, ...
Index จะถูกสร้างใหม่ทุกครั้งที่รีเฟรช ตามลำดับข้อมูลที่เข้ามาในขั้นตอนนี้ ถ้าข้อมูลต้นทางเปลี่ยนลำดับ Index ก็จะเปลี่ยนตาม
ฟังก์ชัน Table.AddIndexColumn ใน Power Query ใช้สำหรับเพิ่มคอลัมน์ใหม่ที่มีค่าเป็นตัวเลขเรียงลำดับ (Index) ให้กับแต่ละแถวในตาราง เช่น 0, 1, 2… หรือ 1, 2, 3… เหมาะสำหรับสร้าง Primary Key หรือลำดับที่