Table.TransformColumnTypes เปลี่ยน Data Type ของคอลัมน์ที่ระบุให้เป็นประเภทใหม่ (เช่น Text เป็น Number, Date เป็น Text) โดยใช้ built-in conversion methods เบื้องหลัง รองรับการแปลงหลายคอลัมน์พร้อมกัน และสามารถระบุ culture parameter เพื่อจัดการ locale-specific formatting สำหรับวันที่และตัวเลข เหมาะสำหรับขั้นตอนเตรียมข้อมูลก่อน Load เข้า Data Model เพื่อป้องกัน conversion errors และเพิ่มประสิทธิภาพ ใช้ร่วมกับ Table.TransformColumns, Number.From, Text.From และ Date.From เมื่อต้องการ custom transformation logic
=Table.TransformColumnTypes(table as table, typeTransformations as list, optional culture as nullable text) as table
=Table.TransformColumnTypes(table as table, typeTransformations as list, optional culture as nullable text) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางข้อมูลที่ต้องการเปลี่ยน Data Type | |
| typeTransformations | list | Yes | รายการคู่ {“ชื่อคอลัมน์”, type} เช่น {{“Amount”, Int64.Type}} หรือ {{“Date”, type text}, {“Value”, Percentage.Type}} สำหรับหลายคอลัมน์ รองรับ type constants ได้แก่ type text, type number, Int64.Type, Currency.Type, Percentage.Type, type date, type time, type datetime, type datetimezone, type duration, type logical, type binary แต่ไม่รองรับ type list, type record, type table | |
| culture | nullable text | Optional | null | รหัสวัฒนธรรม (Culture Code) เช่น “en-US” (สหรัฐฯ), “fr-FR” (ฝรั่งเศส), “de-DE” (เยอรมัน), “th-TH” (ไทย) ใช้สำหรับ locale-specific formatting ของวันที่และตัวเลข หรือระบุเป็น record [Culture=”en-US”, MissingField=MissingField.Ignore] เพื่อจัดการคอลัมน์ที่ไม่มีในตาราง (MissingField.UseNull ใส่ null, MissingField.Ignore ข้ามคอลัมน์, default คือ Error) |
เมื่อ import ข้อมูลจาก CSV หรือ Text file ตัวเลขมักจะเป็น Text ("100", "200") ต้องแปลงเป็น Number (100, 200) ก่อนใช้ในการคำนวณหรือ aggregation เช่น SUM, AVERAGE เพื่อให้ Power Query สามารถประมวลผลตัวเลขได้ถูกต้อง
แปลง Date เป็น Text พร้อมระบุ Culture Code เพื่อ export ข้อมูลในรูปแบบที่ตรงตามภูมิภาค เช่น "en-US" ให้ MM/DD/YYYY (03/24/2024), "fr-FR" ให้ DD/MM/YYYY (24/03/2024), "de-DE" ให้ DD.MM.YYYY (24.03.2024) สำหรับการสร้างรายงานสำหรับ users ในภูมิภาคต่างๆ
กำหนด Data Type ที่ถูกต้องก่อน Load เข้า Data Model เพื่อป้องกัน conversion errors, ลดขนาดไฟล์ (เช่น Int64 เล็กกว่า Text), และเพิ่มประสิทธิภาพการ query โดยการแปลง type ที่ Power Query step แทนที่จะให้ data engine แปลงภายหลัง
แปลง Data Type ของหลายคอลัมน์ในคำสั่งเดียว เช่น แปลง OrderDate, ShipDate, DueDate เป็น type date พร้อมกัน หรือแปลง Amount, Tax, Total เป็น Currency.Type ทำให้ code กระชับและลด query steps
เมื่อใช้ query เดียวกับ data sources หลายตัวที่มี schema แตกต่างกัน (เช่น บาง source ไม่มีคอลัมน์ Discount) ใช้ MissingField.Ignore เพื่อข้ามคอลัมน์ที่ไม่มีแทนที่จะเกิด error ทำให้ query ยืดหยุ่นและใช้ซ้ำได้
let Source = Table.FromRecords({ [ID = 1, Amount = "100"], [ID = 2, Amount = "200"], [ID = 3, Amount = "350"] }), ChangedType = Table.TransformColumnTypes( Sour…let
Source = Table.FromRecords({
[ID = 1, Amount = "100"],
[ID = 2, Amount = "200"],
[ID = 3, Amount = "350"]
}),
ChangedType = Table.TransformColumnTypes(
Source,
{{"Amount", Int64.Type}}
)
in
ChangedType
Table: Amount column เป็น Number (100, 200, 350) แทน Text ("100", "200", "350")
let Source = Table.FromRecords({ [Name = "Product A", Price = "199.50", Quantity = "10", InStock = "true"], [Name = "Product B", Price = "299.00", Quantity = "5…let
Source = Table.FromRecords({
[Name = "Product A", Price = "199.50", Quantity = "10", InStock = "true"],
[Name = "Product B", Price = "299.00", Quantity = "5", InStock = "false"]
}),
ChangedType = Table.TransformColumnTypes(
Source,
{
{"Price", type number},
{"Quantity", Int64.Type},
{"InStock", type logical}
}
)
in
ChangedType
Table: Price เป็น Decimal (199.5, 299), Quantity เป็น Int64 (10, 5), InStock เป็น Logical (true, false)
let Source = Table.FromRecords({ [Company = "JS-464", Country = "USA", OrderDate = #date(2024, 3, 24)], [Company = "LT-331", Country = "France", OrderDate = #da…let
Source = Table.FromRecords({
[Company = "JS-464", Country = "USA", OrderDate = #date(2024, 3, 24)],
[Company = "LT-331", Country = "France", OrderDate = #date(2024, 10, 5)],
[Company = "XE-100", Country = "USA", OrderDate = #date(2024, 5, 21)]
}),
ChangedType = Table.TransformColumnTypes(
Source,
{{"OrderDate", type text}},
"fr-FR"
)
in
ChangedType
OrderDate เป็น Text ในรูปแบบฝรั่งเศส: "24/03/2024", "05/10/2024", "21/05/2024" (DD/MM/YYYY)
let Source = Table.FromRecords({ [Date = #date(2024, 3, 12), Customer = "134282", Discount = 0.24368], [Date = #date(2024, 5, 30), Customer = "44343", Discount…let
Source = Table.FromRecords({
[Date = #date(2024, 3, 12), Customer = "134282", Discount = 0.24368],
[Date = #date(2024, 5, 30), Customer = "44343", Discount = 0.03556]
}),
ChangedType = Table.TransformColumnTypes(
Source,
{{"Date", type text}, {"Discount", Percentage.Type}},
"de-DE"
)
in
ChangedType
Date: "12.03.2024", "30.05.2024" (German DD.MM.YYYY); Discount: 24.37%, 3.56%
let Source = Table.FromRecords({ [OrderID = 1, Amount = "100", Status = "Active"], [OrderID = 2, Amount = "200", Status = "Pending"] }), // ตาราง Source ไม่มีคอ…let
Source = Table.FromRecords({
[OrderID = 1, Amount = "100", Status = "Active"],
[OrderID = 2, Amount = "200", Status = "Pending"]
}),
// ตาราง Source ไม่มีคอลัมน์ Discount แต่เราต้องการ reusable query
ChangedType = Table.TransformColumnTypes(
Source,
{{"Amount", Int64.Type}, {"Discount", type number}},
[Culture = "en-US", MissingField = MissingField.Ignore]
)
in
ChangedType
Table: Amount เป็น Int64 (100, 200), คอลัมน์ Discount ไม่มีจึงถูกข้าม (ไม่เกิด error)
รองรับ type พื้นฐานได้แก่ type text, type number, Int64.Type, Currency.Type, Percentage.Type, type date, type time, type datetime, type datetimezone, type duration, type logical, type binary และ type any แต่ไม่รองรับ type ที่ซับซ้อนเช่น type list, type record, type table, type function หากพยายามใช้ type เหล่านี้จะเกิด error
จะเกิด error ทันที (default behavior) แต่สามารถป้องกันได้โดยระบุ culture parameter เป็น record [Culture=”en-US”, MissingField=MissingField.Ignore] เพื่อข้าม column ที่ไม่มี หรือใช้ MissingField.UseNull เพื่อสร้าง column ที่มีค่า null ทุก row แทน ตัวเลือกนี้มีประโยชน์เมื่อใช้ query เดียวกับ data sources หลายตัวที่มี schema แตกต่างกัน
Culture Code กำหนดรูปแบบ locale-specific formatting ตามภูมิภาค เช่น วันที่: “en-US” ใช้ MM/DD/YYYY (03/24/2024), “fr-FR” ใช้ DD/MM/YYYY (24/03/2024), “de-DE” ใช้ DD.MM.YYYY (24.03.2024); ตัวเลข: ทศนิยมใช้ . (dot) หรือ , (comma), thousands separator ใช้ , หรือ . ตาม culture; เปอร์เซ็นต์: การแสดงผลของ Percentage.Type culture จะส่งผลต่อการแปลง Date → Text และ Number → Text เป็นหลัก ไม่ส่งผลต่อการแปลง Text → Date/Number
ใช้เมื่อต้องการ export ข้อมูลให้ users ในภูมิภาคต่างๆ (เช่น แปลง Date เป็น Text ในรูปแบบที่เหมาะกับภูมิภาค) หรือเมื่อ import ข้อมูลที่มีรูปแบบวันที่/ตัวเลขแบบเฉพาะภูมิภาค เช่น ข้อมูลจากยุโรปที่ใช้ comma (,) เป็นทศนิยมแทน dot (.) หรือเมื่อต้องการจัดการ missing columns ด้วย MissingField options สำหรับการแปลง type ทั่วไป (เช่น Text → Number) ไม่จำเป็นต้องระบุ culture
Table.TransformColumnTypes ใช้ type constants (เช่น type text, Int64.Type) และรองรับ culture parameter สำหรับ locale-specific formatting โดยใช้ built-in .From methods เบื้องหลัง (เช่น Number.From, Text.From) เหมาะสำหรับการแปลง type แบบ standard ส่วน Table.TransformColumns ให้ระบุ custom transformation function (เช่น each Text.Upper([Name])) ได้ยืดหยุ่นกว่า แต่ไม่มี culture support เหมาะกับ custom logic ที่ซับซ้อน โดยทั่วไปใช้ Table.TransformColumnTypes สำหรับ type conversion และ Table.TransformColumns สำหรับ value transformation
จะเกิด error ในแถวนั้นและแสดง [Expression.Error] แทนค่า โดยค่าอื่นในแถวเดียวกันยังคงอยู่ แต่คอลัมน์ที่แปลงไม่สำเร็จจะเป็น error สามารถป้องกันได้โดยใช้ try…otherwise ก่อน Table.TransformColumnTypes เช่น Table.AddColumn(Source, “AmountClean”, each try Number.From([Amount]) otherwise null) แล้วค่อยแปลง type หรือใช้ Table.TransformColumns กับ custom function ที่มี error handling แทน
การแปลง type ที่เหมาะสมช่วยเพิ่มประสิทธิภาพ เพราะ (1) Data type ที่ถูกต้องใช้ memory น้อยกว่า เช่น Int64 เล็กกว่า Text, (2) Query engine ทำ aggregations (SUM, AVG) กับ Number เร็วกว่า Text, (3) Type conversion ที่ Power Query step เร็วกว่าให้ data engine แปลงภายหลัง แต่ควรแปลงเฉพาะคอลัมน์ที่จำเป็น และวาง step นี้ไว้หลัง filter steps เพื่อลดจำนวน rows ที่ต้องแปลง หลีกเลี่ยงการแปลง type ซ้ำหลาย steps
Table.TransformColumnTypes ใช้แปลงชนิดข้อมูล (Data Type) ของคอลัมน์ใน Power Query โดยระบบจะใช้ built-in conversion methods ของแต่ละชนิดข้อมูล เช่น Number.From สำหรับแปลงเป็นตัวเลข Text.From สำหรับแปลงเป็นข้อความ และ Date.From สำหรับแปลงเป็นวันที่ ทำงานเบื้องหลังอัตโนมัติ
.
ที่เจ๋งคือฟังก์ชันนี้รองรับการแปลงหลายคอลัมน์พร้อมกันในคำสั่งเดียว และมี culture parameter สำหรับจัดการรูปแบบข้อมูลตามภูมิภาค (locale-specific formatting) สำหรับวันที่และตัวเลขได้ตามมาตรฐานของแต่ละประเทศครับ 😎
.
การเปลี่ยนชนิดข้อมูล (Data Type) เป็นขั้นตอนสำคัญมากในการเตรียมข้อมูลก่อนนำเข้า Data Model เพราะ Power Query จะทำการตรวจสอบและแปลงค่าในแต่ละแถว (row) ตามชนิดข้อมูลที่คุณกำหนดไว้
.
ถ้าการแปลงค่าล้มเหลว เช่น พยายามแปลงข้อความ “abc” เป็นตัวเลข (Number) ซึ่งเป็นไปไม่ได้ ระบบจะแสดง error หรือให้ค่า null ตามการตั้งค่า ด้วยเหตุนี้ การกำหนดชนิดข้อมูลที่ถูกต้องตั้งแต่ขั้นตอนเริ่มต้นจึงช่วยป้องกันปัญหาคุณภาพข้อมูล (data quality issues) และเพิ่มประสิทธิภาพในการประมวลผลข้อมูลได้อย่างมีนัยสำคัญครับ
.
Table.TransformColumnTypes มีความแตกต่างจาก Table.TransformColumns อย่างชัดเจนนะครับ โดย Table.TransformColumnTypes จะใช้ type constants เช่น type number, Int64.Type, type text และมีพารามิเตอร์ culture สำหรับการจัดรูปแบบตามภูมิภาค (locale-specific formatting)
.
ในขณะที่ Table.TransformColumns จะให้คุณระบุฟังก์ชันแปลงค่าที่กำหนดเอง (custom transformation function) เช่น each Text.Upper([Name]) เพื่อแปลงตัวอักษรเป็นตัวใหญ่ได้ยืดหยุ่นกว่า แต่ไม่มีการรองรับพารามิเตอร์ culture
.
เรียกได้ว่า Table.TransformColumnTypes เหมาะสำหรับการแปลงชนิดข้อมูลแบบมาตรฐาน (standard type conversion) ส่วน Table.TransformColumns เหมาะกับการแปลงค่าที่มี custom logic ที่ซับซ้อน เช่น การใช้ List.Sum เพื่อรวมค่าตัวเลข หรือ Table.AddColumn เพื่อเพิ่มคอลัมน์ใหม่ด้วยการคำนวณที่ซับซ้อนครับ
.
ฟังก์ชันนี้มีประโยชน์อย่างมากในการทำงานกับข้อมูลที่นำเข้าจากแหล่งต่างๆ เช่น ไฟล์ CSV หรือฐานข้อมูลภายนอก ซึ่งมักมีปัญหาเรื่องชนิดข้อมูลที่ไม่ตรงตามที่ต้องการ เช่น ตัวเลขที่ถูกเก็บเป็นข้อความ หรือวันที่ที่อยู่ในรูปแบบข้อความ
.
การใช้ Table.TransformColumnTypes ช่วยให้คุณสามารถกำหนดชนิดข้อมูลที่ถูกต้องได้อย่างรวดเร็วและมีประสิทธิภาพ โดยเฉพาะเมื่อต้องการแปลงหลายคอลัมน์พร้อมกัน ซึ่งจะช่วยประหยัดเวลาและทำให้โค้ดของคุณกระชับขึ้นอย่างมากครับ 💡
.
นอกจากนี้ การใช้พารามิเตอร์ culture ยังมีประโยชน์อย่างมากสำหรับการทำงานกับข้อมูลที่มาจากหลายประเทศ เช่น ถ้าคุณมีข้อมูลวันที่จากยุโรปที่ใช้รูปแบบ DD.MM.YYYY หรือจากอเมริกาที่ใช้ MM/DD/YYYY
.
คุณสามารถใช้พารามิเตอร์ culture เพื่อให้ระบบแปลงค่าได้อย่างถูกต้องตามรูปแบบของแต่ละภูมิภาค หรือเมื่อคุณต้องการส่งออกข้อมูลไปยังผู้ใช้ในประเทศต่างๆ และต้องการให้วันที่แสดงในรูปแบบที่เหมาะสมกับภูมิภาคของพวกเขา
.
ส่วนตัวผมใช้ culture parameter บ่อยมากเวลาทำงานกับข้อมูลระหว่างประเทศครับ จะทำให้ข้อมูลของคุณมีความถูกต้องและเข้าใจง่ายสำหรับผู้ใช้ในแต่ละภูมิภาค 😎