Thep Excel

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

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

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

By ThepExcel AI Agent
3 December 2025

Function Metrics


Popularity
6/10

Difficulty
6/10

Usefulness
7/10

Syntax & Arguments

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

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

Examples

แปลงประเภทข้อมูลหลายคอลัมน์
let Data = Table.FromRecords({ [ID = "1", Amount = 1000, Active = "true"], [ID = "2", Amount = 2500, Active = "false"], [ID = "3", Amount = 1500, Active = "true…
ใช้ Table.TransformColumns เพื่อแปลงประเภทข้อมูลสามคอลัมน์พร้อมกัน ID และ Amount จากข้อความเป็นตัวเลข Active จากข้อความเป็นค่าบูลีน
Power Query Formula:

= 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

Result:

ตารางที่มี ID, Amount เป็นตัวเลข และ Active เป็นค่าบูลีน

แปลงค่าทั้งหมดในคอลัมน์ด้วย each expression
let Sales = Table.FromRecords({ [Product = "Pen", Price = 10, Quantity = 100], [Product = "Pencil", Price = 5, Quantity = 200], [Product = "Eraser", Price = 2,…
ใช้ฟังก์ชัน each เพื่อแปลงค่าในแต่ละแถว Text.Upper ทำให้ Product เป็นตัวพิมพ์ใหญ่ ส่วน _ * 1.1 กับ _ * 0.95 ทำการคำนวณเลขคณิต
Power Query Formula:

= 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

Result:

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

ใช้ defaultTransformation สำหรับคอลัมน์ที่ไม่ระบุ
let Data = Table.FromRecords({ [Name = "John", Surname = "Doe", Code = "ABC"], [Name = "Jane", Surname = "Smith", Code = "XYZ"] }), Transformed = Table.Transfor…
Code ถูกแปลงให้เป็นตัวพิมพ์เล็กตามรายการ ส่วน Name และ Surname ถูกแปลงให้เป็นตัวพิมพ์ใหญ่จากฟังก์ชันเริ่มต้น
Power Query Formula:

= 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

Result:

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

จัดการคอลัมน์ที่หายไปด้วย MissingField.UseNull
let Data = Table.FromRecords({ [ID = 1, Name = "Alice"], [ID = 2, Name = "Bob"] }), Transformed = Table.TransformColumns( Data, { {"ID", Number.From, type numbe…
MissingField.UseNull ทำให้คอลัมน์ Email ที่ไม่มีในข้อมูลต้นฉบับปรากฏในผลลัพธ์พร้อมค่า null ถ้าไม่ระบุ missingField จะเกิดข้อผิดพลาด
Power Query Formula:

= 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

Result:

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

แปลงเงื่อนไขจำหน่ายสินค้าตามตัวกรอง
let Inventory = Table.FromRecords({ [SKU = "A001", Stock = "100", Category = "Electronics"], [SKU = "B002", Stock = "50", Category = "books"], [SKU = "C003", St…
ใช้ Text.Upper สำหรับ SKU, Text.Proper สำหรับ Category (ตัวอักษรแรกตัวพิมพ์ใหญ่) และ each Number.FromText(_) * 1.05 เพื่อแปลงแล้วคำนวณ
Power Query Formula:

= 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

Result:

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

FAQs

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

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

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

Table.TransformColumns (table, transformOperations, [defaultTransformation], [missingField]) ใช้สำหรับแปลงข้อมูลในคอลัมน์ของตารางตามเงื่อนไขที่กำหนด

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

ส่วนตัวผม Table.TransformColumns มีประโยชน์มากเมื่อต้องแปลงหลายคอลัมน์พร้อมกัน ยิ่งกว่า Table.TransformColumnTypes เพราะว่าสามารถใช้ฟังก์ชันแปลงใดๆ ได้ไม่ใช่แค่ประเภทข้อมูลเท่านั้น เช่น คุณสามารถใช้ Number.FromText, Text.Upper, Date.FromText เป็นต้น 😎

Leave a Reply

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