---
title: Table.ReplaceValue – แทนที่ค่าในตารางตามเงื่อนไข
url: https://www.thepexcel.com/functions/power-query/table-functions/table-replacevalue/
type: function-explainer
program: Power Query
syntax: "= Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 8
  difficulty: 3
  usefulness: 8
---

# Table.ReplaceValue – แทนที่ค่าในตารางตามเงื่อนไข

> Table.ReplaceValue ใช้สำหรับค้นหาและแทนที่ค่าในตารางได้หลายวิธี ตั้งแต่แทนที่ค่าทั้งหมดไปจนถึงแทนที่

## คำอธิบาย

Table.ReplaceValue ใช้สำหรับค้นหาและแทนที่ค่าในตารางได้หลายวิธี ตั้งแต่แทนที่ค่าทั้งหมดไปจนถึงแทนที่แบบมีเงื่อนไข

## Syntax

```excel
= Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางต้นฉบับที่ต้องการแทนที่ค่า |
| oldValue | Yes | any |  | ค่าเก่าที่ต้องการค้นหา สามารถเป็น text, number, หรือ each expression สำหรับ conditional logic |
| newValue | Yes | any |  | ค่าใหม่ที่ต้องการแทนที่ สามารถเป็น literal value หรือ each expression |
| replacer | Yes | function |  | ฟังก์ชันที่ใช้สำหรับการแทนที่: Replacer.ReplaceValue (exact match) หรือ Replacer.ReplaceText (partial match) หรือ custom function |
| columnsToSearch | Yes | list |  | รายการชื่อคอลัมน์ที่ต้องการค้นหาและแทนที่ เช่น {"Name", "Email"} |

## ตัวอย่าง

### 1. แทนที่ค่าทั้งหมด (Exact Match)

```excel
let
    Sales = Table.FromRecords({
        [Product = "Laptop", Status = "approved"],
        [Product = "Mouse", Status = "approved"],
        [Product = "Keyboard", Status = "pending"]
    }),
    Replaced = Table.ReplaceValue(Sales, "approved", "Confirmed", Replacer.ReplaceValue, {"Status"})
in
    Replaced
```

**ผลลัพธ์:** `ตารางที่มี Status = "approved" เปลี่ยนเป็น "Confirmed" ส่วน "pending" ยังคงเดิม`

Replacer.ReplaceValue ใช้สำหรับแทนที่ค่าทั้งหมด ในที่นี้ค้นหา "approved" ในคอลัมน์ Status เท่านั้น ถ้าเกิด typo เช่น "approve" (ขาด d) จะไม่ match เพราะต้องตรงกันทั้งหมด

### 2. แทนที่ข้อความ (Partial Match)

```excel
let
    Data = Table.FromRecords({
        [Name = "John_Smith"],
        [Name = "Jane_Doe"],
        [Name = "Bob_Johnson"]
    }),
    Replaced = Table.ReplaceValue(Data, "_", " ", Replacer.ReplaceText, {"Name"})
in
    Replaced
```

**ผลลัพธ์:** `ตารางที่มี underscore "_" ถูกเปลี่ยนเป็น space " ": "John Smith", "Jane Doe", "Bob Johnson"`

Replacer.ReplaceText ใช้สำหรับแทนที่ข้อความเพียงส่วนหนึ่ง (substring) ในตัวอย่างนี้ค้นหา underscore ในชื่อและแทนที่ด้วย space เหมาะสำหรับปรับฟอร์แมตข้อความ

### 3. แทนที่เฉพาะตามเงื่อนไข (Conditional with each)

```excel
let
    Orders = Table.FromRecords({
        [Customer = "Alice", Country = "US", Status = "New"],
        [Customer = "Bob", Country = "UK", Status = "New"],
        [Customer = "Charlie", Country = "US", Status = "New"]
    }),
    Replaced = Table.ReplaceValue(
        Orders,
        each if [Country] = "US" then [Status] else false,
        "US-New",
        Replacer.ReplaceValue,
        {"Status"}
    )
