---
title: Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query
url: https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/
type: function-explainer
program: Power Query
syntax: "Table.SelectRows(table as table, condition as function) as table"
date: 2025-12-18
scores:
  popularity: 10
  difficulty: 3
  usefulness: 10
---

# Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query

> กรองตารางเพื่อเก็บเฉพาะแถวที่ตรงตามเงื่อนไขที่กำหนด

## คำอธิบาย

Table.SelectRows เป็นฟังก์ชันสำหรับกรองตาราง (filter table) ใน Power Query โดยใช้ฟังก์ชันเงื่อนไข (condition function) เพื่อตรวจสอบแต่ละแถว ถ้าเงื่อนไขคืนค่า true จะเก็บแถวนั้นไว้ ถ้าเป็น false จะตัดทิ้ง สามารถใช้ keyword 'each' ร่วมกับการอ้างอิงคอลัมน์ด้วย [ColumnName] เพื่อเขียนเงื่อนไขได้สะดวก รองรับการกรองตามตัวเลข ข้อความ วันที่ การตรวจสอบค่า null และเงื่อนไขที่ซับซ้อนด้วย and/or operators ฟังก์ชันนี้รองรับ Query Folding ซึ่งช่วยเพิ่มประสิทธิภาพเมื่อทำงานกับ data source ขนาดใหญ่

## Syntax

```excel
Table.SelectRows(table as table, condition as function) as table
```

**Variant**

```excel
Table.SelectRows(table, each [Column] > value)
```

รูปแบบพื้นฐาน - กรองด้วยเงื่อนไขเปรียบเทียบค่าคอลัมน์เดียว

**Variant**

```excel
Table.SelectRows(table, each [Col1] > x and [Col2] = y)
```

รูปแบบหลายเงื่อนไข - ใช้ and/or เชื่อมเงื่อนไขหลายตัว

**Variant**

```excel
Table.SelectRows(table, each Text.Contains([Column], "search"))
```

รูปแบบขั้นสูง - ใช้ร่วมกับฟังก์ชัน M อื่นๆ เช่น Text, Date functions

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางข้อมูลต้นฉบับ (source table) ที่ต้องการกรอง ต้องเป็นข้อมูลประเภท table type ใน Power Query |
| condition | Yes | function |  | ฟังก์ชันเงื่อนไข (condition function) ที่รับแถวหนึ่งแถวเป็น input และคืนค่า true (เก็บแถวนี้) หรือ false (ตัดแถวนี้ทิ้ง) มักเขียนในรูปแบบ 'each [ColumnName] comparison value' โดย each เป็น shorthand สำหรับ (_) => _ และ [ColumnName] คือการอ้างอิงคอลัมน์ในแถวปัจจุบัน |

## เคสการใช้งาน

### กรองยอดขายที่สูงกว่าเกณฑ์

เลือกเฉพาะรายการสินค้าหรือใบสั่งซื้อที่มียอดขายมากกว่าค่าที่กำหนด เช่น มากกว่า 10,000 บาท เพื่อวิเคราะห์ลูกค้า VIP หรือสินค้าขายดี

_เหมาะกับ:_ data-analysis

### ตัดข้อมูลที่ไม่สมบูรณ์ออก (Data Cleaning)

กรองออกแถวที่มีคอลัมน์สำคัญเป็นค่าว่าง (null) หรือค่าผิดปกติ เช่น Email ว่าง, วันที่ไม่ถูกต้อง, หมายเลขโทรศัพท์ไม่ครบ เพื่อให้ข้อมูลสะอาดก่อนนำไปวิเคราะห์

_เหมาะกับ:_ data-cleaning

### เลือกช่วงเวลาหรือช่วงวันที่

กรองข้อมูลตามช่วงเวลาที่ต้องการ เช่น เก็บเฉพาะรายการในปีปัจจุบัน, 30 วันล่าสุด, ไตรมาสที่ 1, หรือช่วงวันที่ที่กำหนด เพื่อวิเคราะห์ข้อมูลในช่วงเวลาที่เฉพาะเจาะจง

_เหมาะกับ:_ time-series-analysis

### กรองตามสถานะหรือหมวดหมู่

