Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
7/10

Difficulty
3/10

Usefulness
7/10

Syntax & Arguments

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

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

How it works

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

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

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

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

Examples

ตัวอย่างที่ 1: ตรวจสอบการลงท้ายแบบพื้นฐาน
Text.EndsWith("apple", "ple")
ข้อความ "apple" ลงท้ายด้วย "ple" จึงคืนค่า true
Power Query Formula:

=Text.EndsWith("apple", "ple")

Result:

true

ตัวอย่างที่ 2: ไม่ลงท้ายด้วยข้อความที่กำหนด
Text.EndsWith("banana", "nan")
ข้อความ "banana" ไม่ได้ลงท้ายด้วย "nan" (ลงท้ายด้วย "ana") จึงคืนค่า false
Power Query Formula:

=Text.EndsWith("banana", "nan")

Result:

false

ตัวอย่างที่ 3: Case Sensitive (ค่าเริ่มต้น)
Text.EndsWith("Excel", "cel")
เนื่องจากค่าเริ่มต้นเป็น Case Sensitive "Excel" ลงท้ายด้วย "cel" (ตัวพิมพ์เล็ก) ไม่ตรง จึงคืนค่า false
Power Query Formula:

=Text.EndsWith("Excel", "cel")

Result:

false

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

=Text.EndsWith("Excel", "cel", Comparer.OrdinalIgnoreCase)

Result:

true

ตัวอย่างที่ 5: ใช้กรองไฟล์ตามนามสกุล
let Files = {"document.xlsx", "image.png", "backup.xlsx", "data.csv"}, FilteredExcel = List.Select(Files, each Text.EndsWith(_, ".xlsx")) in FilteredExcel
ใช้ List.Select ร่วมกับ Text.EndsWith เพื่อกรองเฉพาะไฟล์ที่มีนามสกุล .xlsx
Power Query Formula:

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

Result:

{"document.xlsx", "backup.xlsx"}

ตัวอย่างที่ 6: ใช้ในการกรองแถว Table สำหรับตรวจสอบ Email Domain
let Customers = Table.FromRows({{"john@company.com"}, {"jane@gmail.com"}, {"bob@company.com"}}, {"Email"}), CompanyEmails = Table.SelectRows(Customers, each Tex…
ใช้ Table.SelectRows ร่วมกับ Text.EndsWith เพื่อกรองเฉพาะ Email ที่ลงท้ายด้วย @company.com (ไม่สนใจ Case)
Power Query Formula:

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

Result:

Table with 2 rows (john@company.com, bob@company.com)

FAQs

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

ฟังก์ชัน Text.EndsWith ใน Power Query ใช้สำหรับตรวจสอบว่าข้อความหลัก ลงท้ายด้วยข้อความย่อยที่กำหนดหรือไม่ โดยคืนค่าเป็น logical (true/false)

เจ๋งของ Text.EndsWith คือสามารถใช้ร่วมกับ Comparer เพื่อควบคุมวิธีการเปรียบเทียบได้ เช่น Comparer.OrdinalIgnoreCase สำหรับการเปรียบเทียบที่ไม่สนใจ Case Sensitivity อันนี้ช่วยเยอะเวลาทำงานกับข้อมูลที่ผสมกันระหว่าง Uppercase กับ Lowercase

ส่วนตัวผมใช้ Text.EndsWith บ่อยครั้งสำหรับการกรองไฟล์ตามนามสกุล เช่น “filename.xlsx” จบด้วย “.xlsx” หรือกรองเบอร์โทรศัพท์ที่ลงท้ายด้วยหลัก specific 😎

Leave a Reply

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