Table.TransformRows ใช้สำหรับประมวลผลแต่ละแถวของตารางด้วยฟังก์ชนที่กำหนด แล้วคืนผลลัพธ์กลับมาเป็น List (ไม่ใช่ Table)
=Table.TransformRows(table as table, transform as function) as list
=Table.TransformRows(table as table, transform as function) as list
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางที่ต้องการประมวลผลแต่ละแถว | |
| transform | function | Yes | ฟังก์ชันที่ใช้แปลงข้อมูลแต่ละแถว สามารถใช้ each [Column] หรือ (row) => expression ได้ |
สร้าง List ใหม่จากการคำนวณค่าในแต่ละแถว
แปลงโครงสร้างข้อมูลจากตารางเป็น List ของ Record หรือค่าอื่นๆ
เตรียมข้อมูลสำหรับการประมวลผลด้วย List functions
let Sales = Table.FromRecords({ [OrderID = 1, Amount = 1500], [OrderID = 2, Amount = 2300], [OrderID = 3, Amount = 800] }), AmountList = Table.TransformRows(Sal…let
Sales = Table.FromRecords({
[OrderID = 1, Amount = 1500],
[OrderID = 2, Amount = 2300],
[OrderID = 3, Amount = 800]
}),
AmountList = Table.TransformRows(Sales, each [Amount])
in
AmountList
// Result: {1500, 2300, 800}
{1500, 2300, 800}
let Invoice = Table.FromRecords({ [Qty = 5, Price = 100], [Qty = 3, Price = 200], [Qty = 10, Price = 50] }), Totals = Table.TransformRows(Invoice, each [Qty] *…let
Invoice = Table.FromRecords({
[Qty = 5, Price = 100],
[Qty = 3, Price = 200],
[Qty = 10, Price = 50]
}),
Totals = Table.TransformRows(Invoice, each [Qty] * [Price])
in
Totals
// Result: {500, 600, 500}
{500, 600, 500}
let Employee = Table.FromRecords({ [ID = 1, Name = "Alice", Salary = 50000], [ID = 2, Name = "Bob", Salary = 55000] }), WithBonus = Table.TransformRows( Employe…let
Employee = Table.FromRecords({
[ID = 1, Name = "Alice", Salary = 50000],
[ID = 2, Name = "Bob", Salary = 55000]
}),
WithBonus = Table.TransformRows(
Employee,
(row) => [
ID = row[ID],
Name = row[Name],
Annual = row[Salary] * 12,
Bonus = Number.ToText(row[Salary] * 0.1)
]
)
in
WithBonus
// Result: {[ID = 1, Name = "Alice", Annual = 600000, Bonus = "5000"], [ID = 2, Name = "Bob", Annual = 660000, Bonus = "5500"]}
{[ID = 1, Name = "Alice", Annual = 600000, Bonus = "5000"], [ID = 2, Name = "Bob", Annual = 660000, Bonus = "5500"]}
let Products = Table.FromRecords({ [ProductName = "Laptop", Category = "Electronics"], [ProductName = "Mouse", Category = "Electronics"], [ProductName = "Desk",…let
Products = Table.FromRecords({
[ProductName = "Laptop", Category = "Electronics"],
[ProductName = "Mouse", Category = "Electronics"],
[ProductName = "Desk", Category = "Furniture"]
}),
Labels = Table.TransformRows(
Products,
each [Category] & ": " & [ProductName]
)
in
Labels
// Result: {"Electronics: Laptop", "Electronics: Mouse", "Furniture: Desk"}
{\"Electronics: Laptop\", \"Electronics: Mouse\", \"Furniture: Desk\"}
Table.AddColumn สร้างคอลัมน์ใหม่แล้วคืน Table กลับมา ส่วน Table.TransformRows แปลงแต่ละแถวแล้วคืน List ไม่ใช่ Table ใช้ Table.TransformRows ตอนต้อง Export ข้อมูล API หรือประมวลผลแบบแถวต่อแถว
Table.TransformRows ทำงานกับ Table ได้ใช้ [ColumnName] โดยตรง ส่วน List.Transform ทำงานกับ List และ (item) => expression List.Transform เหมาะสำหรับข้อมูลที่เป็น List อยู่แล้ว ส่วน Table.TransformRows เหมาะสำหรับ Table
ไม่จำเป็น Transform function สามารถคืน type ต่างๆ ได้ (text, number, record, list) ตัวอย่างเช่น each [Amount] คืน number ส่วน each [Name] & “:” & [ID] คืน text ได้
ความเร็วขึ้นอยู่กับความซับซ้อนของฟังก์ชัน transform ถ้าเรียบง่าย (แค่ [Column] หรือบวกลบ) ไม่ช้า แต่ถ้า transform มี lookup หรือเรียก API ในแต่ละแถว จะช้ามากๆ ลอง Table.AddColumn เพราะบาง transform สามารถใช้ได้ทั้งสองฟังก์ชัน
Table.TransformRows(table as table, transform as function) as list ใช้สำหรับแปลง Table เป็น List โดยประมวลผลแต่ละแถว
ที่เจ๋งของ Table.TransformRows คือมันสามารถดึงแค่คอลัมน์เดียว บวกค่าหลายคอลัมน์ หรือแปลงแต่ละแถวเป็น Record ใหม่อย่างสมบูรณ์ได้ ทีแรกฉันงง ว่าทำไมต้องแปลงเป็น List แทนที่จะใช้ Table.AddColumn ก็ได้ แต่พอเข้าใจแล้ว มันมีประโยชน์มากเวลา Export ข้อมูลไป API หรือเก็บเป็น JSON list แทนที่จะเป็นตารางครับ
ส่วนตัวผม Table.TransformRows มักใช้คู่กับ List.ToTable() เมื่อต้องการแปลง List of Records กลับเป็น Table เพื่อสร้างตารางชุดใหม่ที่มีโครงสร้างต่างจากตัวเดิม