---
title: Table.TransformColumns – แปลงคอลัมน์ด้วยฟังก์ชันการแปลง
url: https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumns/
type: function-explainer
program: Power Query
syntax: "Table.TransformColumns(table, transformOperations, [defaultTransformation], [missingField])"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 6
  difficulty: 6
  usefulness: 7
---

# Table.TransformColumns – แปลงคอลัมน์ด้วยฟังก์ชันการแปลง

> Table.TransformColumns ใช้สำหรับแปลงข้อมูลในคอลัมน์โดยใช้ฟังก์ชันที่กำหนดไว้ แต่ละคอลัมน์สามารถมีการ

## คำอธิบาย

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

## Syntax

```excel
Table.TransformColumns(table, transformOperations, [defaultTransformation], [missingField])
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | table |  | ตารางที่ต้องการแปลง |
| transformOperations | Yes | list |  | รายการการแปลง โดยมี 2 รูปแบบ: {column name, transformation} หรือ {column name, transformation, new type} |
| defaultTransformation | No | nullable function | null | ฟังก์ชันการแปลงที่ใช้กับคอลัมน์ที่ไม่ระบุในรายการการแปลง |
| missingField | No | nullable number | MissingField.Error | ระบุว่าจะทำอย่างไรกับคอลัมน์ที่หายไป: MissingField.Error (default), MissingField.UseNull หรือ MissingField.Ignore |

## ตัวอย่าง

### 1. แปลงประเภทข้อมูลหลายคอลัมน์

```excel
= let
    Data = Table.FromRecords({
        [ID = "1", Amount = 1000, Active = "true"],
        [ID = "2", Amount = 2500, Active = "false"],
        [ID = "3", Amount = 1500, Active = "true"]
    }),
    Transformed = Table.TransformColumns(Data, {
        {"ID", Number.FromText, type number},
        {"Amount", Number.FromText, type number},
        {"Active", Logical.FromText, type logical}
    })
in
    Transformed
```

**ผลลัพธ์:** `ตารางที่มี ID, Amount เป็นตัวเลข และ Active เป็นค่าบูลีน`

ใช้ Table.TransformColumns เพื่อแปลงประเภทข้อมูลสามคอลัมน์พร้อมกัน ID และ Amount จากข้อความเป็นตัวเลข Active จากข้อความเป็นค่าบูลีน

### 2. แปลงค่าทั้งหมดในคอลัมน์ด้วย each expression

```excel
= let
    Sales = Table.FromRecords({
        [Product = "Pen", Price = 10, Quantity = 100],
        [Product = "Pencil", Price = 5, Quantity = 200],
        [Product = "Eraser", Price = 2, Quantity = 150]
    }),
    Transformed = Table.TransformColumns(Sales, {
        {"Product", Text.Upper, type text},
        {"Price", each _ * 1.1, type number},
        {"Quantity", each _ * 0.95, type number}
    })
in
    Transformed
```

**ผลลัพธ์:** `Product เป็นตัวพิมพ์ใหญ่ Price เพิ่มขึ้น 10% Quantity ลดลง 5%`

ใช้ฟังก์ชัน each เพื่อแปลงค่าในแต่ละแถว Text.Upper ทำให้ Product เป็นตัวพิมพ์ใหญ่ ส่วน _ * 1.1 กับ _ * 0.95 ทำการคำนวณเลขคณิต

### 3. ใช้ defaultTransformation สำหรับคอลัมน์ที่ไม่ระบุ

```excel
= let
    Data = Table.FromRecords({
        [Name = "John", Surname = "Doe", Code = "ABC"],
        [Name = "Jane", Surname = "Smith", Code = "XYZ"]
    }),
    Transformed = Table.TransformColumns(
        Data,
        {{"Code", Text.Lower, type text}},
        Text.Upper  // defaultTransformation สำหรับคอลัมน์อื่น
    )
in
    Transformed
```

**ผลลัพธ์:** `Code เป็นตัวพิมพ์เล็ก Name และ Surname เป็นตัวพิมพ์ใหญ่`

Code ถูกแปลงให้เป็นตัวพิมพ์เล็กตามรายการ ส่วน Name และ Surname ถูกแปลงให้เป็นตัวพิมพ์ใหญ่จากฟังก์ชันเริ่มต้น

### 4. จัดการคอลัมน์ที่หายไปด้วย MissingField.UseNull

```excel
= let
    Data = Table.FromRecords({
        [ID = 1, Name = "Alice"],
        [ID = 2, Name = "Bob"]
    }),
    Transformed = Table.TransformColumns(
        Data,
        {
            {"ID", Number.From, type number},
            {"Name", Text.Upper, type text},
            {"Email", Text.Upper, type text}  // คอลัมน์ที่ไม่มีในข้อมูล
        },
        null,
        MissingField.UseNull
    )
in
    Transformed
