Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
8/10

Difficulty
3/10

Usefulness
8/10

Syntax & Arguments

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

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

Examples

แทนที่ค่าทั้งหมด (Exact Match)
let Sales = Table.FromRecords({ [Product = "Laptop", Status = "approved"], [Product = "Mouse", Status = "approved"], [Product = "Keyboard", Status = "pending"]…
Replacer.ReplaceValue ใช้สำหรับแทนที่ค่าทั้งหมด ในที่นี้ค้นหา "approved" ในคอลัมน์ Status เท่านั้น ถ้าเกิด typo เช่น "approve" (ขาด d) จะไม่ match เพราะต้องตรงกันทั้งหมด
Power Query Formula:

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

Result:

ตารางที่มี Status = "approved" เปลี่ยนเป็น "Confirmed" ส่วน "pending" ยังคงเดิม

แทนที่ข้อความ (Partial Match)
let Data = Table.FromRecords({ [Name = "John_Smith"], [Name = "Jane_Doe"], [Name = "Bob_Johnson"] }), Replaced = Table.ReplaceValue(Data, "_", " ", Replacer.Rep…
Replacer.ReplaceText ใช้สำหรับแทนที่ข้อความเพียงส่วนหนึ่ง (substring) ในตัวอย่างนี้ค้นหา underscore ในชื่อและแทนที่ด้วย space เหมาะสำหรับปรับฟอร์แมตข้อความ
Power Query Formula:

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

Result:

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

แทนที่เฉพาะตามเงื่อนไข (Conditional with each)
let Orders = Table.FromRecords({ [Customer = "Alice", Country = "US", Status = "New"], [Customer = "Bob", Country = "UK", Status = "New"], [Customer = "Charlie"…
ใช้ each expression ในพารามิเตอร์ oldValue เพื่อตั้งเงื่อนไข: "ถ้า Country = 'US' แล้วค้นหา Status เป็น 'New' ถ้าไม่ใช่ return false" ทำให้แทนที่เฉพาะแถวที่ตรงตามเงื่อนไข
Power Query Formula:

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

Result:

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

สร้างค่าใหม่แบบ Dynamic (each expression ใน newValue)
let Names = Table.FromRecords({ [OriginalName = "alice"], [OriginalName = "bob"], [OriginalName = "charlie"] }), Replaced = Table.ReplaceValue( Names, each [Ori…
ใช้ each expression ทั้งใน oldValue และ newValue เพื่อแปลงตัวอักษรของแต่ละค่า oldValue = [OriginalName] หมายถึง "ค้นหาค่าปัจจุบันของคอลัมน์นั้น" newValue = Text.Proper([OriginalName]) หมายถึง "แปลงเป็น Title Case" ทำให้แปลงค่าเดิมไปเป็นค่าใหม่แบบ dynamic
Power Query Formula:

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

Result:

ทั้งหมดเปลี่ยนเป็น Title Case: "Alice", "Bob", "Charlie"

แทนที่ขึ้นต้น/ลงท้าย (Prefix/Suffix removal)
let Codes = Table.FromRecords({ [ItemCode = "SKU-12345"], [ItemCode = "SKU-67890"], [ItemCode = "SKU-54321"] }), Replaced = Table.ReplaceValue(Codes, "SKU-", ""…
ใช้ Replacer.ReplaceText เพื่อลบ prefix ออกจากค่า แทนด้วยข้อความว่าง ("") เหมาะสำหรับสร้างความสะอาดให้กับข้อมูลที่มี prefix/suffix ที่ไม่ต้องการ
Power Query Formula:

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

Result:

ลบ prefix "SKU-" ออก: "12345", "67890", "54321"

แทนที่หลายคอลัมน์พร้อมกัน
let Data = Table.FromRecords({ [FirstName = "N/A", LastName = "N/A", Status = "Active"], [FirstName = "John", LastName = "Doe", Status = "Active"], [FirstName =…
ระบุหลายชื่อคอลัมน์ใน columnsToSearch: {"FirstName", "LastName"} ทำให้ค้นหาและแทนที่ค่า "N/A" ในทั้งสองคอลัมน์พร้อมกัน ประหยัดการใช้ Table.ReplaceValue หลายครั้ง
Power Query Formula:

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

Result:

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

FAQs

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

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

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

Table.ReplaceValue(table, oldValue, newValue, replacer, columnsToSearch) ใช้สำหรับค้นหาค่าเก่าในคอลัมน์ที่ระบุและแทนที่ด้วยค่าใหม่ โดยมีอยู่สองวิธีหลัก: Replacer.ReplaceValue สำหรับแทนที่ค่าทั้งหมด (exact match) หรือ Replacer.ReplaceText สำหรับแทนที่ข้อความเพียงส่วนหนึ่ง (partial match)

ส่วนตัวผม Table.ReplaceValue เป็นเครื่องมือที่โคตร powerful ในการทำความสะอาดข้อมูล โดยเฉพาะตอนที่ต้องกำจัด typos หรือแปลงค่าที่ไม่เป็นมาตรฐาน เช่น เปลี่ยน “USA” เป็น “United States” หรือลบ prefix/suffix ออกจากข้อความ การใช้ each expression ยังทำให้เพิ่มเงื่อนไข conditional logic ได้ด้วย

ที่เจ๋งคือ Table.ReplaceValue สามารถใช้ custom replacer function ได้ ทำให้สามารถสร้างตรรกะการแทนที่ที่ซับซ้อนได้เช่น “แทนที่เฉพาะแถวที่มี condition บางอย่าง” หรือ “แทนที่ให้ตรงกับความยาวของค่าเดิม” นอกจากนี้ยังสามารถค้นหาข้ามหลายคอลัมน์พร้อมกัน ทำให้ประหยัดเวลาเมื่อจัดการข้อมูลที่มีหลายคอลัมน์ต้อง transform

Leave a Reply

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