---
title: Table.ReorderColumns – จัดเรียงลำดับคอลัมน์ใหม่
url: https://www.thepexcel.com/functions/power-query/table-functions/table-reordercolumns/
type: function-explainer
program: Power Query
syntax: "Table.ReorderColumns(table as table, columnOrder as list, optional missingField as nullable number) as table"
date: 2025-12-03
updated: 2025-12-23
scores:
  popularity: 7
  difficulty: 2
  usefulness: 7
---

# Table.ReorderColumns – จัดเรียงลำดับคอลัมน์ใหม่

> จัดเรียงลำดับคอลัมน์ในตารางใหม่ตามลำดับที่ต้องการ พร้อมจัดการกรณีคอลัมน์หายไป

## คำอธิบาย

จัดเรียงลำดับคอลัมน์ในตารางใหม่ตามลำดับที่ต้องการ พร้อมจัดการกรณีคอลัมน์หายไป

## Syntax

```excel
Table.ReorderColumns(table as table, columnOrder as list, optional missingField as nullable number) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางข้อมูล |
| columnOrder | Yes | list |  | List ของชื่อคอลัมน์เรียงตามลำดับที่ต้องการ |
| missingField | No | number | - | จัดการกรณีไม่พบคอลัมน์ (เช่น MissingField.Ignore) |

## ตัวอย่าง

### 1. จัดเรียงคอลัมน์พื้นฐาน - ID ไปหน้าสุด

```excel
let
    Sales = Table.FromRows(
        {{1, "Bob", "123-4567", 500}, {2, "Alice", "789-0123", 1200}},
        {"Phone", "Name", "Amount", "ID"}
    ),
    Reordered = Table.ReorderColumns(Sales, {"ID", "Name", "Phone", "Amount"})
in
    Reordered
```

**ผลลัพธ์:** `| ID | Name  | Phone      | Amount |
|----|-------|------------|--------|
| 1  | Bob   | 123-4567   | 500    |
| 2  | Alice | 789-0123   | 1200   |`

ต้องการให้ ID อยู่ด้านหน้าสำหรับการรายงาน ฟังก์ชันจัดเรียงลำดับคอลัมน์ตามลำดับที่ระบุในรายชื่อ

### 2. จัดเรียงบางคอลัมน์ - คอลัมน์ที่เหลือปรากฏในตำแหน่งเดิม

```excel
let
    Data = Table.FromRows(
        {{"Bangkok", 5000000, "Thailand", 101}, {"New York", 8000000, "USA", 102}},
        {"City", "Population", "Country", "ID"}
    ),
    Reordered = Table.ReorderColumns(Data, {"Country", "City"})
in
    Reordered
```

**ผลลัพธ์:** `| Country   | City      | Population | ID  |
|-----------|-----------|------------|-----|
| Thailand  | Bangkok   | 5000000    | 101 |
| USA       | New York  | 8000000    | 102 |`

ระบุเฉพาะคอลัมน์ที่ต้องย้าย (Country, City) คอลัมน์ที่เหลือ (Population, ID) จะยังคงอยู่ในตำแหน่งเดิม

### 3. จัดการคอลัมน์ที่อาจไม่มีอยู่ - ใช้ MissingField.Ignore

```excel
let
    Source = Table.FromRows(
        {{1, "John", "john@email.com"}},
        {"ID", "Name", "Email"}
    ),
    SafeReorder = Table.ReorderColumns(
        Source,
        {"ID", "Name", "Email", "Phone", "Address"},
        MissingField.Ignore
    )
in
    SafeReorder
```

**ผลลัพธ์:** `| ID | Name | Email            |
|----|------|------------------|
| 1  | John | john@email.com   |`

ขอ Phone และ Address ที่ไม่มีในตาราง เพิ่ม MissingField.Ignore เพื่อไม่ให้เกิด error แค่ข้ามไป

### 4. จัดเรียงให้ตรงกับรูปแบบ Standard Report

```excel
let
    RawData = Table.FromRows(
        {{"2024-01-15", 150, "Completed", 101, "Product A"}, {"2024-01-16", 250, "Pending", 102, "Product B"}},
        {"Date", "Amount", "Status", "OrderID", "Product"}
    ),
    StandardFormat = Table.ReorderColumns(
        RawData,
        {"OrderID", "Date", "Product", "Amount", "Status"}
    )
