Table.ReplaceValue ใช้สำหรับค้นหาและแทนที่ค่าในตารางได้หลายวิธี ตั้งแต่แทนที่ค่าทั้งหมดไปจนถึงแทนที่แบบมีเงื่อนไข
= Table.ReplaceValue(table as table, oldValue as any, newValue as any, replacer as function, columnsToSearch as list) as table
= 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”} |
let Sales = Table.FromRecords({ [Product = "Laptop", Status = "approved"], [Product = "Mouse", Status = "approved"], [Product = "Keyboard", Status = "pending"]…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" ยังคงเดิม
let Data = Table.FromRecords({ [Name = "John_Smith"], [Name = "Jane_Doe"], [Name = "Bob_Johnson"] }), Replaced = Table.ReplaceValue(Data, "_", " ", Replacer.Rep…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"
let Orders = Table.FromRecords({ [Customer = "Alice", Country = "US", Status = "New"], [Customer = "Bob", Country = "UK", Status = "New"], [Customer = "Charlie"…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"
let Names = Table.FromRecords({ [OriginalName = "alice"], [OriginalName = "bob"], [OriginalName = "charlie"] }), Replaced = Table.ReplaceValue( Names, each [Ori…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"
let Codes = Table.FromRecords({ [ItemCode = "SKU-12345"], [ItemCode = "SKU-67890"], [ItemCode = "SKU-54321"] }), Replaced = Table.ReplaceValue(Codes, "SKU-", ""…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"
let Data = Table.FromRecords({ [FirstName = "N/A", LastName = "N/A", Status = "Active"], [FirstName = "John", LastName = "Doe", Status = "Active"], [FirstName =…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
Replacer.ReplaceValue ใช้สำหรับแทนที่ค่าทั้งหมด (exact match) – ถ้าค้นหา ‘goodbye’ จะไม่ match ‘goodbyes’. Replacer.ReplaceText ใช้สำหรับแทนที่ข้อความเพียงส่วนหนึ่ง (substring) – ถ้าค้นหา ‘good’ จะ match ทั้ง ‘goodbye’ และ ‘goodbyes’ แล้วแทนที่เพียงส่วนนั้น
Table.ReplaceValue ใช้สำหรับค้นหาค่าเฉพาะและแทนที่ (targeted replacement) ส่วน Table.TransformColumns ใช้สำหรับแปลงทุกค่าในคอลัมน์ด้วยฟังก์ชัน (comprehensive transformation) ถ้าต้องการแปลงค่าทั้งหมดใช้ Table.TransformColumns ถ้าต้องการแทนที่เฉพาะบางค่าใช้ Table.ReplaceValue
ได้ใช้ใน 3 ที่: (1) oldValue = each [column] = value – สำหรับ conditional search, (2) newValue = each formula – สำหรับ dynamic replacement, (3) both – ใช้ทั้งสองพร้อมเพื่อสร้างความจำเพาะสูงสุด
ไม่ error คำสั่ง Table.ReplaceValue จะทำงานปกติ เพียงแต่ไม่มีการแทนที่เพราะไม่พบค่าที่ค้นหา ตารางส่งกลับมาเหมือนเดิม
ได้ เช่น Table.ReplaceValue(Table.ReplaceValue(Source, “A”, “B”, …), “C”, “D”, …) แต่ถ้าแทนที่หลายค่า ลองใช้ let…in เพื่อให้อ่านง่าย หรือใช้ custom replacer function สำหรับตรรกะที่ซับซ้อน
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