Table.ToRecords แปลงตารางให้เป็น List โดยแต่ละแถวกลายเป็น Record พร้อมชื่อคอลัมน์เป็น Field Name ทำให้สะดวกในการวนลูปแบบมีโครงสร้าง
=Table.ToRecords(table as table) as list
=Table.ToRecords(table as table) as list
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางต้นทางที่ต้องการแปลงเป็น List ของ Record |
ใช้ List.Transform ร่วมกับ Table.ToRecords เพื่อประมวลผลข้อมูลแต่ละแถวด้วย Logic ที่ซับซ้อน
โครงสร้าง Record ที่ได้จากฟังก์ชันนี้ใกล้เคียงกับ Object ใน JSON ทำให้ง่ายต่อการ Export เป็น JSON
let SourceTable = Table.FromRecords({ [ProductID = 1, ProductName = "Laptop", Price = 25000], [ProductID = 2, ProductName = "Mouse", Price = 500], [ProductID =…let
SourceTable = Table.FromRecords({
[ProductID = 1, ProductName = "Laptop", Price = 25000],
[ProductID = 2, ProductName = "Mouse", Price = 500],
[ProductID = 3, ProductName = "Keyboard", Price = 2000]
}),
AsRecordList = Table.ToRecords(SourceTable)
in
AsRecordList
{
[ProductID = 1, ProductName = "Laptop", Price = 25000],
[ProductID = 2, ProductName = "Mouse", Price = 500],
[ProductID = 3, ProductName = "Keyboard", Price = 2000]
}
let Source = Table.FromRecords({ [Quantity = 10, UnitPrice = 100], [Quantity = 5, UnitPrice = 200], [Quantity = 3, UnitPrice = 500] }), RecordList = Table.ToRec…let
Source = Table.FromRecords({
[Quantity = 10, UnitPrice = 100],
[Quantity = 5, UnitPrice = 200],
[Quantity = 3, UnitPrice = 500]
}),
RecordList = Table.ToRecords(Source),
WithTotal = List.Transform(RecordList, each _ & [Total = [Quantity] * [UnitPrice]])
in
WithTotal
{
[Quantity = 10, UnitPrice = 100, Total = 1000],
[Quantity = 5, UnitPrice = 200, Total = 1000],
[Quantity = 3, UnitPrice = 500, Total = 1500]
}
let Source = Table.FromRecords({ [Name = "Alice", Email = "alice@example.com", Department = "Sales"], [Name = "Bob", Email = "bob@example.com", Department = "IT…let
Source = Table.FromRecords({
[Name = "Alice", Email = "alice@example.com", Department = "Sales"],
[Name = "Bob", Email = "bob@example.com", Department = "IT"]
}),
RecordList = Table.ToRecords(Source),
NameAndEmail = List.Transform(RecordList, each [Name = [Name], Email = [Email]])
in
NameAndEmail
{
[Name = "Alice", Email = "alice@example.com"],
[Name = "Bob", Email = "bob@example.com"]
}
let Source = Table.FromRecords({ [StudentName = "John", Score = 85], [StudentName = "Jane", Score = 92], [StudentName = "Mike", Score = 78], [StudentName = "Sar…let
Source = Table.FromRecords({
[StudentName = "John", Score = 85],
[StudentName = "Jane", Score = 92],
[StudentName = "Mike", Score = 78],
[StudentName = "Sarah", Score = 88]
}),
RecordList = Table.ToRecords(Source),
HighScores = List.Select(RecordList, each [Score] >= 85)
in
HighScores
{
[StudentName = "John", Score = 85],
[StudentName = "Jane", Score = 92],
[StudentName = "Sarah", Score = 88]
}
List ของ Record ไม่มีชื่อคอลัมน์แสดง แต่ข้อมูลแต่ละ Record ยังมี Field Name ที่สามารถเข้าถึงแบบ [FieldName] ได้อยู่ ส่วนตาราง (Table) มีโครงสร้างแสดงชื่อคอลัมน์ชัดเจน ถ้าต้องการแสดงผลลัพธ์เป็นตารางอีกครั้งก็ใช้ Table.FromRecords กลับไป
Table.ToRecords คืน List ของ Record (มีชื่อ Field) ส่วน Table.ToRows คืน List ของ List (เป็นเพียงค่าโดยไม่มีชื่อคอลัมน์) เมื่อใช้ Table.ToRows ต้องจำว่าสมาชิกลำดับที่ 1 คือคอลัมน์แรก ลำดับที่ 2 คือคอลัมน์ที่สอง เป็นต้น แต่ Table.ToRecords ทำให้สะดวกกว่าเพราะเข้าถึงแบบชื่อ
ใช้เมื่อต้องการประมวลผลแต่ละแถวแบบ Custom Logic ที่ซับซ้อน เช่นคำนวณค่า, สร้าง Field ใหม่, เปลี่ยนชื่อ Field, หรือกรองข้อมูลแบบมีเงื่อนไขที่ต้องเข้าถึง Field หลายตัว เมื่อใช้ร่วมกับ List.Transform, List.Select, List.Select ทำให้สะดวกมากครับ
ฟังก์ชัน Table.ToRecords ใน Power Query ใช้สำหรับแปลงข้อมูลตารางให้เป็น List ของ Record โดยแต่ละ Record แทนหนึ่งแถวในตาราง และ Field Name ใน Record คือชื่อคอลัมน์ของตารางเดิม
ที่เจ๋งคือการแปลงนี้ช่วยให้เราสามารถทำงานกับข้อมูลแบบมีโครงสร้างได้ อย่างเช่นใช้ List.Transform เพื่อประมวลผลแต่ละแถว หรือใช้กับ Record.FieldNames เพื่อเข้าถึง Field ต่างๆ ได้ง่ายๆ ไม่ต้องจำหมายเลขคอลัมน์
ส่วนตัวผมใช้ Table.ToRecords บ่อยมากตอนต้องการทำงานที่ซับซ้อน เช่น สร้าง Custom Calculation ต่อแถว หรือจำเป็นต้อง Access ข้อมูลแบบมีชื่อ Field 😎