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
=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 |
เลือกเฉพาะไฟล์ที่ลงท้ายด้วย ".xlsx" หรือ ".csv"
ถ้าข้อความในคอลัมน์ลงท้ายด้วย "_Final" ให้ถือว่าเป็นรายงานฉบับสมบูรณ์
Text.EndsWith("apple", "ple")=Text.EndsWith("apple", "ple")
true
Text.EndsWith("banana", "nan")=Text.EndsWith("banana", "nan")
false
Text.EndsWith("Excel", "cel")=Text.EndsWith("Excel", "cel")
false
Text.EndsWith("Excel", "cel", Comparer.OrdinalIgnoreCase)=Text.EndsWith("Excel", "cel", Comparer.OrdinalIgnoreCase)
true
let Files = {"document.xlsx", "image.png", "backup.xlsx", "data.csv"}, FilteredExcel = List.Select(Files, each Text.EndsWith(_, ".xlsx")) in FilteredExcellet
Files = {"document.xlsx", "image.png", "backup.xlsx", "data.csv"},
FilteredExcel = List.Select(Files, each Text.EndsWith(_, ".xlsx"))
in
FilteredExcel
{"document.xlsx", "backup.xlsx"}
let Customers = Table.FromRows({{"john@company.com"}, {"jane@gmail.com"}, {"bob@company.com"}}, {"Email"}), CompanyEmails = Table.SelectRows(Customers, each Tex…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)
เป็น Case Sensitive ครับ เช่น Text.EndsWith(“Excel”, “cel”) จะได้ false เพราะ “cel” (ตัวพิมพ์เล็ก) ไม่ตรงกับ “cel” ใน “Excel” ถ้าต้องการไม่สนใจ Case ต้องใช้ Comparer.OrdinalIgnoreCase
ฟังก์ชนจะคืนค่า null เพราะ text parameter เป็น nullable ถ้าต้องการหลีกเลี่ยง null ให้ใช้ ?? operator เช่น Text.EndsWith(Email ?? “”, “@company.com”)
Text.EndsWith ตรวจสอบการลงท้าย ส่วน Text.StartsWith ตรวจสอบการเริ่มต้น เช่น Text.EndsWith(“hello.txt”, “.txt”) vs Text.StartsWith(“hello.txt”, “hello”)
ไม่ได้ Text.EndsWith เป็นการตรวจสอบแบบทั่วไปเท่านั้น ถ้าต้องใช้ Regex ให้ใช้ Text.RegexTest() หรือ Text.RegexSplit() แทน
ฟังก์ชัน Text.EndsWith ใน Power Query ใช้สำหรับตรวจสอบว่าข้อความหลัก ลงท้ายด้วยข้อความย่อยที่กำหนดหรือไม่ โดยคืนค่าเป็น logical (true/false)
เจ๋งของ Text.EndsWith คือสามารถใช้ร่วมกับ Comparer เพื่อควบคุมวิธีการเปรียบเทียบได้ เช่น Comparer.OrdinalIgnoreCase สำหรับการเปรียบเทียบที่ไม่สนใจ Case Sensitivity อันนี้ช่วยเยอะเวลาทำงานกับข้อมูลที่ผสมกันระหว่าง Uppercase กับ Lowercase
ส่วนตัวผมใช้ Text.EndsWith บ่อยครั้งสำหรับการกรองไฟล์ตามนามสกุล เช่น “filename.xlsx” จบด้วย “.xlsx” หรือกรองเบอร์โทรศัพท์ที่ลงท้ายด้วยหลัก specific 😎