in
    Replaced
```

**ผลลัพธ์:** `เฉพาะแถว US มี Status เปลี่ยนจาก "New" เป็น "US-New" ส่วน UK ยังคงเป็น "New"`

ใช้ each expression ในพารามิเตอร์ oldValue เพื่อตั้งเงื่อนไข: "ถ้า Country = 'US' แล้วค้นหา Status เป็น 'New' ถ้าไม่ใช่ return false" ทำให้แทนที่เฉพาะแถวที่ตรงตามเงื่อนไข

### 4. สร้างค่าใหม่แบบ Dynamic (each expression ใน newValue)

```excel
let
    Names = Table.FromRecords({
        [OriginalName = "alice"],
        [OriginalName = "bob"],
        [OriginalName = "charlie"]
    }),
    Replaced = Table.ReplaceValue(
        Names,
        each [OriginalName],
        each Text.Proper([OriginalName]),
        Replacer.ReplaceValue,
        {"OriginalName"}
    )
in
    Replaced
```

**ผลลัพธ์:** `ทั้งหมดเปลี่ยนเป็น Title Case: "Alice", "Bob", "Charlie"`

ใช้ each expression ทั้งใน oldValue และ newValue เพื่อแปลงตัวอักษรของแต่ละค่า oldValue = [OriginalName] หมายถึง "ค้นหาค่าปัจจุบันของคอลัมน์นั้น" newValue = Text.Proper([OriginalName]) หมายถึง "แปลงเป็น Title Case" ทำให้แปลงค่าเดิมไปเป็นค่าใหม่แบบ dynamic

### 5. แทนที่ขึ้นต้น/ลงท้าย (Prefix/Suffix removal)

```excel
let
    Codes = Table.FromRecords({
        [ItemCode = "SKU-12345"],
        [ItemCode = "SKU-67890"],
        [ItemCode = "SKU-54321"]
    }),
    Replaced = Table.ReplaceValue(Codes, "SKU-", "", Replacer.ReplaceText, {"ItemCode"})
in
    Replaced
```

**ผลลัพธ์:** `ลบ prefix "SKU-" ออก: "12345", "67890", "54321"`

ใช้ Replacer.ReplaceText เพื่อลบ prefix ออกจากค่า แทนด้วยข้อความว่าง ("") เหมาะสำหรับสร้างความสะอาดให้กับข้อมูลที่มี prefix/suffix ที่ไม่ต้องการ

### 6. แทนที่หลายคอลัมน์พร้อมกัน

```excel
let
    Data = Table.FromRecords({
        [FirstName = "N/A", LastName = "N/A", Status = "Active"],
        [FirstName = "John", LastName = "Doe", Status = "Active"],
        [FirstName = "N/A", LastName = "Smith", Status = "Inactive"]
    }),
    Replaced = Table.ReplaceValue(Data, "N/A", null, Replacer.ReplaceValue, {"FirstName", "LastName"})
in
    Replaced
