---
title: Table.FromRecords – สร้างตารางจากรายการ Record
url: https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/
type: function-explainer
program: Power Query
syntax: "Table.FromRecords(records as list, optional columns as any, optional missingField as nullable number) as table"
date: 2025-12-03
updated: 2025-12-23
scores:
  popularity: 6
  difficulty: 4
  usefulness: 7
---

# Table.FromRecords – สร้างตารางจากรายการ Record

> แปลง List ของ Record เป็นตาราง โดยแต่ละ Record กลายเป็นหนึ่งแถว เหมาะสำหรับข้อมูลจาก API หรือ JSON

## คำอธิบาย

แปลง List ของ Record เป็นตาราง โดยแต่ละ Record กลายเป็นหนึ่งแถว เหมาะสำหรับข้อมูลจาก API หรือ JSON

## Syntax

```excel
Table.FromRecords(records as list, optional columns as any, optional missingField as nullable number) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| records | Yes | List |  | รายการ Record ที่ต้องการแปลงเป็นตาราง เช่น {[ID=1, Name="Alice"], [ID=2, Name="Bob"]} |
| columns | No | Any | null | ระบุชื่อคอลัมน์ (List of Text) หรือ Type ของตาราง (type table [...]) เพื่อกำหนดลำดับคอลัมน์ตามที่ต้อง |
| missingField | No | Number | MissingField.Error | วิธีการจัดการฟิลด์ที่หายไปในบาง Record - MissingField.Error (ค่า default) หรือ MissingField.UseNull (เติม null) |

## ตัวอย่าง

### 1. สร้างตารางพื้นฐาน

```excel
let
    Source = Table.FromRecords({
        [ID = 1, Name = "Alice", Sales = 500],
        [ID = 2, Name = "Bob", Sales = 750],
        [ID = 3, Name = "Charlie", Sales = 600]
    })
in
    Source
```

**ผลลัพธ์:** `Table 3 rows, 3 columns (ID, Name, Sales)`

แปลง List ของ Record 3 ตัวเป็นตาราง โดยไม่ระบุ columns parameter ลำดับคอลัมน์อาจขึ้นอยู่กับลำดับการปรากฏตัวของฟิลด์

### 2. กำหนดลำดับคอลัมน์และ Type

```excel
let
    Source = Table.FromRecords({
        [ID = 1, Name = "Alice"],
        [ID = 2, Name = "Bob"]
    }, type table [Name = text, ID = number])
in
    Source
```

**ผลลัพธ์:** `Table (Name, ID) - ชื่อคอลัมน์อยู่ก่อน ID`

ใช้ columns parameter ด้วย type table เพื่อบังคับลำดับคอลัมน์ให้ Name มาก่อน ID และกำหนด Type ของแต่ละคอลัมน์

### 3. จัดการฟิลด์ที่หายไป

```excel
let
    Source = Table.FromRecords({
        [CustomerID = 1, FirstName = "Alice", Email = "alice@company.com"],
        [CustomerID = 2, FirstName = "Bob"],
        [CustomerID = 3, FirstName = "Charlie", Email = "charlie@company.com"]
    },
    type table [CustomerID = number, FirstName = text, Email = text],
    MissingField.UseNull)
in
    Source
```

**ผลลัพธ์:** `Table 3 rows - แถวที่ 2 มี null ในคอลัมน์ Email`

Record ที่ 2 ไม่มี Email field จะแสดงเป็น null แทน ซึ่ง MissingField.UseNull ช่วยให้ไม่เกิด Error

### 4. สร้างจากข้อมูล JSON API

```excel
let
    JsonData = Json.Document(Web.Contents("https://api.example.com/users")),
    RecordsList = JsonData[data],
    Table = Table.FromRecords(
        RecordsList,
        type table [id = number, username = text, email = text],
        MissingField.UseNull
    )
in
    Table
```

**ผลลัพธ์:** `Table ของผู้ใช้จาก API`

รับข้อมูล JSON จาก API, แปลงเป็น Record List, แล้วสร้างตาราง โดยจัดการฟิลด์ที่อาจหายไป

## หมายเหตุเพิ่มเติม

- ถ้าต้องการให้ Record หลายๆ ตัวมีฟิลด์ครบ ใช้ List.Transform หรือ Record.AddField ก่อนเพื่อ normalize

- ใช้ type table ที่ชัดเจน จะช่วยให้ debug ง่าย และไม่สับสนกับลำดับคอลัมน์

- ถ้าข้อมูลมาจาก API ให้ confirm ว่า schema มีคุณสมบัติครบก่อน (หรือใช้ MissingField.UseNull)

- หากต้องการเปลี่ยนชื่อฟิลด์ใน Record ใช้ List.Transform + Record.RenameFields ก่อนสร้างตาราง

- คำนึงถึง Data Type - ถ้าฟิลด์ควรเป็น text หรือ number ให้ระบุใน type table เพื่อ Type checking

## คำถามที่พบบ่อย

**Q: ต่างกันระหว่าง Table.FromRecords กับ Table.FromRows ยังไง?**

Table.FromRecords ใช้กับ List of Record (named fields) เช่น {[Name="Alice", Age=25]} ส่วน Table.FromRows ใช้กับ List of List (positional values) เช่น {{"Alice", 25}} เลือกตามรูปแบบข้อมูลที่มี

**Q: ถ้า Record ตัวหนึ่งไม่มีฟิลด์ที่ Record อื่นมี จะเกิดอะไร?**

ตามค่า default (MissingField.Error) จะเกิด Error หากต้องให้มันเติม null แทน ให้ใช้ MissingField.UseNull

**Q: ลำดับคอลัมน์ไม่ตรงตามที่ต้องการ ต้องทำยังไง?**

ใช้ columns parameter โดยระบุ type table เช่น type table [ColA = text, ColB = number] จะบังคับลำดับตามลำดับการประกาศ

**Q: เวอร์ชัน Power Query ไหนรองรับ Table.FromRecords?**

สองพารามิเตอร์แรก (records, columns) มีจากแรก ส่วน missingField parameter เพิ่มมาในเวอร์ชันหลัง (Power Query 2016 ขึ้นไป)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn - Official Documentation](https://learn.microsoft.com/en-us/powerquery-m/table-fromrecords) _(official)_
- [PowerQuery.how - Table.FromRecords Tutorial](https://powerquery.how/table-fromrecords/) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/](https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/)_
