---
title: Table.AddIndexColumn – เพิ่มคอลัมน์ลำดับเลข
url: https://www.thepexcel.com/functions/power-query/table-functions/table-addindexcolumn/
type: function-explainer
program: Power Query
syntax: "Table.AddIndexColumn(table, newColumnName, [initialValue], [increment], [columnType])"
date: 2025-12-03
updated: 2025-12-23
scores:
  popularity: 7
  difficulty: 3
  usefulness: 7
---

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

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

## คำอธิบาย

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

## Syntax

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

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | Table |  | ตารางต้นฉบับที่ต้องการเพิ่มคอลัมน์ลำดับเลข |
| newColumnName | Yes | Text |  | ชื่อของคอลัมน์ลำดับเลขที่จะเพิ่มเข้าไป เช่น "Index" หรือ "RowNumber" |
| initialValue | No | Number | 0 | ค่าเริ่มต้นของลำดับเลข (ค่าปกติ: 0) |
| increment | No | Number | 1 | ค่าการเพิ่มขึ้นสำหรับแต่ละแถว (ค่าปกติ: 1) |
| columnType | No | Type |  | ชนิดข้อมูลของคอลัมน์ใหม่ เช่น Int64.Type, Int32.Type (ค่าปกติ: ชนิดตัวเลข) |

## เคสการใช้งาน

### สร้าง Running Number

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

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

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

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

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

## ตัวอย่าง

### 1. เพิ่มลำดับเลขพื้นฐาน (เริ่มจาก 0)

```excel
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`

ใช้ Table.AddIndexColumn กับค่าเริ่มต้น (0) และค่าเพิ่มขึ้น (1) ค่าปกติ จะได้ลำดับเลข 0, 1, 2 สำหรับแต่ละแถว

### 2. ลำดับเลขเริ่มจาก 1

```excel
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`

โดยการตั้ง initialValue = 1 จะได้ลำดับเลขเริ่มจาก 1 แทนที่จะเป็น 0 ซึ่งเหมาะสำหรับผู้ใช้ที่ต้องการลำดับเลขทั่วไป

### 3. ลำดับเลขขั้นที่ 5

```excel
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`

การตั้ง initialValue = 10 และ increment = 5 จะให้ลำดับเลข 10, 15, 20, ... ซึ่งเหมาะสำหรับการสร้าง ID ที่มีช่องว่าง

### 4. ลำดับเลขพร้อมระบุชนิดข้อมูล

```excel
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`

การระบุ columnType = Int64.Type จะทำให้คอลัมน์ใหม่เป็นชนิด 64-bit integer ซึ่งเหมาะสำหรับตาราง Excel ที่ต้องการความเข้ากันได้

## หมายเหตุเพิ่มเติม

- ถ้าต้องการเพิ่มลำดับเลข 1, 2, 3, ... ให้ใช้ Table.AddIndexColumn(Source, "RowNumber", 1) โดยตั้ง initialValue = 1

- สามารถใช้ Table.AddIndexColumn ก่อนเพิ่มคอลัมน์อื่นๆ เพื่อให้ได้ index ที่เสถียร

- ถ้าต้องการลำดับเลขที่ลดลง ให้ใช้ค่า increment เป็นลบ เช่น Table.AddIndexColumn(Source, "Index", 100, -1)

- สามารถใช้ Table.AddIndexColumn ร่วมกับ Table.SelectRows เพื่อให้ได้ลำดับเลขของแถวที่กรองแล้ว

## คำถามที่พบบ่อย

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

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

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

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

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

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

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

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

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Table.AddIndexColumn](https://learn.microsoft.com/en-us/powerquery-m/table-addindexcolumn) _(official)_
- [PowerQuery.how - Table.AddIndexColumn](https://powerquery.how/table-addindexcolumn/) _(article)_
- [PowerQuery M Reference - Table.AddIndexColumn](https://pqm.guide/table-functions/table-addindexcolumn.html) _(article)_
- [RADACAD - Power Query Table Functions](https://radacad.com/power-query-formula-language-m-table-functions-part-1/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-addindexcolumn/](https://www.thepexcel.com/functions/power-query/table-functions/table-addindexcolumn/)_
