ตัวอย่างที่ 1: แทนที่ส่วนของคำ
=Text.Replace("apple", "pp", "rr")
arrle
แทนที่ข้อความเก่าด้วยข้อความใหม่ทั้งหมด
Text.Replace ค้นหาทุกส่วนของข้อความเก่า (old) ที่พบในข้อความหลัก (text) และแทนที่ด้วยข้อความใหม่ (new) เป็น Case Sensitive หากต้องการแทนที่แบบ Case Insensitive ต้องใช้ Text.ReplaceAll ร่วมกับ Comparer.OrdinalIgnoreCase
=Text.Replace(text as nullable text, old as text, new as text) as nullable text
=Text.Replace(text as nullable text, old as text, new as text) as nullable text
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| text | text | Yes | ข้อความหลักที่ต้องการแก้ไข | |
| old | text | Yes | ข้อความย่อยที่ต้องการค้นหาและแทนที่ | |
| new | text | Yes | ข้อความใหม่ที่จะนำมาแทนที่ |
เปลี่ยนรหัสสินค้าจาก "PROD-" เป็น "ITEM-"
แทนที่คำผิด หรือสัญลักษณ์พิเศษ เช่น "#N/A" ด้วยค่าว่าง
แทนที่ช่องว่างในชื่อไฟล์ด้วยเครื่องหมายขีดกลาง (-) สำหรับ URL
=Text.Replace("apple", "pp", "rr")
arrle
=Text.Replace("Hello World", "World", "Power Query")
Hello Power Query
=Text.Replace("banana", "a", "o")
bonono
เป็น Case Sensitive ครับ เช่น Text.Replace(“Apple”, “apple”, “Orange”) จะไม่แทนที่ ถ้าต้องการแทนที่แบบไม่สนใจ Case ต้องใช้ Text.ReplaceAll ร่วมกับ Comparer.OrdinalIgnoreCase
Text.Replace จะแทนที่ทุกครั้งที่เจอครับ ถ้าต้องการแทนที่แค่ครั้งแรก ต้องใช้ฟังก์ชันที่ซับซ้อนขึ้น หรือใช้ Text.RemoveRange ร่วมกับ Text.Insert
ฟังก์ชัน Text.Replace ใน Power Query ใช้สำหรับแทนที่ข้อความย่อย (Substring) ที่พบในข้อความหลัก ด้วยข้อความใหม่ โดยจะแทนที่ทุกครั้งที่พบ