เลือกเฉพาะแถวที่มีสถานะหรือหมวดหมู่ที่ต้องการ เช่น สถานะ "Complete", "Shipped", หมวดหมู่ "Electronics", "Food" หรือภูมิภาค "Asia" เพื่อแบ่งกลุ่มข้อมูลตามเกณฑ์ธุรกิจ

_เหมาะกับ:_ data-segmentation

### กรองข้อมูลด้วยการค้นหาข้อความ

ใช้ร่วมกับ Text functions เช่น Text.Contains, Text.StartsWith เพื่อค้นหาแถวที่มีข้อความบางส่วนตรงกัน เช่น ชื่อลูกค้าที่มีคำว่า "Company", รหัสสินค้าที่ขึ้นต้นด้วย "PRD-"

_เหมาะกับ:_ text-search

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: กรองตามค่าตัวเลขพื้นฐาน

```excel
let
    Sales = Table.FromRecords({
        [OrderID = 1, CustomerName = "Alice", Amount = 500],
        [OrderID = 2, CustomerName = "Bob", Amount = 1500],
        [OrderID = 3, CustomerName = "Charlie", Amount = 800],
        [OrderID = 4, CustomerName = "Diana", Amount = 2200]
    }),
    HighValueOrders = Table.SelectRows(Sales, each [Amount] > 1000)
in
    HighValueOrders
```

**ผลลัพธ์:** `Table with 2 rows:
[OrderID=2, CustomerName="Bob", Amount=1500]
[OrderID=4, CustomerName="Diana", Amount=2200]`

ตัวอย่างนี้สร้างตาราง Sales ด้วย Table.FromRecords แล้วใช้ Table.SelectRows กรองเอาเฉพาะแถวที่ Amount มากกว่า 1000
.
โดยใช้เงื่อนไข each [Amount] > 1000 ซึ่ง 'each' เป็น keyword ที่ใช้วนลูปตรวจสอบทุกแถว และ [Amount] คือการอ้างอิงคอลัมน์ Amount ในแถวปัจจุบัน
.
ผลลัพธ์จะได้ table ที่เหลือเพียง 2 แถวที่มียอดขายเกิน 1000 (Bob และ Diana) ส่วน Alice และ Charlie ถูกตัดออกเพราะยอดขายต่ำกว่าเกณฑ์ 😎

### 2. ตัวอย่างที่ 2: กรองข้อความด้วย Text functions (Case Sensitive)

```excel
let
    Products = Table.FromRecords({
        [ProductID = "P001", Category = "Electronics", Price = 15000],
        [ProductID = "P002", Category = "Food", Price = 200],
        [ProductID = "P003", Category = "Electronics", Price = 8000],
        [ProductID = "P004", Category = "Clothing", Price = 1200]
    }),
    ElectronicsOnly = Table.SelectRows(Products, each [Category] = "Electronics")
in
    ElectronicsOnly
```

**ผลลัพธ์:** `Table with 2 rows:
[ProductID="P001", Category="Electronics", Price=15000]
[ProductID="P003", Category="Electronics", Price=8000]`

ตัวอย่างนี้แสดงการกรองด้วยการเปรียบเทียบข้อความโดยตรง โดยใช้เงื่อนไข each [Category] = "Electronics" เพื่อเลือกเฉพาะสินค้าในหมวด Electronics
.
ที่ต้องระวังคือ Power Query เป็น case-sensitive นะครับ ดังนั้น "Electronics" จะไม่เท่ากับ "electronics" หรือ "ELECTRONICS" ถ้าต้องการกรองแบบไม่สนตัวพิมพ์เล็ก-ใหญ่ ให้ใช้ Text.Lower([Category]) = "electronics" แทน
.
ผลลัพธ์จะได้เฉพาะสินค้า 2 รายการที่เป็น Electronics ส่วน Food และ Clothing ถูกกรองออก

### 3. ตัวอย่างที่ 3: หลายเงื่อนไขด้วย AND/OR และการจัดการ null

