Table.AddIndexColumn ใช้สำหรับเพิ่มคอลัมน์ที่มีลำดับเลขให้กับตาราง ช่วยให้สามารถกำหนดหมายเลขแถวตามลำดับได้
=Table.AddIndexColumn(table, newColumnName, [initialValue], [increment], [columnType])
=Table.AddIndexColumn(table, newColumnName, [initialValue], [increment], [columnType])
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | Table | Yes | ตารางต้นฉบับที่ต้องการเพิ่มคอลัมน์ลำดับเลข | |
| newColumnName | Text | Yes | ชื่อของคอลัมน์ลำดับเลขที่จะเพิ่มเข้าไป เช่น “Index” หรือ “RowNumber” | |
| initialValue | Number | Optional | 0 | ค่าเริ่มต้นของลำดับเลข (ค่าปกติ: 0) |
| increment | Number | Optional | 1 | ค่าการเพิ่มขึ้นสำหรับแต่ละแถว (ค่าปกติ: 1) |
| columnType | Type | Optional | ชนิดข้อมูลของคอลัมน์ใหม่ เช่น Int64.Type, Int32.Type (ค่าปกติ: ชนิดตัวเลข) |
เพิ่มเลขลำดับ 1, 2, 3… เพื่อใช้อ้างอิงแถว
เพิ่ม Index ก่อน Sort เพื่อให้สามารถ Sort กลับมาเป็นลำดับเดิมได้ในภายหลัง
สร้าง Surrogate Key ให้กับ Dimension Table ที่ไม่มี ID
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithIndex = Table.…let
Sales = Table.FromRecords({
[Product = "Apple", Quantity = 5],
[Product = "Banana", Quantity = 3],
[Product = "Orange", Quantity = 7]
}),
WithIndex = Table.AddIndexColumn(Sales, "Index")
in
WithIndex
ตาราง 3 แถว โดยเพิ่มคอลัมน์ Index ที่มีค่า 0, 1, 2
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithRowNumber = Ta…let
Sales = Table.FromRecords({
[Product = "Apple", Quantity = 5],
[Product = "Banana", Quantity = 3],
[Product = "Orange", Quantity = 7]
}),
WithRowNumber = Table.AddIndexColumn(Sales, "RowNumber", 1)
in
WithRowNumber
ตาราง 3 แถว โดยคอลัมน์ RowNumber มีค่า 1, 2, 3
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithCustomIncremen…let
Sales = Table.FromRecords({
[Product = "Apple", Quantity = 5],
[Product = "Banana", Quantity = 3],
[Product = "Orange", Quantity = 7]
}),
WithCustomIncrement = Table.AddIndexColumn(Sales, "ID", 10, 5)
in
WithCustomIncrement
ตาราง 3 แถว โดยคอลัมน์ ID มีค่า 10, 15, 20
let Source = Table.FromRecords({ [Name = "John"], [Name = "Jane"], [Name = "Jack"] }), WithIndexInt64 = Table.AddIndexColumn(Source, "IndexID", 5, 5, Int64.Type…let
Source = Table.FromRecords({
[Name = "John"],
[Name = "Jane"],
[Name = "Jack"]
}),
WithIndexInt64 = Table.AddIndexColumn(Source, "IndexID", 5, 5, Int64.Type)
in
WithIndexInt64
ตาราง 3 แถว โดยคอลัมน์ IndexID เป็นชนิด Int64 ที่มีค่า 5, 10, 15
Table.RowCount จะนับจำนวนแถวทั้งหมด ส่วน Table.AddIndexColumn เพิ่มคอลัมน์ใหม่ที่มีลำดับเลขแต่ละแถว ทั้งสองสามารถใช้ร่วมกันได้
ไม่จำเป็น สามารถใช้ค่าลบได้ เช่น increment = -1 จะให้ลำดับเลขลดลง เช่น 10, 9, 8, 7, …
ได้ สามารถเรียก Table.AddIndexColumn หลายครั้ง โดยแต่ละครั้งเพิ่มคอลัมน์ลำดับเลขชื่อต่างกัน
ขึ้นอยู่ว่าคุณ filter ก่อนหรือหลัง ถ้า filter ก่อน แล้วค่อยเพิ่ม index จะได้ลำดับเลขจากแถวที่เหลือ
Table.AddIndexColumn เป็นฟังก์ชันที่ช่วยเพิ่มคอลัมน์ใหม่ลงในตารางพร้อมกับค่าลำดับเลข (index values) ซึ่งมีประโยชน์สำหรับการสร้างตัวเลขประจำตัวแถวหรือการเรียงลำดับข้อมูล
ส่วนตัวผมชอบใช้ Table.AddIndexColumn เพราะสามารถกำหนดค่าเริ่มต้นและค่าการเพิ่มขึ้นได้ตามต้องการ ไม่ว่าจะเริ่มจาก 0, 1 หรือตัวเลขอื่นๆ ก็ได้ อีกทั้งยังสามารถกำหนดชนิดข้อมูลของคอลัมน์ได้อีก 😎