```

**ผลลัพธ์:** `ตารางเพิ่มคอลัมน์ Email ที่มีค่า null ในทุกแถว`

MissingField.UseNull ทำให้คอลัมน์ Email ที่ไม่มีในข้อมูลต้นฉบับปรากฏในผลลัพธ์พร้อมค่า null ถ้าไม่ระบุ missingField จะเกิดข้อผิดพลาด

### 5. แปลงเงื่อนไขจำหน่ายสินค้าตามตัวกรอง

```excel
= let
    Inventory = Table.FromRecords({
        [SKU = "A001", Stock = "100", Category = "Electronics"],
        [SKU = "B002", Stock = "50", Category = "books"],
        [SKU = "C003", Stock = "200", Category = "OFFICE"]
    }),
    Transformed = Table.TransformColumns(Inventory, {
        {"SKU", Text.Upper, type text},
        {"Stock", each Number.FromText(_) * 1.05, type number},
        {"Category", Text.Proper, type text}
    })
in
    Transformed
```

**ผลลัพธ์:** `SKU และ Category ถูกปรับรูปแบบ Stock คำนวณใหม่เพิ่มขึ้น 5%`

ใช้ Text.Upper สำหรับ SKU, Text.Proper สำหรับ Category (ตัวอักษรแรกตัวพิมพ์ใหญ่) และ each Number.FromText(_) * 1.05 เพื่อแปลงแล้วคำนวณ

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

- ใช้ Table.TransformColumns เมื่อต้องแปลงหลายคอลัมน์พร้อมกัน เพื่อหลีกเลี่ยงการเขียนหลายคำสั่ง Table.TransformColumnTypes

- หากต้องการแปลงคอลัมน์เดียว ให้ใช้ Table.ReplaceValue หรือ Table.AddColumn จะง่ายกว่า

- ใช้ Text.Trim() ร่วมกับการแปลงข้อความเพื่อเอาช่องว่างออก เช่น {"Name", each Text.Trim(Text.Upper(_))}

- สำหรับการแปลงเงื่อนไข ใช้ if-then-else ภายใน each expression เช่น {"Status", each if _ = "Active" then "YES" else "NO"}

- ตรวจสอบประเภทข้อมูลที่ได้หลังจากแปลง โดยใช้ Table.Schema() เพื่อดูประเภทของแต่ละคอลัมน์

- ใช้ MissingField.Ignore เมื่อต้องการข้ามคอลัมน์ที่ไม่มีโดยไม่เกิดข้อผิดพลาด

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

**Q: ต่างกันยังไงระหว่าง Table.TransformColumns กับ Table.TransformColumnTypes**

Table.TransformColumnTypes ใช้สำหรับเปลี่ยนประเภทข้อมูลเท่านั้น ส่วน Table.TransformColumns สามารถใช้ฟังก์ชันแปลงใดๆ ได้ เช่น การเปลี่ยนเนื้อหา การคำนวณ ฯลฯ ทำให้ Table.TransformColumns มีความยืดหยุ่นมากกว่า

**Q: ถ้ากำหนดคอลัมน์ที่ไม่มีในข้อมูลจะเกิดอะไร**

หากไม่ระบุ missingField จะเกิดข้อผิดพลาด Error.Expression ถ้าต้องการสร้างคอลัมน์ใหม่ให้ใช้ MissingField.UseNull เพื่อเพิ่มคอลัมน์นั้นพร้อมค่า null

**Q: สามารถแปลงคอลัมน์เดียวกันหลายครั้งได้ไหม**

ได้ แต่จะแปลงตามลำดับในรายการ ถ้าต้องการแปลงหลายครั้งให้ใส่ฟังก์ชันแปลงที่ซ้อนกัน เช่น {"Column", each Text.Upper(Text.Trim(_))}

**Q: ทำไมต้องระบุประเภทข้อมูลใหม่ (new type) ในรายการแปลง**

ประเภทข้อมูลช่วยให้ Power Query รู้ว่าคอลัมน์นั้นควรแสดงเป็นประเภทใด และสามารถตรวจสอบให้สอดคล้องกับการแปลง ถ้าไม่ระบุ Power Query จะใช้ประเภทเดิม

**Q: defaultTransformation ใช้กับคอลัมน์ที่ระบุแล้วหรือไม่**

ไม่ defaultTransformation ใช้เฉพาะคอลัมน์ที่ไม่ระบุในรายการ transformOperations เท่านั้น

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

- [Table.TransformColumnTypes – เปลี่ยนชนิดข้อมูลของคอลัมน์](https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumntypes/)
- [Table.AddColumn – เพิ่มคอลัมน์ใหม่ด้วย Calculated Values](https://www.thepexcel.com/functions/power-query/table-functions/table-addcolumn/)
- [Table.ReplaceValue – แทนที่ค่าในตารางตามเงื่อนไข](https://www.thepexcel.com/functions/power-query/table-functions/table-replacevalue/)
- [List.Transform – แปลงค่าในลิสต์แต่ละตัว](https://www.thepexcel.com/functions/power-query/list-functions/list-transform/)
- [Table.SelectColumns – เลือกคอลัมน์ที่ต้องการจากตาราง](https://www.thepexcel.com/functions/power-query/table-functions/table-selectcolumns/)

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

- [Microsoft Learn - Table.TransformColumns](https://learn.microsoft.com/en-us/powerquery-m/table-transformcolumns) _(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-transformcolumns/](https://www.thepexcel.com/functions/power-query/table-functions/table-transformcolumns/)_
