Thep Excel

Table.AddIndexColumn – เพิ่มคอลัมน์ลำดับเลข

Table.AddIndexColumn ใช้สำหรับเพิ่มคอลัมน์ที่มีลำดับเลขให้กับตาราง ช่วยให้สามารถกำหนดหมายเลขแถวตามลำดับได้

=Table.AddIndexColumn(table, newColumnName, [initialValue], [increment], [columnType])

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
7/10

Difficulty
3/10

Usefulness
7/10

Syntax & Arguments

=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 (ค่าปกติ: ชนิดตัวเลข)

How it works

สร้าง Running Number

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

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

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

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

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

Examples

เพิ่มลำดับเลขพื้นฐาน (เริ่มจาก 0)
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithIndex = Table.…
ใช้ Table.AddIndexColumn กับค่าเริ่มต้น (0) และค่าเพิ่มขึ้น (1) ค่าปกติ จะได้ลำดับเลข 0, 1, 2 สำหรับแต่ละแถว
Power Query Formula:

let
    Sales = Table.FromRecords({
        [Product = "Apple", Quantity = 5],
        [Product = "Banana", Quantity = 3],
        [Product = "Orange", Quantity = 7]
    }),
    WithIndex = Table.AddIndexColumn(Sales, "Index")
in
    WithIndex

Result:

ตาราง 3 แถว โดยเพิ่มคอลัมน์ Index ที่มีค่า 0, 1, 2

ลำดับเลขเริ่มจาก 1
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithRowNumber = Ta…
โดยการตั้ง initialValue = 1 จะได้ลำดับเลขเริ่มจาก 1 แทนที่จะเป็น 0 ซึ่งเหมาะสำหรับผู้ใช้ที่ต้องการลำดับเลขทั่วไป
Power Query Formula:

let
    Sales = Table.FromRecords({
        [Product = "Apple", Quantity = 5],
        [Product = "Banana", Quantity = 3],
        [Product = "Orange", Quantity = 7]
    }),
    WithRowNumber = Table.AddIndexColumn(Sales, "RowNumber", 1)
in
    WithRowNumber

Result:

ตาราง 3 แถว โดยคอลัมน์ RowNumber มีค่า 1, 2, 3

ลำดับเลขขั้นที่ 5
let Sales = Table.FromRecords({ [Product = "Apple", Quantity = 5], [Product = "Banana", Quantity = 3], [Product = "Orange", Quantity = 7] }), WithCustomIncremen…
การตั้ง initialValue = 10 และ increment = 5 จะให้ลำดับเลข 10, 15, 20, … ซึ่งเหมาะสำหรับการสร้าง ID ที่มีช่องว่าง
Power Query Formula:

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

Result:

ตาราง 3 แถว โดยคอลัมน์ ID มีค่า 10, 15, 20

ลำดับเลขพร้อมระบุชนิดข้อมูล
let Source = Table.FromRecords({ [Name = "John"], [Name = "Jane"], [Name = "Jack"] }), WithIndexInt64 = Table.AddIndexColumn(Source, "IndexID", 5, 5, Int64.Type…
การระบุ columnType = Int64.Type จะทำให้คอลัมน์ใหม่เป็นชนิด 64-bit integer ซึ่งเหมาะสำหรับตาราง Excel ที่ต้องการความเข้ากันได้
Power Query Formula:

let
    Source = Table.FromRecords({
        [Name = "John"],
        [Name = "Jane"],
        [Name = "Jack"]
    }),
    WithIndexInt64 = Table.AddIndexColumn(Source, "IndexID", 5, 5, Int64.Type)
in
    WithIndexInt64

Result:

ตาราง 3 แถว โดยคอลัมน์ IndexID เป็นชนิด Int64 ที่มีค่า 5, 10, 15

FAQs

ต่างกับ Table.RowCount อย่างไร?

Table.RowCount จะนับจำนวนแถวทั้งหมด ส่วน Table.AddIndexColumn เพิ่มคอลัมน์ใหม่ที่มีลำดับเลขแต่ละแถว ทั้งสองสามารถใช้ร่วมกันได้

ค่า increment ต้องเป็นบวกเสมอหรือ?

ไม่จำเป็น สามารถใช้ค่าลบได้ เช่น increment = -1 จะให้ลำดับเลขลดลง เช่น 10, 9, 8, 7, …

เพิ่มลำดับเลขหลายคอลัมน์ได้หรือ?

ได้ สามารถเรียก Table.AddIndexColumn หลายครั้ง โดยแต่ละครั้งเพิ่มคอลัมน์ลำดับเลขชื่อต่างกัน

ลำดับเลขจะยังคงเหมือนเดิมหลังจากกรองข้อมูล (filter) หรือ?

ขึ้นอยู่ว่าคุณ filter ก่อนหรือหลัง ถ้า filter ก่อน แล้วค่อยเพิ่ม index จะได้ลำดับเลขจากแถวที่เหลือ

Resources & Related

Additional Notes

Table.AddIndexColumn เป็นฟังก์ชันที่ช่วยเพิ่มคอลัมน์ใหม่ลงในตารางพร้อมกับค่าลำดับเลข (index values) ซึ่งมีประโยชน์สำหรับการสร้างตัวเลขประจำตัวแถวหรือการเรียงลำดับข้อมูล

ส่วนตัวผมชอบใช้ Table.AddIndexColumn เพราะสามารถกำหนดค่าเริ่มต้นและค่าการเพิ่มขึ้นได้ตามต้องการ ไม่ว่าจะเริ่มจาก 0, 1 หรือตัวเลขอื่นๆ ก็ได้ อีกทั้งยังสามารถกำหนดชนิดข้อมูลของคอลัมน์ได้อีก 😎

Leave a Reply

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