in
    StandardFormat
```

**ผลลัพธ์:** `| OrderID | Date       | Product    | Amount | Status    |
|---------|------------|------------|--------|----------|
| 101     | 2024-01-15 | Product A  | 150    | Completed |
| 102     | 2024-01-16 | Product B  | 250    | Pending   |`

สร้างลำดับ standard ให้พร้อมส่งให้ client หรือ report ซึ่งต้องการเห็นข้อมูลในลำดับแบบนี้เสมอ

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

- ใช้ Table.ReorderColumns ก่อน Table.SelectColumns ถ้าต้องการให้ง่ายต่อการอ่านโค้ด (ReorderColumns จัดเรียงลำดับ จากนั้น SelectColumns เลือกคอลัมน์ที่ต้องการ)

- เมื่อ merge ข้อมูลจากหลายแหล่ง บางครั้งบางตารางมีคอลัมน์บางตาราง ไม่มี สำหรับกรณีนี้ใช้ MissingField.Ignore ก่อนแล้วค่อยจัดการคอลัมน์ที่หายไป

- ถ้าต้องการให้ output มี column ในลำดับ fixed ทุกครั้ง แม้ source เปลี่ยน ใช้ Table.ReorderColumns แล้วตามด้วย Table.SelectColumns เพื่อกำหนดให้ชัดเจน

- Power Query จำลำดับ column ตามลำดับ physical ของตาราง ถ้าเผลอไประบุลำดับผิด อาจทำให้ report ดู confusing ตรวจสอบลำดับ column ก่อน publish

- ใช้ Table.ColumnNames(table) เพื่อดู column names ทั้งหมด จากนั้นใช้เป็น reference สำหรับ ReorderColumns ลดความผิดพลาด

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

**Q: ถ้าผมไม่ระบุคอลัมน์ทั้งหมดในรายชื่อจะเกิดอะไร?**

คอลัมน์ที่ไม่ระบุจะยังคงปรากฏที่ตำแหน่งเดิม ตามหลังคอลัมน์ที่ระบุไว้ในรายชื่อ ตัวอย่าง: ถ้าตารางมี A, B, C, D แล้วเรา reorder เป็น {C, A} ผลลัพธ์จะเป็น C, A, B, D

**Q: มี error ว่า 'column not found' ทำไม?**

คอลัมน์ที่ระบุในรายชื่อไม่มีอยู่ในตาราง ลองเช็คชื่อคอลัมน์ใหม่ (case-sensitive!) หรือใช้ MissingField.Ignore เพื่อข้ามคอลัมน์ที่ไม่มี

**Q: แล้ว MissingField.UseNull คืออะไร? ต่างกับ Ignore ยังไง?**

MissingField.UseNull จะสร้างคอลัมน์ใหม่ที่มีค่า null ถ้าคอลัมน์ไม่มี แต่ Ignore จะข้ามไป ใช้ UseNull เมื่อ receiver ต้องเห็นคอลัมน์ทั้งหมด แม้ว่าจะว่างเปล่า

**Q: ใช้ Table.ReorderColumns ได้กับข้อมูลขนาดใหญ่มั้ย? Performance เป็นไง?**

ได้เลย ฟังก์ชันนี้เป็น lightweight operation ทำแค่จัดเรียงลำดับคอลัมน์ ไม่ได้ transform ข้อมูล ถึงแม้ข้อมูลมีแสนแถวก็ยังทำได้เร็ว

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

- [Microsoft Learn - Official Documentation](https://learn.microsoft.com/en-us/powerquery-m/table-reordercolumns) _(official)_
- [PowerQuery.how](https://powerquery.how/table-reordercolumns/) _(article)_

---

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