```excel
let
    Orders = Table.FromRecords({
        [OrderID = 101, Status = "Complete", Amount = 1200, Email = "alice@example.com"],
        [OrderID = 102, Status = "Pending", Amount = 800, Email = null],
        [OrderID = 103, Status = "Complete", Amount = 500, Email = "charlie@example.com"],
        [OrderID = 104, Status = "Complete", Amount = 1500, Email = "diana@example.com"],
        [OrderID = 105, Status = "Cancelled", Amount = 2000, Email = "eve@example.com"]
    }),
    ValidHighValueOrders = Table.SelectRows(
        Orders,
        each [Status] = "Complete" and [Amount] > 1000 and [Email] <> null
    )
in
    ValidHighValueOrders
```

**ผลลัพธ์:** `Table with 1 row:
[OrderID=104, Status="Complete", Amount=1500, Email="diana@example.com"]`

ตัวอย่างนี้แสดงการใช้หลายเงื่อนไขพร้อมกัน โดยใช้ operator 'and' เชื่อมเงื่อนไข 3 ตัว: (1) Status ต้องเป็น "Complete" (2) Amount ต้องมากกว่า 1000 และ (3) Email ต้องไม่เป็น null โดยใช้ [Email]  null เพื่อกรองค่าว่างออก
.
ผลลัพธ์จะได้เฉพาะแถวที่ตรงเงื่อนไขทั้ง 3 ตัว (OrderID 104) ส่วนแถวอื่นถูกตัดออกเพราะ: OrderID 101 ยอดต่ำกว่า 1000, OrderID 102 เป็น Pending และ Email เป็น null, OrderID 103 ยอดต่ำกว่า 1000, OrderID 105 เป็น Cancelled
.
ตัวอย่างนี้แสดงให้เห็นความสำคัญของการจัดการ null ใน Power Query ครับ เพราะถ้าไม่ใส่เงื่อนไข [Email]  null อาจได้ผลลัพธ์ไม่ตรงตามที่ต้องการ 💡

### 4. ตัวอย่างที่ 4: กรองขั้นสูงด้วย Text functions และ Date functions

```excel
let
    Transactions = Table.FromRecords({
        [TransID = "TXN-2024-001", CustomerName = "ABC Company Ltd.", Date = #date(2024, 3, 15), Amount = 5000],
        [TransID = "TXN-2024-002", CustomerName = "John Doe", Date = #date(2024, 6, 20), Amount = 1200],
        [TransID = "TXN-2024-003", CustomerName = "XYZ Corporation", Date = #date(2024, 9, 10), Amount = 8500],
        [TransID = "TXN-2023-999", CustomerName = "Smith & Co.", Date = #date(2023, 12, 5), Amount = 3000]
    }),
    FilteredTransactions = Table.SelectRows(
        Transactions,
        each Text.Contains([CustomerName], "Company") or Text.Contains([CustomerName], "Corporation")
             and Date.Year([Date]) = 2024
             and [Amount] >= 5000
    )
in
    FilteredTransactions
```

**ผลลัพธ์:** `Table with 2 rows:
[TransID="TXN-2024-001", CustomerName="ABC Company Ltd.", Date=#date(2024,3,15), Amount=5000]
[TransID="TXN-2024-003", CustomerName="XYZ Corporation", Date=#date(2024,9,10), Amount=8500]`

ตัวอย่างนี้แสดงการใช้ Table.SelectRows ร่วมกับฟังก์ชัน M อื่นๆ แบบขั้นสูง โดยใช้ Text.Contains() เพื่อค้นหาข้อความบางส่วนในชื่อลูกค้า (ชื่อที่มีคำว่า "Company" หรือ "Corporation") ใช้ Date.Year() เพื่อดึงปีจากวันที่และเปรียบเทียบว่าเป็นปี 2024 และเงื่อนไขยอดเงินต้องมากกว่าหรือเท่ากับ 5000
.
ที่เจ๋งคือการใช้ or และ and ร่วมกันทำให้สร้างเงื่อนไขที่ซับซ้อนได้ โดย Power Query จะประเมินเงื่อนไข and ก่อน or ตามหลัก operator precedence
.
ผลลัพธ์จะได้ธุรกรรมของบริษัท (corporate customers) ที่เกิดในปี 2024 และมียอดเงินตั้งแต่ 5000 ขึ้นไป ตัวอย่างนี้แสดงให้เห็นว่า Table.SelectRows สามารถทำงานร่วมกับฟังก์ชันจากหลาย category (Text, Date, Logical) เพื่อสร้างเงื่อนไขกรองที่มีความซับซ้อนและตอบโจทย์ธุรกิจจริงครับ 😎

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