```

**ผลลัพธ์:** `ใน FirstName และ LastName ทั้งสองคอลัมน์ "N/A" ถูกแทนที่ด้วย null`

ระบุหลายชื่อคอลัมน์ใน columnsToSearch: {"FirstName", "LastName"} ทำให้ค้นหาและแทนที่ค่า "N/A" ในทั้งสองคอลัมน์พร้อมกัน ประหยัดการใช้ Table.ReplaceValue หลายครั้ง

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

- ใช้ Replacer.ReplaceValue สำหรับแทนที่ค่าเดี่ยว (exact match) และ Replacer.ReplaceText สำหรับเพิ่ม/ลบ prefix/suffix

- เมื่อต้องแทนที่หลายค่า ใช้ let...in ทำให้ code อ่านง่ายขึ้น แทนการใช้ Table.ReplaceValue ซ้อนกันหลายครั้ง

- ถ้าต้องแทนที่ conditional (เฉพาะแถวบางแถว) ใช้ each expression ใน oldValue เพื่อสร้าง condition

- ระวัง column name case-sensitive ชื่อคอลัมน์ต้องตรงกันทุกตัว "Status" กับ "status" ต่างกัน

- เมื่อแทนที่ค่า null หรือ empty string ทำให้ data cleaner - ช่วยหลีกเลี่ยง issues ลงไลน์ของ analysis

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

**Q: Replacer.ReplaceValue กับ Replacer.ReplaceText ต่างกันอย่างไร**

Replacer.ReplaceValue ใช้สำหรับแทนที่ค่าทั้งหมด (exact match) - ถ้าค้นหา 'goodbye' จะไม่ match 'goodbyes'. Replacer.ReplaceText ใช้สำหรับแทนที่ข้อความเพียงส่วนหนึ่ง (substring) - ถ้าค้นหา 'good' จะ match ทั้ง 'goodbye' และ 'goodbyes' แล้วแทนที่เพียงส่วนนั้น

**Q: แตกต่างจาก Table.TransformColumns ยังไง**

Table.ReplaceValue ใช้สำหรับค้นหาค่าเฉพาะและแทนที่ (targeted replacement) ส่วน Table.TransformColumns ใช้สำหรับแปลงทุกค่าในคอลัมน์ด้วยฟังก์ชัน (comprehensive transformation) ถ้าต้องการแปลงค่าทั้งหมดใช้ Table.TransformColumns ถ้าต้องการแทนที่เฉพาะบางค่าใช้ Table.ReplaceValue

**Q: แล้ว each expression ได้ใช้ในตรงไหนบ้าง**

ได้ใช้ใน 3 ที่: (1) oldValue = each [column] = value - สำหรับ conditional search, (2) newValue = each formula - สำหรับ dynamic replacement, (3) both - ใช้ทั้งสองพร้อมเพื่อสร้างความจำเพาะสูงสุด

**Q: ถ้าค่าไม่มีในคอลัมน์มันจะ error หรือ**

ไม่ error คำสั่ง Table.ReplaceValue จะทำงานปกติ เพียงแต่ไม่มีการแทนที่เพราะไม่พบค่าที่ค้นหา ตารางส่งกลับมาเหมือนเดิม

**Q: สามารถใช้ Table.ReplaceValue ซ้อนกันได้ไหม**

ได้ เช่น Table.ReplaceValue(Table.ReplaceValue(Source, "A", "B", ...), "C", "D", ...) แต่ถ้าแทนที่หลายค่า ลองใช้ let...in เพื่อให้อ่านง่าย หรือใช้ custom replacer function สำหรับตรรกะที่ซับซ้อน

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

- [Table.TransformColumns – แปลงคอลัมน์ด้วยฟังก์ชันการแปลง](https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumns/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)
- [Text.Replace – แทนที่ข้อความใน Power Query](https://www.thepexcel.com/functions/power-query/text-functions/text-replace/)
- [Table.AddColumn – เพิ่มคอลัมน์ใหม่ด้วย Calculated Values](https://www.thepexcel.com/functions/power-query/table-functions/table-addcolumn/)
- [Replacer.ReplaceText](https://www.thepexcel.com/functions/power-query/replacer-functions/replacer-replacetext/)
- [Replacer.ReplaceValue](https://www.thepexcel.com/functions/power-query/replacer-functions/replacer-replacevalue/)

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

- [Microsoft Learn - Table.ReplaceValue](https://learn.microsoft.com/en-us/powerquery-m/table-replacevalue) _(official)_
- [Microsoft Learn - Replacer.ReplaceValue](https://learn.microsoft.com/en-us/powerquery-m/replacer-replacevalue) _(official)_
- [Microsoft Learn - Replacer.ReplaceText](https://learn.microsoft.com/en-us/powerquery-m/replacer-replacetext) _(official)_
- [Power Query M Function Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

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