---
title: Record.TransformFields – แปลงค่าในฟิลด์เรคอร์ด
url: https://www.thepexcel.com/functions/power-query/record-functions/record-transformfields/
type: function-explainer
program: Power Query
syntax: "Record.TransformFields(record as record, transformOperations as list, optional missingField as nullable number) as record"
date: 2025-12-03
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 5
  usefulness: 5
---

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

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

## คำอธิบาย

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

## Syntax

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

## Arguments

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

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: แปลงฟิลด์ราคาจากข้อความเป็นตัวเลข

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

**ผลลัพธ์:** `[OrderID = 1, CustomerID = 1, Item = "Fishing rod", Price = 100]`

แปลงฟิลด์ Price จากข้อความ "100.0" เป็นตัวเลข 100 โดยใช้ฟังก์ชัน Number.FromText ฟิลด์อื่นๆ ยังคงเหมือนเดิม

### 2. ตัวอย่างที่ 2: แปลงหลายฟิลด์เป็นตัวพิมพ์ใหญ่

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

**ผลลัพธ์:** `[FirstName = "JOHN", LastName = "SMITH", City = "NEW YORK"]`

แปลงทั้งสามฟิลด์เป็นตัวพิมพ์ใหญ่พร้อมกัน โดยใช้ฟังก์ชัน Text.Upper กับฟิลด์ FirstName LastName และ City

### 3. ตัวอย่างที่ 3: การแปลงหลายแบบพร้อมกัน

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

**ผลลัพธ์:** `[Value1 = 10, Value2 = 110, Name = "ALEX"]`

แปลงฟิลด์ Value1 โดยคูณด้วย 2 แปลง Value2 โดยบวก 100 และแปลง Name เป็นตัวพิมพ์ใหญ่ ทั้งนี้ทำในการเรียกฟังก์ชันครั้งเดียว

### 4. ตัวอย่างที่ 4: เปลี่ยนประเภทข้อมูลจากข้อความ

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

**ผลลัพธ์:** `[OrderID = 12345, Quantity = 50, Price = 99.99]`

แปลงทั้งสามฟิลด์จากข้อความเป็นตัวเลข เหมาะสำหรับการทำความสะอาดข้อมูลที่นำเข้ามาเป็นข้อความทั้งหมด

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

- ทำการแปลงหลายฟิลด์พร้อมกันเลย ดีกว่าแปลงทีละตัวด้วย Record.AddField หลายครั้ง มันเร็วกว่าและอ่านง่ายกว่า

- เลือกฟังก์ชันที่มีชื่อเข้าใจง่าย แบบ Text.Upper, Number.FromText ทำให้ใครอ่านโค้ดก็รู้เลยว่าแปลงอะไร

- ต้องการแปลงแบบเป็นเอง? เอา each มาใช้ได้เลย ไม่ว่า each _ * 2 หรือ each Text.Upper(_) ทั้งหมดได้

- เตรียมตัวให้พร้อมสำหรับค่า null ตรวจสอบว่าฟิลด์อาจมีค่าว่างหรือไม่ ก่อนรัน

- เวลาทำงานกับเรคอร์ด ลองจัดหา Record.AddField, Record.RemoveFields, Record.SelectFields มาช่วย ทำให้การจัดการข้อมูลเรียบร้อยขึ้น

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

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

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

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

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

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

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

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

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

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

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

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

- [List.Transform – แปลงค่าในลิสต์แต่ละตัว](https://www.thepexcel.com/functions/power-query/list-functions/list-transform/)
- [Table.TransformColumns – แปลงคอลัมน์ด้วยฟังก์ชันการแปลง](https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumns/)
- [Record.RemoveFields – Remove fields from a record](https://www.thepexcel.com/functions/power-query/record-functions/record-removefields/)
- [Text.Upper – แปลงข้อความเป็นตัวอักษรใหญ่](https://www.thepexcel.com/functions/power-query/text-functions/text-upper/)
- [Number.FromText – แปลงข้อความเป็นตัวเลข](https://www.thepexcel.com/functions/power-query/number-functions/number-fromtext/)
- [Number.From – แปลงค่าเป็นตัวเลข](https://www.thepexcel.com/functions/power-query/number-functions/number-from/)

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

- [Microsoft Learn: Record.TransformFields](https://learn.microsoft.com/en-us/powerquery-m/record-transformfields) _(official)_
- [Microsoft Learn: Record Functions](https://learn.microsoft.com/en-us/powerquery-m/record-functions) _(official)_
- [Power Query M Language Reference](https://learn.microsoft.com/en-us/powerquery-m/power-query-m-function-reference) _(official)_

---

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