เรียงลำดับข้อมูลในตารางตามคอลัมน์ที่ระบุ รองรับการเรียงหลายระดับและกำหนดทิศทางการเรียง
=Table.Sort(table as table, comparisonCriteria as any) as table
=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 |
let Sales = #table( {"OrderID", "Amount"}, {{3, 500}, {1, 1200}, {2, 800}} ), Sorted = Table.Sort(Sales, {"OrderID"}) in Sortedlet
Sales = #table(
{"OrderID", "Amount"},
{{3, 500}, {1, 1200}, {2, 800}}
),
Sorted = Table.Sort(Sales, {"OrderID"})
in
Sorted
| OrderID | Amount |
|---------|--------|
| 1 | 1200 |
| 2 | 800 |
| 3 | 500 |
let Sales = #table( {"OrderID", "Amount"}, {{3, 500}, {1, 1200}, {2, 800}} ), Sorted = Table.Sort(Sales, {{"Amount", Order.Descending}}) in Sortedlet
Sales = #table(
{"OrderID", "Amount"},
{{3, 500}, {1, 1200}, {2, 800}}
),
Sorted = Table.Sort(Sales, {{"Amount", Order.Descending}})
in
Sorted
| OrderID | Amount |
|---------|--------|
| 1 | 1200 |
| 2 | 800 |
| 3 | 500 |
let Sales = #table( {"Region", "Month", "Sales"}, {{"North", "Jan", 100}, {"South", "Jan", 250}, {"North", "Feb", 150}, {"South", "Feb", 200}} ), Sorted = Table…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
| Region | Month | Sales |
|--------|-------|-------|
| North | Feb | 150 |
| North | Jan | 100 |
| South | Feb | 200 |
| South | Jan | 250 |
let Names = #table( {"Name"}, {{"zebra"}, {"Apple"}, {"banana"}, {"Cherry"}} ), Sorted = Table.Sort(Names, {"Name"}) in Sortedlet
Names = #table(
{"Name"},
{{"zebra"}, {"Apple"}, {"banana"}, {"Cherry"}}
),
Sorted = Table.Sort(Names, {"Name"})
in
Sorted
| Name |
|--------|
| Apple |
| Cherry |
| banana |
| zebra |
ครับ ถ้าเป็นการเรียงแบบเดียวอันเดียวสามารถใช้ {“ColumnName”} หรือ {{“ColumnName”, Order.Ascending}} ถ้าเรียงหลายคอลัมน์ต้องเป็น {{“Col1”, Order.Asc}, {“Col2”, Order.Desc}} – เป็น list ของ list นั่นเอง
Order.Ascending = จากน้อยไปมาก (A-Z หรือ 0-9) / Order.Descending = จากมากไปน้อย (Z-A หรือ 9-0) – ส่วนใหญ่ใช้ Descending เมื่อต้องการเห็นรายการสำคัญก่อน เช่น ยอดขายสูงสุด
ไม่แน่นอน Power Query ไม่รับประกันลำดับแถวเดิม (stable sort) ถ้าสำคัญเก่ามากต้องเพิ่มคอลัมน์ index ก่อน เรียงตามค่าของอันแรก แล้วเรียงตามค่า index เป็นระดับสุดท้าย
ค่า null จะถูกเรียงไปไว้ที่ด้านหน้าเสมอ (ก่อนค่าปกติ) ถ้าต้องจัดการพิเศษต้องใช้ Table.SelectRows เพื่อแยกแถว null ออกก่อน แล้วต่อรวมกลับหลังจากเรียง
Table.Sort ใช้สำหรับเรียงลำดับแถวในตารางตามเกณฑ์ที่ระบุ เหมือนกับการใช้ Sort ใน Excel แต่ทำงานในระดับ Power Query ได้ตั้งแต่เริ่มต้น ฟังก์ชันนี้รองรับการเรียงแบบ ascending (จากน้อยไปมาก) หรือ descending (จากมากไปน้อย) และสามารถเรียงตามหลายคอลัมน์พร้อมกันได้ ซึ่งมีประโยชน์เมื่อต้องการจัดข้อมูลตามลำดับความสำคัญหรือเตรียมข้อมูลสำหรับการวิเคราะห์เพิ่มเติม