💡 **Tip**: Table.SelectRows เป็นหนึ่งในฟังก์ชันที่ใช้บ่อยที่สุดใน Power Query และเป็นรากฐานสำคัญของการทำ data transformation โดยรองรับ Query Folding ซึ่งหมายความว่าเมื่อใช้กับ data source ที่รองรับ (เช่น SQL database) Power Query จะแปลง M code เป็น SQL WHERE clause และส่งไปให้ database ทำการกรองโดยตรง ทำให้ไม่ต้องโหลดข้อมูลทั้งหมดมาที่ client ช่วยประหยัด memory และเพิ่มความเร็วอย่างมาก
.
แต่หากใช้ custom functions หรือ logic ที่ซับซ้อนเกินไป Query Folding อาจใช้งานไม่ได้ ควรตรวจสอบโดยคลิกขวาที่ step แล้วเลือก View Native Query เพื่อดูว่า query ถูกแปลงเป็น SQL หรือไม่
.
ส่วนตัวผมแนะนำให้ระวังเรื่อง null handling ด้วยนะครับ เพราะใน M language การเปรียบเทียบกับ null จะให้ผลลัพธ์เป็น null (ไม่ใช่ true หรือ false) ดังนั้นควรใช้  null หรือ = null อย่างชัดเจนในเงื่อนไข ไม่งั้นอาจได้ผลลัพธ์ที่ไม่คาดคิดได้ 😅

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

**Q: ทำไมกรองข้อความแล้วหาไม่เจอ แม้ว่าข้อความจะมีอยู่จริง?**

เจอปัญหานี้บ่อยมากเลยครับ 😅 สาเหตุหลักคือ Power Query เป็น case-sensitive สำหรับการเปรียบเทียบข้อความ ดังนั้น "Apple" ไม่เท่ากับ "apple" หรือ "APPLE"
.
ถ้าต้องการกรองแบบไม่สนใจตัวพิมพ์เล็ก-ใหญ่ ให้แปลงทั้งสองฝ่ายเป็นตัวพิมพ์เล็กก่อนเปรียบเทียบ เช่น each Text.Lower([Category]) = "electronics" หรือใช้ Text.Contains([Name], "search", Comparer.OrdinalIgnoreCase) สำหรับการค้นหาที่ไม่สนใจ case

**Q: จะกรองเพื่อเอาเฉพาะแถวที่มีค่า null หรือไม่มีค่า null ทำอย่างไร?**

ใช้เงื่อนไข each [Column]  null เพื่อกรองค่าว่าง (null) ออก หรือใช้ each [Column] = null เพื่อเลือกเฉพาะแถวที่เป็นค่าว่าง
.
สามารถใช้ร่วมกับเงื่อนไขอื่นๆ ได้ด้วย and/or เช่น each [Email]  null and [Phone]  null เพื่อเลือกเฉพาะแถวที่มีข้อมูลครบทั้ง Email และ Phone

**Q: Table.SelectRows รองรับ Query Folding หรือไม่?**

ใช่ครับ Table.SelectRows รองรับ Query Folding เมื่อใช้กับ data source ที่รองรับ (เช่น SQL Server, Oracle) และเงื่อนไขที่ใช้เป็นแบบที่สามารถแปลงเป็น SQL WHERE clause ได้
.
ซึ่งจะช่วยเพิ่มประสิทธิภาพอย่างมากเพราะการกรองจะทำที่ database โดยตรง ไม่ต้องโหลดข้อมูลทั้งหมดมาที่ Power Query ก่อน 😎
.
แต่ถ้าใช้ custom functions หรือ M functions ที่ซับซ้อนเกินไป อาจทำให้ Query Folding ไม่สามารถทำงานได้ ตรวจสอบได้โดยคลิกขวาที่ step แล้วดู View Native Query

