Thep Excel

Record.TransformFields – แปลงค่าในฟิลด์เรคอร์ด

Record.TransformFields แปลงค่าในฟิลด์ที่ระบุของเรคอร์ดโดยใช้ฟังก์ชันแปลงค่า สามารถแปลงฟิลด์เดียวหรือหลายฟิลด์พร้อมกันได้ เหมาะสำหรับการทำความสะอาดข้อมูลและการแปลงประเภทข้อมูล

=Record.TransformFields(record as record, transformOperations as list, optional missingField as nullable number) as record

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
5/10

Difficulty
5/10

Usefulness
5/10

Syntax & Arguments

=Record.TransformFields(record as record, transformOperations as list, optional missingField as nullable number) as record

Argument Type Required Default Description
record record Yes เรคอร์ดที่ต้องการแปลงค่า
transformOperations list Yes รายการคู่ของชื่อฟิลด์และฟังก์ชันแปลงค่า รูปแบบ: {“FieldName”, Function} สำหรับฟิลด์เดียว หรือ {{“Field1”, Function1}, {“Field2”, Function2}} สำหรับหลายฟิลด์
missingField nullable number Optional พารามิเตอร์ทางเลือกสำหรับการจัดการกับฟิลด์ที่ไม่พบ

Examples

ตัวอย่างที่ 1: แปลงฟิลด์ราคาจากข้อความเป็นตัวเลข
Record.TransformFields( [OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = "100.0"], {{"Price", Number.FromText}} )
แปลงฟิลด์ Price จากข้อความ "100.0" เป็นตัวเลข 100 โดยใช้ฟังก์ชัน Number.FromText ฟิลด์อื่นๆ ยังคงเหมือนเดิม
Power Query Formula:

=Record.TransformFields(
    [OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = "100.0"],
    {{"Price", Number.FromText}}
)

Result:

[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100]

ตัวอย่างที่ 2: แปลงหลายฟิลด์เป็นตัวพิมพ์ใหญ่
Record.TransformFields( [FirstName = "john", LastName = "smith", City = "new york"], {{"FirstName", Text.Upper}, {"LastName", Text.Upper}, {"City", Text.Upper}}…
แปลงทั้งสามฟิลด์เป็นตัวพิมพ์ใหญ่พร้อมกัน โดยใช้ฟังก์ชัน Text.Upper กับฟิลด์ FirstName LastName และ City
Power Query Formula:

=Record.TransformFields(
    [FirstName = "john", LastName = "smith", City = "new york"],
    {{"FirstName", Text.Upper}, {"LastName", Text.Upper}, {"City", Text.Upper}}
)

Result:

[FirstName = "JOHN", LastName = "SMITH", City = "NEW YORK"]

ตัวอย่างที่ 3: การแปลงหลายแบบพร้อมกัน
let Data = [Value1 = 5, Value2 = 10, Name = "alex"], Transformed = Record.TransformFields( Data, {{"Value1", each _ * 2}, {"Value2", each _ + 100}, {"Name", Tex…
แปลงฟิลด์ Value1 โดยคูณด้วย 2 แปลง Value2 โดยบวก 100 และแปลง Name เป็นตัวพิมพ์ใหญ่ ทั้งนี้ทำในการเรียกฟังก์ชันครั้งเดียว
Power Query Formula:

let
    Data = [Value1 = 5, Value2 = 10, Name = "alex"],
    Transformed = Record.TransformFields(
        Data,
        {{"Value1", each _ * 2}, {"Value2", each _ + 100}, {"Name", Text.Upper}}
    )
in
    Transformed

Result:

[Value1 = 10, Value2 = 110, Name = "ALEX"]

ตัวอย่างที่ 4: เปลี่ยนประเภทข้อมูลจากข้อความ
let OrderRecord = [OrderID = "12345", Quantity = "50", Price = "99.99"], Converted = Record.TransformFields( OrderRecord, {{"OrderID", Number.FromText}, {"Quant…
แปลงทั้งสามฟิลด์จากข้อความเป็นตัวเลข เหมาะสำหรับการทำความสะอาดข้อมูลที่นำเข้ามาเป็นข้อความทั้งหมด
Power Query Formula:

let
    OrderRecord = [OrderID = "12345", Quantity = "50", Price = "99.99"],
    Converted = Record.TransformFields(
        OrderRecord,
        {{"OrderID", Number.FromText}, {"Quantity", Number.FromText}, {"Price", Number.FromText}}
    )
in
    Converted

Result:

[OrderID = 12345, Quantity = 50, Price = 99.99]

FAQs

ฟังก์ชัน each ใช้ในการแปลงอย่างไร?

each คือตัวสั้นของ Power Query ที่บอกว่า “เอาค่านี้ แล้วทำอะไรสักอย่าง” โดย _ คือค่าปัจจุบัน เช่น each _ * 2 คือ “เอาค่านี้ คูณ 2” แบบนี้เขียนการแปลงแบบไหนก็ได้โดยไม่ต้องเขียนฟังก์ชันเต็ม

สามารถแปลงฟิลด์ที่ไม่มีอยู่ได้หรือไม่?

ไม่ได้ ถ้าคุณพยายามแปลงฟิลด์ที่ไม่มีจริง มันจะตัดสินใจได้ว่าจะให้ error หรือไม่ขึ้นอยู่กับพารามิเตอร์ missingField เลยดีที่สุดคือ ตรวจสอบชื่อฟิลด์ให้ถูกต้องก่อนรัน

จะแปลงฟิลด์ทั้งหมดในเรคอร์ดได้อย่างไร?

ง่ายแค่ระบุทั้งหมดในรายการ transformOperations แต่จริงๆ ไม่ต้องจัดการฟิลด์ที่ไม่ต้องแปลง เลยโฟกัสแค่ที่ต้องปรับ

ถ้าค่าฟิลด์เป็น null จะเกิดอะไร?

ขึ้นอยู่กับฟังก์ชันที่ใช้ พอ Number.FromText เจอ null มันจะ error เลย แต่บางฟังก์ชันมี null handling อยู่แล้ว วิธีที่ดีที่สุดคือเทสต์ก่อน หรือใช้ฟังก์ชัน error handling จาก Power Query

สามารถใช้ฟังก์ชันที่กำหนดเองได้หรือไม่?

ได้เลย ใช้ each ได้ทั้ง lambda ที่สร้างขึ้นใหม่ กับฟังก์ชันที่สร้างมาแล้ว เช่น each Text.Upper(_) หรือ Text.Upper ตรงๆ ทั้งหมดใช้ได้หมด

Resources & Related

Additional Notes

Record.TransformFields คือฟังก์ชันที่ช่วยแปลงค่าฟิลด์ได้หลายตัวพร้อมกันในเรคอร์ดเดียว เหมือนว่าคุณสามารถให้แต่ละฟิลด์ผ่านการ “ปรับปรุง” ของตัวเองในครั้งเดียวได้ แทนที่จะจัดการทีละตัว.
ที่เจ๋งคือ มันรองรับการแปลงทุกแบบ ตั้งแต่การเปลี่ยนประเภทข้อมูล (ข้อความเป็นตัวเลข) ไปจนถึงการแปลงตามเงื่อนไขที่คุณกำหนดเอง โดยใช้ฟังก์ชัน each ได้
ส่วนตัวผม ใช้มันเมื่อต้องทำความสะอาดข้อมูลจากระบบภายนอก มันช่วยประหยัดเวลาหลายนาทีเทียบกับการแปลงทีละฟิลด์

Leave a Reply

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