---
title: Text.Replace – แทนที่ข้อความใน Power Query
url: https://www.thepexcel.com/functions/power-query/text-functions/text-replace/
type: function-explainer
program: Power Query
syntax: "Text.Replace(text as nullable text, old as text, new as text) as nullable text"
date: 2025-12-03
updated: 2025-12-22
scores:
  popularity: 8
  difficulty: 3
  usefulness: 8
---

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

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

## คำอธิบาย

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

## Syntax

```excel
Text.Replace(text as nullable text, old as text, new as text) as nullable text
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| text | Yes | text |  | ข้อความต้นฉบับที่ต้องการค้นหาและแทนที่ |
| old | Yes | text |  | ข้อความย่อยที่ต้องการค้นหาและแทนที่ |
| new | Yes | text |  | ข้อความใหม่ที่จะแทนที่ |

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

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

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

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

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

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

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

## ตัวอย่าง

### 1. แทนที่ส่วนของคำ

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

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

ค้นหา "pp" ใน "apple" แล้วแทนที่ด้วย "rr" ได้ผลลัพธ์เป็น "arrle"

### 2. แทนที่หลายครั้งในข้อความเดียว

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

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

แทนที่ทุกตัวอักษร 'a' ด้วย 'o' ได้ 3 ครั้ง = 2 × 3 × 4 = bonono

### 3. ทำความสะอาดข้อมูลด้วย let...in

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

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

ลบเครื่องหมาย hyphen ออกจากรหัสสินค้า ทำให้ข้อมูลสะอาดขึ้น

### 4. แทนที่ URL encoding

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

**ผลลัพธ์:** `Hello World from Power Query`

แทนที่ %20 (URL space encoding) ด้วยช่องว่างจริง เพื่อให้ข้อมูลอ่านง่ายขึ้น

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

- Text.Replace แทนที่ทุกครั้งที่เจอในข้อความ ไม่ใช่แค่ครั้งแรก

- ลองใช้ Text.Replace ร่วมกับ Text.Trim เพื่อทำความสะอาดข้อมูล เช่น ลบ leading/trailing spaces

- สำหรับแทนที่ที่ไม่สนใจ Case ต้องทำการแปลง Text.Upper หรือ Text.Lower ก่อนใช้ Text.Replace

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

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

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

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

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

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

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

## ฟังก์ชันที่เกี่ยวข้อง

- [Text.Remove – ลบอักขระที่ไม่ต้องการ](https://www.thepexcel.com/functions/power-query/text-functions/text-remove/)
- [Text.Combine – รวมข้อความหลายรายการเป็นข้อความเดียว](https://www.thepexcel.com/functions/power-query/text-functions/text-combine/)
- [Text.ReplaceRange – แทนที่ข้อความในช่วงที่ระบุ](https://www.thepexcel.com/functions/power-query/text-functions/text-replacerange/)
- [Text.Upper – แปลงข้อความเป็นตัวอักษรใหญ่](https://www.thepexcel.com/functions/power-query/text-functions/text-upper/)

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

- [Microsoft Learn: Text.Replace](https://learn.microsoft.com/en-us/powerquery-m/text-replace) _(official)_
- [PowerQuery.how](https://powerquery.how/text-replace/) _(article)_

---

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