**Q: ต่างจาก Table.RemoveRows อย่างไร?**

Table.SelectRows ใช้สำหรับกรองโดยระบุเงื่อนไขว่าแถวไหนที่ต้องการเก็บไว้ (positive selection) ส่วน Table.RemoveRows มีหลายรูปแบบ เช่น Table.RemoveRows(table, count) สำหรับลบแถวแรก n แถว หรือ Table.RemoveRows(table, each [condition]) สำหรับลบแถวที่ตรงเงื่อนไข (negative selection)
.
การเลือกใช้ขึ้นอยู่กับว่าเราต้องการคิดในแบบ "เก็บแถวที่ต้องการ" หรือ "ลบแถวที่ไม่ต้องการ" ตามความเหมาะสมของแต่ละสถานการณ์ครับ

**Q: สามารถใช้ตัวแปรภายนอกในเงื่อนไข each ได้หรือไม่?**

ได้ครับ สามารถอ้างอิงตัวแปรที่ประกาศไว้ใน let statement ก่อนหน้าได้ เช่น let Threshold = 1000, FilteredTable = Table.SelectRows(Source, each [Amount] > Threshold) in FilteredTable จะใช้ค่า Threshold ที่เป็น 1000 ในการกรอง
.
วิธีนี้ช่วยให้เปลี่ยนค่าเกณฑ์ได้ง่าย โดยไม่ต้องแก้หลายจุด และทำให้โค้ดอ่านง่ายขึ้นด้วย 💡

**Q: ทำไมควรใช้ Table.SelectRows แทนการใช้ Filter UI ใน Power Query Editor?**

การใช้ Table.SelectRows (เขียนโค้ด M) มีข้อดีคือ: (1) ควบคุมเงื่อนไขที่ซับซ้อนได้แม่นยำกว่า (2) สามารถใช้ตัวแปร parameters ทำให้ยืดหยุ่น (3) อ่านและบำรุงรักษาโค้ดได้ง่ายกว่าเมื่อมีหลายขั้นตอน (4) สามารถ reuse และ copy ไปใช้ใน query อื่นได้ง่าย
.
แต่สำหรับเงื่อนไขง่ายๆ การใช้ Filter UI ก็เพียงพอและสะดวกกว่านะครับ ขึ้นอยู่กับความซับซ้อนของงานและประสบการณ์ของผู้ใช้

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.RemoveRows – ลบแถวตามจำนวนและตำแหน่งที่ระบุ](https://www.thepexcel.com/functions/power-query/table-functions/table-removerows/)
- [Table.Distinct – ลบแถวที่ซ้ำกันออกจากตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-distinct/)
- [Table.Group – จัดกลุ่มและสรุปผลข้อมูล (Group By)](https://www.thepexcel.com/functions/power-query/table-functions/table-group/)
- [List.Select – เลือกสมาชิกจาก List ตามเงื่อนไข](https://www.thepexcel.com/functions/power-query/list-functions/list-select/)
- [Table.AddColumn – เพิ่มคอลัมน์ใหม่ด้วย Calculated Values](https://www.thepexcel.com/functions/power-query/table-functions/table-addcolumn/)
- [Table.SelectColumns – เลือกคอลัมน์ที่ต้องการจากตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-selectcolumns/)
- [Text.Contains – ตรวจสอบว่าข้อความมีส่วนประกอบที่ต้องการหรือไม่](https://www.thepexcel.com/functions/power-query/text-functions/text-contains/)
- [Text.Lower – แปลงข้อความเป็นตัวพิมพ์เล็ก](https://www.thepexcel.com/functions/power-query/text-functions/text-lower/)

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

- [Microsoft Learn: Table.SelectRows official documentation](https://learn.microsoft.com/en-us/powerquery-m/table-selectrows) _(documentation)_
- [Microsoft Learn: Understanding Power Query M formula language](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-language-specification) _(documentation)_
- [PowerQuery.how: Table.SelectRows guide and examples](https://powerquery.how/table-selectrows/) _(guide)_
- [Microsoft Learn: Query Folding in Power Query](https://learn.microsoft.com/en-us/power-query/power-query-folding) _(documentation)_

---

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