---
title: Text.EndsWith – ตรวจสอบว่าข้อความลงท้ายด้วยข้อความย่อยหรือไม่
url: https://www.thepexcel.com/functions/power-query/text-functions/text-endswith/
type: function-explainer
program: Power Query
syntax: "Text.EndsWith(text as nullable text, substring as text, optional comparer as nullable function) as nullable logical"
date: 2025-12-03
updated: 2025-12-23
scores:
  popularity: 7
  difficulty: 3
  usefulness: 7
---

# Text.EndsWith – ตรวจสอบว่าข้อความลงท้ายด้วยข้อความย่อยหรือไม่

> Text.EndsWith ตรวจสอบว่าข้อความหลักลงท้ายด้วยข้อความย่อย (substring) ที่กำหนดหรือไม่ คืนค่า true หาก

## คำอธิบาย

Text.EndsWith ตรวจสอบว่าข้อความหลักลงท้ายด้วยข้อความย่อย (substring) ที่กำหนดหรือไม่ คืนค่า true หากตรงกัน และ false หากไม่ตรงกัน สามารถใช้ Comparer เพื่อควบคุม Case Sensitivity

## Syntax

```excel
Text.EndsWith(text as nullable text, substring as text, optional comparer as nullable function) as nullable logical
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| text | Yes | text (nullable) |  | ข้อความหลักที่ต้องการตรวจสอบว่าลงท้ายด้วยข้อความย่อยหรือไม่ |
| substring | Yes | text |  | ข้อความย่อยที่ต้องการตรวจสอบการลงท้าย (ต้องไม่เป็น null) |
| comparer | No | function | Comparer.Ordinal (default case-sensitive) | ฟังก์ชัน Comparer สำหรับการเปรียบเทียบ เช่น Comparer.Ordinal หรือ Comparer.OrdinalIgnoreCase |

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

### กรองข้อมูลตามนามสกุลไฟล์

เลือกเฉพาะไฟล์ที่ลงท้ายด้วย ".xlsx" หรือ ".csv"

### จัดหมวดหมู่ข้อมูล

ถ้าข้อความในคอลัมน์ลงท้ายด้วย "_Final" ให้ถือว่าเป็นรายงานฉบับสมบูรณ์

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ตรวจสอบการลงท้ายแบบพื้นฐาน

```excel
Text.EndsWith("apple", "ple")
```

**ผลลัพธ์:** `true`

ข้อความ "apple" ลงท้ายด้วย "ple" จึงคืนค่า true

### 2. ตัวอย่างที่ 2: ไม่ลงท้ายด้วยข้อความที่กำหนด

```excel
Text.EndsWith("banana", "nan")
```

**ผลลัพธ์:** `false`

ข้อความ "banana" ไม่ได้ลงท้ายด้วย "nan" (ลงท้ายด้วย "ana") จึงคืนค่า false

### 3. ตัวอย่างที่ 3: Case Sensitive (ค่าเริ่มต้น)

```excel
Text.EndsWith("Excel", "cel")
```

**ผลลัพธ์:** `false`

เนื่องจากค่าเริ่มต้นเป็น Case Sensitive "Excel" ลงท้ายด้วย "cel" (ตัวพิมพ์เล็ก) ไม่ตรง จึงคืนค่า false

### 4. ตัวอย่างที่ 4: ใช้ Comparer.OrdinalIgnoreCase สำหรับการเปรียบเทียบไม่สนใจ Case

```excel
Text.EndsWith("Excel", "cel", Comparer.OrdinalIgnoreCase)
```

**ผลลัพธ์:** `true`

Comparer.OrdinalIgnoreCase ทำให้การเปรียบเทียบไม่สนใจตัวพิมพ์เล็ก/ใหญ่ ดังนั้น "Excel" ลงท้ายด้วย "cel" (ตัวพิมพ์เล็ก) ก็ถือว่าตรง

### 5. ตัวอย่างที่ 5: ใช้กรองไฟล์ตามนามสกุล

```excel
let
    Files = {"document.xlsx", "image.png", "backup.xlsx", "data.csv"},
    FilteredExcel = List.Select(Files, each Text.EndsWith(_, ".xlsx"))
