Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
7/10

Difficulty
2/10

Usefulness
7/10

Syntax & Arguments

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

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

Examples

จัดเรียงคอลัมน์พื้นฐาน – ID ไปหน้าสุด
let Sales = Table.FromRows( {{1, "Bob", "123-4567", 500}, {2, "Alice", "789-0123", 1200}}, {"Phone", "Name", "Amount", "ID"} ), Reordered = Table.ReorderColumns…
ต้องการให้ ID อยู่ด้านหน้าสำหรับการรายงาน ฟังก์ชันจัดเรียงลำดับคอลัมน์ตามลำดับที่ระบุในรายชื่อ
Power Query Formula:

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

Result:

| ID | Name | Phone | Amount |
|----|-------|------------|--------|
| 1 | Bob | 123-4567 | 500 |
| 2 | Alice | 789-0123 | 1200 |

จัดเรียงบางคอลัมน์ – คอลัมน์ที่เหลือปรากฏในตำแหน่งเดิม
let Data = Table.FromRows( {{"Bangkok", 5000000, "Thailand", 101}, {"New York", 8000000, "USA", 102}}, {"City", "Population", "Country", "ID"} ), Reordered = Ta…
ระบุเฉพาะคอลัมน์ที่ต้องย้าย (Country, City) คอลัมน์ที่เหลือ (Population, ID) จะยังคงอยู่ในตำแหน่งเดิม
Power Query Formula:

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

Result:

| Country | City | Population | ID |
|-----------|-----------|------------|-----|
| Thailand | Bangkok | 5000000 | 101 |
| USA | New York | 8000000 | 102 |

จัดการคอลัมน์ที่อาจไม่มีอยู่ – ใช้ MissingField.Ignore
let Source = Table.FromRows( {{1, "John", "john@email.com"}}, {"ID", "Name", "Email"} ), SafeReorder = Table.ReorderColumns( Source, {"ID", "Name", "Email", "Ph…
ขอ Phone และ Address ที่ไม่มีในตาราง เพิ่ม MissingField.Ignore เพื่อไม่ให้เกิด error แค่ข้ามไป
Power Query Formula:

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

Result:

| ID | Name | Email |
|----|------|------------------|
| 1 | John | john@email.com |

จัดเรียงให้ตรงกับรูปแบบ Standard Report
let RawData = Table.FromRows( {{"2024-01-15", 150, "Completed", 101, "Product A"}, {"2024-01-16", 250, "Pending", 102, "Product B"}}, {"Date", "Amount", "Status…
สร้างลำดับ standard ให้พร้อมส่งให้ client หรือ report ซึ่งต้องการเห็นข้อมูลในลำดับแบบนี้เสมอ
Power Query Formula:

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

Result:

| OrderID | Date | Product | Amount | Status |
|---------|------------|------------|--------|----------|
| 101 | 2024-01-15 | Product A | 150 | Completed |
| 102 | 2024-01-16 | Product B | 250 | Pending |

FAQs

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

Table.ReorderColumns ใช้สำหรับจัดเรียงลำดับคอลัมน์ในตารางให้เป็นไปตามที่ต้องการ ซึ่งมีประโยชน์มากเมื่อต้องการให้คอลัมน์ที่สำคัญปรากฏไว้ด้านหน้า เช่น ID, Name ก่อน หรือเตรียมตารางให้พร้อมสำหรับการส่งออกข้อมูล

ที่เจ๋งคือ ฟังก์ชันนี้มี parameter ตัวที่สามคือ missingField ที่ช่วยให้จัดการกรณีที่บางคอลัมน์อาจไม่มีอยู่ในตาราง (ปัญหาที่พบบ่อยเวลา merge ข้อมูลจากหลายแหล่ง)

ส่วนตัวผม มักใช้ Table.ReorderColumns ก่อน export ข้อมูล หรือเมื่อต้องการให้ column ที่ receiver ต้องการปรากฏในลำดับที่ถูกต้อง ทำให้ไม่ต้องมานั่งจัดเรียงใน Excel ทีหลัง 😎

Leave a Reply

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