จัดเรียงลำดับคอลัมน์ในตารางใหม่ตามลำดับที่ต้องการ พร้อมจัดการกรณีคอลัมน์หายไป
=Table.ReorderColumns(table as table, columnOrder as list, optional missingField as nullable number) as table
=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) |
let Sales = Table.FromRows( {{1, "Bob", "123-4567", 500}, {2, "Alice", "789-0123", 1200}}, {"Phone", "Name", "Amount", "ID"} ), Reordered = Table.ReorderColumns…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 |
let Data = Table.FromRows( {{"Bangkok", 5000000, "Thailand", 101}, {"New York", 8000000, "USA", 102}}, {"City", "Population", "Country", "ID"} ), Reordered = Ta…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 |
let Source = Table.FromRows( {{1, "John", "john@email.com"}}, {"ID", "Name", "Email"} ), SafeReorder = Table.ReorderColumns( Source, {"ID", "Name", "Email", "Ph…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 |
let RawData = Table.FromRows( {{"2024-01-15", 150, "Completed", 101, "Product A"}, {"2024-01-16", 250, "Pending", 102, "Product B"}}, {"Date", "Amount", "Status…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 |
คอลัมน์ที่ไม่ระบุจะยังคงปรากฏที่ตำแหน่งเดิม ตามหลังคอลัมน์ที่ระบุไว้ในรายชื่อ ตัวอย่าง: ถ้าตารางมี A, B, C, D แล้วเรา reorder เป็น {C, A} ผลลัพธ์จะเป็น C, A, B, D
คอลัมน์ที่ระบุในรายชื่อไม่มีอยู่ในตาราง ลองเช็คชื่อคอลัมน์ใหม่ (case-sensitive!) หรือใช้ MissingField.Ignore เพื่อข้ามคอลัมน์ที่ไม่มี
MissingField.UseNull จะสร้างคอลัมน์ใหม่ที่มีค่า null ถ้าคอลัมน์ไม่มี แต่ Ignore จะข้ามไป ใช้ UseNull เมื่อ receiver ต้องเห็นคอลัมน์ทั้งหมด แม้ว่าจะว่างเปล่า
ได้เลย ฟังก์ชันนี้เป็น lightweight operation ทำแค่จัดเรียงลำดับคอลัมน์ ไม่ได้ transform ข้อมูล ถึงแม้ข้อมูลมีแสนแถวก็ยังทำได้เร็ว
Table.ReorderColumns ใช้สำหรับจัดเรียงลำดับคอลัมน์ในตารางให้เป็นไปตามที่ต้องการ ซึ่งมีประโยชน์มากเมื่อต้องการให้คอลัมน์ที่สำคัญปรากฏไว้ด้านหน้า เช่น ID, Name ก่อน หรือเตรียมตารางให้พร้อมสำหรับการส่งออกข้อมูล
ที่เจ๋งคือ ฟังก์ชันนี้มี parameter ตัวที่สามคือ missingField ที่ช่วยให้จัดการกรณีที่บางคอลัมน์อาจไม่มีอยู่ในตาราง (ปัญหาที่พบบ่อยเวลา merge ข้อมูลจากหลายแหล่ง)
ส่วนตัวผม มักใช้ Table.ReorderColumns ก่อน export ข้อมูล หรือเมื่อต้องการให้ column ที่ receiver ต้องการปรากฏในลำดับที่ถูกต้อง ทำให้ไม่ต้องมานั่งจัดเรียงใน Excel ทีหลัง 😎