Thep Excel

Table.ToRecordsแปลงตารางเป็น List ของ Record

Table.ToRecords แปลงตารางให้เป็น List โดยแต่ละแถวกลายเป็น Record พร้อมชื่อคอลัมน์เป็น Field Name ทำให้สะดวกในการวนลูปแบบมีโครงสร้าง

fx
=Table.ToRecords(table) as list

ความนิยม6/10
ความยาก4/10
ประโยชน์7/10

อัปเดต 3 December 2025

Syntax & Arguments

อาร์กิวเมนต์ ชนิด คำอธิบาย
table table ตารางต้นทางที่ต้องการแปลงเป็น List ของ Record

Additional Notes

ฟังก์ชัน Table.ToRecords ใน Power Query ใช้สำหรับแปลงข้อมูลตารางให้เป็น List ของ Record โดยแต่ละ Record แทนหนึ่งแถวในตาราง และ Field Name ใน Record คือชื่อคอลัมน์ของตารางเดิม

ที่เจ๋งคือการแปลงนี้ช่วยให้เราสามารถทำงานกับข้อมูลแบบมีโครงสร้างได้ อย่างเช่นใช้ List.Transform เพื่อประมวลผลแต่ละแถว หรือใช้กับ Record.FieldNames เพื่อเข้าถึง Field ต่างๆ ได้ง่ายๆ ไม่ต้องจำหมายเลขคอลัมน์

ส่วนตัวผมใช้ Table.ToRecords บ่อยมากตอนต้องการทำงานที่ซับซ้อน เช่น สร้าง Custom Calculation ต่อแถว หรือจำเป็นต้อง Access ข้อมูลแบบมีชื่อ Field 😎

How it works

ประมวลผลข้อมูลทีละแถว

ใช้ List.Transform ร่วมกับ Table.ToRecords เพื่อประมวลผลข้อมูลแต่ละแถวด้วย Logic ที่ซับซ้อน

การสร้างข้อมูล JSON

โครงสร้าง Record ที่ได้จากฟังก์ชันนี้ใกล้เคียงกับ Object ใน JSON ทำให้ง่ายต่อการ Export เป็น JSON

Examples

ตัวอย่างที่ 1: แปลงตารางพื้นฐานlet SourceTable = Table.FromRecords({ [ProductID = 1, ProductName = "Laptop", Price = 25000], [ProductID = 2, ProductName = "Mouse", Price = 500], [ProductID =…

ตาราง 3 แถว ถูกแปลงเป็น List ของ 3 Records โดยแต่ละ Record มี Field ProductID, ProductName, Price ตามชื่อคอลัมน์เดิม

fx
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]
}
ตัวอย่างที่ 2: ใช้ร่วมกับ List.Transform เพื่อคำนวณแต่ละแถวlet Source = Table.FromRecords({ [Quantity = 10, UnitPrice = 100], [Quantity = 5, UnitPrice = 200], [Quantity = 3, UnitPrice = 500] }), RecordList = Table.ToRec…

ใช้ Table.ToRecords แปลงเป็น List แล้วทำ List.Transform เพื่อเพิ่ม Field Total ให้แต่ละ Record ด้วยการคูณ Quantity × UnitPrice

fx
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]
}
ตัวอย่างที่ 3: แยกเอา Field ที่ต้องการจาก Record Listlet Source = Table.FromRecords({ [Name = "Alice", Email = "alice@example.com", Department = "Sales"], [Name = "Bob", Email = "bob@example.com", Department = "IT…

ใช้ Table.ToRecords + List.Transform ทำให้เราเลือกเฉพาะ Field Name และ Email ที่ต้องการ โดยตัดทิ้ง Department

fx
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"]
}
ตัวอย่างที่ 4: ใช้ List.Select เพื่อกรองข้อมูลจาก Record Listlet Source = Table.FromRecords({ [StudentName = "John", Score = 85], [StudentName = "Jane", Score = 92], [StudentName = "Mike", Score = 78], [StudentName = "Sar…

แปลงตารางเป็น Record List แล้วใช้ List.Select เพื่อกรองเฉพาะ Record ที่ Score ≥ 85 ออกมา

fx
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]
}

FAQs

ผลลัพธ์ที่ได้เป็น List ของ Record ต่างจาก Table อย่างไร?+

List ของ Record ไม่มีชื่อคอลัมน์แสดง แต่ข้อมูลแต่ละ Record ยังมี Field Name ที่สามารถเข้าถึงแบบ [FieldName] ได้อยู่ ส่วนตาราง (Table) มีโครงสร้างแสดงชื่อคอลัมน์ชัดเจน ถ้าต้องการแสดงผลลัพธ์เป็นตารางอีกครั้งก็ใช้ Table.FromRecords กลับไป

Table.ToRecords ต่างจาก Table.ToRows อย่างไร?+

Table.ToRecords คืน List ของ Record (มีชื่อ Field) ส่วน Table.ToRows คืน List ของ List (เป็นเพียงค่าโดยไม่มีชื่อคอลัมน์) เมื่อใช้ Table.ToRows ต้องจำว่าสมาชิกลำดับที่ 1 คือคอลัมน์แรก ลำดับที่ 2 คือคอลัมน์ที่สอง เป็นต้น แต่ Table.ToRecords ทำให้สะดวกกว่าเพราะเข้าถึงแบบชื่อ

เมื่อไหร่ที่ต้องใช้ Table.ToRecords?+

ใช้เมื่อต้องการประมวลผลแต่ละแถวแบบ Custom Logic ที่ซับซ้อน เช่นคำนวณค่า, สร้าง Field ใหม่, เปลี่ยนชื่อ Field, หรือกรองข้อมูลแบบมีเงื่อนไขที่ต้องเข้าถึง Field หลายตัว เมื่อใช้ร่วมกับ List.Transform, List.Select, List.Select ทำให้สะดวกมากครับ

Resources & Related

ฟังก์ชันที่ผู้เขียนโยงไว้กับ Table.ToRecords จัดกลุ่มตามหมวด · ชี้ที่ชื่อใดชื่อหนึ่ง แล้วฟังก์ชันที่ใช้คู่กันจะมีจุดทอง

ฟังก์ชันนี้Table.ToRecordsTable functions
Table functions (1)
Table.ToRows

Table.ToRows แปลงตารางให้เป็น List ของ List โดยแต่ละ List ย่อยแทนข้อมูล 1 แถว ไม่มีชื่อคอลัมน์ เหมาะสำหรับส่งออกข้อมูลเป็น JSON/CSV หรือประมวลผลแถวโดยไม่สนใจชื่อฟิลด์

Comments

อีเมลของคุณจะไม่ถูกเผยแพร่