in
    FilteredExcel
```

**ผลลัพธ์:** `{"document.xlsx", "backup.xlsx"}`

ใช้ List.Select ร่วมกับ Text.EndsWith เพื่อกรองเฉพาะไฟล์ที่มีนามสกุล .xlsx

### 6. ตัวอย่างที่ 6: ใช้ในการกรองแถว Table สำหรับตรวจสอบ Email Domain

```excel
let
    Customers = Table.FromRows({{"john@company.com"}, {"jane@gmail.com"}, {"bob@company.com"}}, {"Email"}),
    CompanyEmails = Table.SelectRows(Customers, each Text.EndsWith([Email], "@company.com", Comparer.OrdinalIgnoreCase))
in
    CompanyEmails
```

**ผลลัพธ์:** `Table with 2 rows (john@company.com, bob@company.com)`

ใช้ Table.SelectRows ร่วมกับ Text.EndsWith เพื่อกรองเฉพาะ Email ที่ลงท้ายด้วย @company.com (ไม่สนใจ Case)

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

- ใช้ Text.EndsWith ร่วมกับ List.Select หรือ Table.SelectRows เพื่อกรองข้อมูล

- เมื่อทำงานกับ File Extensions ให้ใช้ Comparer.OrdinalIgnoreCase เพื่อไม่สนใจตัวพิมพ์

- ถ้าต้องตรวจสอบหลายเงื่อนไขในเวลาเดียวกัน ให้ใช้ Text.EndsWith ร่วมกับ Text.StartsWith หรือ Text.Contains

- สำหรับการตรวจสอบ Email Domain ให้ใช้ Comparer.OrdinalIgnoreCase เพื่อรองรับ "@Company.COM" และ "@company.com" เป็นเดียวกัน

- ใช้ ?? operator ร่วมกับ Text.EndsWith เพื่อป้องกัน null error เช่น Text.EndsWith(value ?? "", "@domain.com")

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

**Q: Text.EndsWith เป็น Case Sensitive หรือไม่?**

เป็น Case Sensitive ครับ เช่น Text.EndsWith("Excel", "cel") จะได้ false เพราะ "cel" (ตัวพิมพ์เล็ก) ไม่ตรงกับ "cel" ใน "Excel" ถ้าต้องการไม่สนใจ Case ต้องใช้ Comparer.OrdinalIgnoreCase

**Q: ถ้า text parameter เป็น null จะเกิดอะไร?**

ฟังก์ชนจะคืนค่า null เพราะ text parameter เป็น nullable ถ้าต้องการหลีกเลี่ยง null ให้ใช้ ?? operator เช่น Text.EndsWith(Email ?? "", "@company.com")

**Q: ส่วนต่างระหว่าง Text.EndsWith กับ Text.StartsWith คืออะไร?**

Text.EndsWith ตรวจสอบการลงท้าย ส่วน Text.StartsWith ตรวจสอบการเริ่มต้น เช่น Text.EndsWith("hello.txt", ".txt") vs Text.StartsWith("hello.txt", "hello")

**Q: สามารถใช้ Text.EndsWith กับ Pattern Matching แบบ Regex ได้ไหม?**

ไม่ได้ Text.EndsWith เป็นการตรวจสอบแบบทั่วไปเท่านั้น ถ้าต้องใช้ Regex ให้ใช้ Text.RegexTest() หรือ Text.RegexSplit() แทน

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

- [Microsoft Learn: Text.EndsWith](https://learn.microsoft.com/en-us/powerquery-m/text-endswith) _(official)_
- [PowerQuery.how](https://powerquery.how/) _(article)_
- [Text Functions in Power Query](https://learn.microsoft.com/en-us/powerquery-m/text-functions) _(official)_

---

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