Thep Excel

Table.Sort – เรียงลำดับข้อมูลในตาราง

เรียงลำดับข้อมูลในตารางตามคอลัมน์ที่ระบุ รองรับการเรียงหลายระดับและกำหนดทิศทางการเรียง

=Table.Sort(table as table, comparisonCriteria as any) as table

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
9/10

Difficulty
2/10

Usefulness
9/10

Syntax & Arguments

=Table.Sort(table as table, comparisonCriteria as any) as table

Argument Type Required Default Description
table Table Yes ตารางที่ต้องการเรียงลำดับ
comparisonCriteria List Yes เกณฑ์การเรียงลำดับ อาจเป็น: 1) ชื่อคอลัมน์เดียว (text) = เรียง ascending เป็นค่าเริ่มต้น 2) List ของ text = เรียงหลายคอลัมน์ตามลำดับ 3) List ของ {text, Order} = ระบุทิศทาง Order.Ascending หรือ Order.Descending

Examples

เรียงตามคอลัมน์เดียว (Ascending)
let Sales = #table( {"OrderID", "Amount"}, {{3, 500}, {1, 1200}, {2, 800}} ), Sorted = Table.Sort(Sales, {"OrderID"}) in Sorted
เรียงตาม OrderID จากน้อยไปมากแบบอัตโนมัติ (1, 2, 3) – ส่วนใหญ่เราใช้แบบนี้เวลาต้องเรียงลำดับที่เป็นตัวเลข
Power Query Formula:

let
    Sales = #table(
        {"OrderID", "Amount"},
        {{3, 500}, {1, 1200}, {2, 800}}
    ),
    Sorted = Table.Sort(Sales, {"OrderID"})
in
    Sorted

Result:

| OrderID | Amount |
|---------|--------|
| 1 | 1200 |
| 2 | 800 |
| 3 | 500 |

เรียงลดลง (Descending) + ระบุ Order.Descending
let Sales = #table( {"OrderID", "Amount"}, {{3, 500}, {1, 1200}, {2, 800}} ), Sorted = Table.Sort(Sales, {{"Amount", Order.Descending}}) in Sorted
เรียงตาม Amount จากมากไปน้อย ด้วย Order.Descending – ใช้เมื่อต้องการเห็นจำนวนที่มากที่สุดก่อน
Power Query Formula:

let
    Sales = #table(
        {"OrderID", "Amount"},
        {{3, 500}, {1, 1200}, {2, 800}}
    ),
    Sorted = Table.Sort(Sales, {{"Amount", Order.Descending}})
in
    Sorted

Result:

| OrderID | Amount |
|---------|--------|
| 1 | 1200 |
| 2 | 800 |
| 3 | 500 |

เรียงหลายคอลัมน์ (Multi-Level Sort)
let Sales = #table( {"Region", "Month", "Sales"}, {{"North", "Jan", 100}, {"South", "Jan", 250}, {"North", "Feb", 150}, {"South", "Feb", 200}} ), Sorted = Table…
เรียงแบบ 3 ระดับ: ตาม Region ก่อน (A-Z) แล้วตาม Month ถ้า Region เหมือนกัน สุดท้ายตาม Sales จากมากไปน้อย – เหมาะสำหรับรายงานที่ต้องจัดกลุ่มตามพื้นที่
Power Query Formula:

let
    Sales = #table(
        {"Region", "Month", "Sales"},
        {{"North", "Jan", 100}, {"South", "Jan", 250}, {"North", "Feb", 150}, {"South", "Feb", 200}}
    ),
    Sorted = Table.Sort(
        Sales,
        {{"Region", Order.Ascending}, {"Month", Order.Ascending}, {"Sales", Order.Descending}}
    )
in
    Sorted

Result:

| Region | Month | Sales |
|--------|-------|-------|
| North | Feb | 150 |
| North | Jan | 100 |
| South | Feb | 200 |
| South | Jan | 250 |

เรียงข้อมูลตัวอักษร (Case Sensitive)
let Names = #table( {"Name"}, {{"zebra"}, {"Apple"}, {"banana"}, {"Cherry"}} ), Sorted = Table.Sort(Names, {"Name"}) in Sorted
Power Query เรียงแบบ case-sensitive ตัวใหญ่มาก่อนตัวเล็ก ถ้าต้อง case-insensitive ให้แปลงเป็นตัวเล็กก่อนเรียง หรือใช้ Comparer
Power Query Formula:

let
    Names = #table(
        {"Name"},
        {{"zebra"}, {"Apple"}, {"banana"}, {"Cherry"}}
    ),
    Sorted = Table.Sort(Names, {"Name"})
in
    Sorted

Result:

| Name |
|--------|
| Apple |
| Cherry |
| banana |
| zebra |

FAQs

ฉันต้องใช้ List เล็กน้อยสำหรับ comparisonCriteria ถึงจะไปได้?

ครับ ถ้าเป็นการเรียงแบบเดียวอันเดียวสามารถใช้ {“ColumnName”} หรือ {{“ColumnName”, Order.Ascending}} ถ้าเรียงหลายคอลัมน์ต้องเป็น {{“Col1”, Order.Asc}, {“Col2”, Order.Desc}} – เป็น list ของ list นั่นเอง

ความแตกต่างระหว่าง Order.Ascending และ Order.Descending คืออะไร?

Order.Ascending = จากน้อยไปมาก (A-Z หรือ 0-9) / Order.Descending = จากมากไปน้อย (Z-A หรือ 9-0) – ส่วนใหญ่ใช้ Descending เมื่อต้องการเห็นรายการสำคัญก่อน เช่น ยอดขายสูงสุด

Table.Sort จะรักษาลำดับแถวเดิมไว้ไหมถ้าค่าในคอลัมน์เรียงเหมือนกัน?

ไม่แน่นอน Power Query ไม่รับประกันลำดับแถวเดิม (stable sort) ถ้าสำคัญเก่ามากต้องเพิ่มคอลัมน์ index ก่อน เรียงตามค่าของอันแรก แล้วเรียงตามค่า index เป็นระดับสุดท้าย

ถ้าคอลัมน์มีค่า null หรือเป็นค่าว่าง Table.Sort จะจัดการอย่างไร?

ค่า null จะถูกเรียงไปไว้ที่ด้านหน้าเสมอ (ก่อนค่าปกติ) ถ้าต้องจัดการพิเศษต้องใช้ Table.SelectRows เพื่อแยกแถว null ออกก่อน แล้วต่อรวมกลับหลังจากเรียง

Resources & Related

Additional Notes

Table.Sort ใช้สำหรับเรียงลำดับแถวในตารางตามเกณฑ์ที่ระบุ เหมือนกับการใช้ Sort ใน Excel แต่ทำงานในระดับ Power Query ได้ตั้งแต่เริ่มต้น ฟังก์ชันนี้รองรับการเรียงแบบ ascending (จากน้อยไปมาก) หรือ descending (จากมากไปน้อย) และสามารถเรียงตามหลายคอลัมน์พร้อมกันได้ ซึ่งมีประโยชน์เมื่อต้องการจัดข้อมูลตามลำดับความสำคัญหรือเตรียมข้อมูลสำหรับการวิเคราะห์เพิ่มเติม

Leave a Reply

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