Thep Excel

Text.Replace – แทนที่ข้อความใน Power Query

Text.Replace ใช้สำหรับแทนที่ทุกส่วนของข้อความเก่าในข้อความหลักด้วยข้อความใหม่ เป็น Case Sensitive และแทนที่ทีละครั้งที่เจอในข้อความทั้งหมด

=Text.Replace(text as nullable text, old as text, new as text) as nullable text

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
8/10

Difficulty
3/10

Usefulness
8/10

Syntax & Arguments

=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 ข้อความใหม่ที่จะแทนที่

How it works

แก้ไขรหัสสินค้า

เปลี่ยนรหัสสินค้าจาก "PROD-" เป็น "ITEM-"

ทำความสะอาดข้อมูล

แทนที่คำผิด หรือสัญลักษณ์พิเศษ เช่น "#N/A" ด้วยค่าว่าง

สร้าง URL ที่ถูกต้อง

แทนที่ช่องว่างในชื่อไฟล์ด้วยเครื่องหมายขีดกลาง (-) สำหรับ URL

Examples

แทนที่ส่วนของคำ
Text.Replace("apple", "pp", "rr")
ค้นหา "pp" ใน "apple" แล้วแทนที่ด้วย "rr" ได้ผลลัพธ์เป็น "arrle"
Power Query Formula:

= Text.Replace("apple", "pp", "rr")

Result:

arrle

แทนที่หลายครั้งในข้อความเดียว
Text.Replace("banana", "a", "o")
แทนที่ทุกตัวอักษร 'a' ด้วย 'o' ได้ 3 ครั้ง = 2 × 3 × 4 = bonono
Power Query Formula:

= Text.Replace("banana", "a", "o")

Result:

bonono

ทำความสะอาดข้อมูลด้วย let…in
let ProductCode = "PROD-2024-001", Clean = Text.Replace(ProductCode, "-", "") in Clean
ลบเครื่องหมาย hyphen ออกจากรหัสสินค้า ทำให้ข้อมูลสะอาดขึ้น
Power Query Formula:

let
    ProductCode = "PROD-2024-001",
    Clean = Text.Replace(ProductCode, "-", "")
in
    Clean

Result:

PROD2024001

แทนที่ URL encoding
let Encoded = "Hello%20World%20from%20Power%20Query", Decoded = Text.Replace(Encoded, "%20", " ") in Decoded
แทนที่ %20 (URL space encoding) ด้วยช่องว่างจริง เพื่อให้ข้อมูลอ่านง่ายขึ้น
Power Query Formula:

let
    Encoded = "Hello%20World%20from%20Power%20Query",
    Decoded = Text.Replace(Encoded, "%20", " ")
in
    Decoded

Result:

Hello World from Power Query

FAQs

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

ใช่ครับ เป็น Case Sensitive สมบูรณ์ ตัวอย่างเช่น Text.Replace(“Apple”, “apple”, “Orange”) จะไม่แทนที่อะไรเลย ถ้าต้องการแทนที่ไม่สนใจ Case สามารถใช้ Text.Upper(text) หรือ Text.Lower(text) ทั้งคำค้นหาและข้อความเดิมก่อน

ถ้าต้องการแทนที่แค่ครั้งแรกที่พบทำอย่างไร?

Text.Replace จะแทนที่ทุกครั้งที่พบ ถ้าต้องแทนที่แค่ครั้งแรก ต้องใช้วิธีอื่น เช่น สร้าง custom formula ด้วย Text.RemoveRange ร่วมกับ Text.Insert และ Text.PositionOf เพื่อหาตำแหน่งแรกที่พบ

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

จะได้ null กลับมา ถ้าต้องการหลีกเลี่ยงสถานการณ์นี้ ตรวจสอบ null ก่อน เช่น if text null then Text.Replace(text, old, new) else “”

Resources & Related

Additional Notes

Text.Replace ทำหน้าที่ค้นหาข้อความย่อย (substring) ที่ระบุจากข้อความหลัก แล้วแทนที่ด้วยข้อความใหม่ทีละครั้งที่เจอในข้อความทั้งหมด

ส่วนตัวมักใช้ Text.Replace เพื่อเช็ดทำความสะอาด (clean up) ข้อมูลที่ไม่สอดคล้องกัน เช่น ลบ suffix, prefix หรือแทนที่เครื่องหมายพิเศษ 😎 สิ่งที่ต้องระวัง คือ Text.Replace เป็น Case Sensitive ถ้าต้องการแทนที่ไม่สนใจ Case ต้องใช้ Text.Upper หรือ Text.Lower ก่อน หรือใช้ฟังก์ชันที่มี Comparer.OrdinalIgnoreCase

Leave a Reply

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