---
title: Table.First – ดึงข้อมูลแถวแรก
url: https://www.thepexcel.com/functions/power-query/table-functions/table-first/
type: function-explainer
program: Power Query
syntax: "Table.First(table as table, optional default as any) as any"
date: 2025-12-03
updated: 2025-12-24
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# Table.First – ดึงข้อมูลแถวแรก

> ดึงแถวแรก (Record) จากตารางข้อมูล พร้อมตัวเลือกค่า default สำหรับกรณีตารางว่าง

## คำอธิบาย

ดึงแถวแรก (Record) จากตารางข้อมูล พร้อมตัวเลือกค่า default สำหรับกรณีตารางว่าง

## Syntax

```excel
Table.First(table as table, optional default as any) as any
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table | Yes | Table |  | ตารางข้อมูลที่ต้องการดึงแถวแรก |
| default | No | Any | null | ค่าที่จะคืนกลับถ้าตารางว่างเปล่า ถ้าไม่ระบุและตารางว่าง จะคืนค่า error จึงควรใส่ default เสมอในการใช้งานจริง |

## ตัวอย่าง

### 1. ดึงแถวแรกจากตารางปกติ

```excel
let
    Sales = Table.FromRecords({
        [OrderID = 1, Customer = "Alice", Amount = 5000],
        [OrderID = 2, Customer = "Bob", Amount = 3200],
        [OrderID = 3, Customer = "Charlie", Amount = 4500]
    }),
    FirstOrder = Table.First(Sales)
in
    FirstOrder
```

**ผลลัพธ์:** `[OrderID = 1, Customer = "Alice", Amount = 5000]`

ดึงแถวแรกสุด (Record) จากตาราง Sales ได้ OrderID=1 ของ Alice

### 2. ดึงแถวแรกพร้อม default value สำหรับตารางว่าง

```excel
let
    EmptyTable = Table.FromRecords({}),
    DefaultRecord = [OrderID = 0, Customer = "No Data", Amount = 0],
    Result = Table.First(EmptyTable, DefaultRecord)
in
    Result
```

**ผลลัพธ์:** `[OrderID = 0, Customer = "No Data", Amount = 0]`

ตารางว่าง ดังนั้น Table.First คืนค่า default ที่ระบุไว้แทนที่จะเกิด error

### 3. ดึงค่าจาก Field ในแถวแรก

```excel
let
    Employees = Table.FromRecords({
        [Name = "David", Position = "Manager", Salary = 80000],
        [Name = "Emma", Position = "Analyst", Salary = 50000],
        [Name = "Frank", Position = "Developer", Salary = 65000]
    }),
    FirstRow = Table.First(Employees),
    FirstName = FirstRow[Name]
in
    FirstName
```

**ผลลัพธ์:** `"David"`

ดึงแถวแรกเป็น Record แล้วเข้าถึง Field Name ได้ค่า "David"

### 4. ใช้ Table.First หลังจากการกรองข้อมูล

```excel
let
    Products = Table.FromRecords({
        [SKU = "A001", Category = "Fruits", Price = 30],
        [SKU = "B002", Category = "Vegetables", Price = 20],
        [SKU = "A002", Category = "Fruits", Price = 45]
    }),
    FilteredFruits = Table.SelectRows(Products, each [Category] = "Fruits"),
    FirstFruit = Table.First(FilteredFruits, [SKU = "EMPTY", Price = 0])
in
    FirstFruit
```

**ผลลัพธ์:** `[SKU = "A001", Category = "Fruits", Price = 30]`

กรองเฉพาะสินค้าประเภท Fruits แล้วดึงแถวแรก ถ้าไม่มี Fruits จะคืน default

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

- ใช้ Table.First ร่วมกับ Table.SelectRows เพื่อดึงแถวแรกที่ตรงเงื่อนไข

- หากต้อง Error Handling ให้ใช้ try-otherwise: try Table.First(Source) otherwise [empty = 0]

- ใส่ default value เสมอ แม้ว่าแน่ใจว่าตารางจะไม่ว่าง (Code style ที่ดี)

- ถ้าต้องดึงหลายแถวแรก ใช้ Table.FirstN ที่ return Table ซึ่งอ่านออกกว่า

- สามารถเชื่อม Field ได้เลย: Table.First(Source)[FieldName] ได้ค่า Field ของแถวแรก

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

**Q: ต่างกันยังไง Table.First กับ Table.FirstN?**

Table.First คืนค่า Record (แถว 1 รายการ) ส่วน Table.FirstN คืนค่า Table ที่มีหลายแถว เช่น Table.FirstN(tbl, 5) คืนตารางแถวแรก 5 แถว ถ้าต้องการแถวเดียว ใช้ Table.First ประหยัดกว่า

**Q: ถ้าตารางว่างแต่ไม่มี default จะเป็นไง?**

จะเกิด error "Expression.Error: We cannot find a row to return" ดังนั้นหากไม่แน่ใจว่าตารางจะมีข้อมูล ควรใส่ default เสมอ

**Q: Table.First ใช้ได้กับ List ไหม?**

ไม่ได้ Table.First ใช้ได้เฉพาะ Table type เท่านั้น ถ้าต้องการดึง Element แรกจาก List ให้ใช้ List.First แทน

**Q: แล้ว Table.Last ล่ะ ทำอะไร?**

ทำหน้าที่เดียวกับ Table.First แต่ดึงแถวสุดท้าย (Last row) ของตารางแทน

**Q: ถ้าต้องการดึง Field ที่เป็น Number ก่อนใน Default จะทำไง?**

ก็ระบุค่า Number ใน default ตรงๆ เช่น Table.First(tbl, 0) หรือ Table.First(tbl, null) ขึ้นอยู่กับว่าต้องการคืนค่าอะไร

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.Last – ดึงข้อมูลแถวสุดท้าย](https://www.thepexcel.com/functions/power-query/table-functions/table-last/)
- [Table.FirstN – ดึงแถวแรกตามจำนวนหรือเงื่อนไข](https://www.thepexcel.com/?post_type=function-explainer&p=37952)
- [List.First – ดึงสมาชิกตัวแรกจาก List](https://www.thepexcel.com/functions/power-query/list-functions/list-first/)

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

- [Microsoft Learn - Official Documentation](https://learn.microsoft.com/en-us/powerquery-m/table-first) _(official)_
- [PowerQuery.how - M Language Reference](https://powerquery.how/table-first/) _(article)_

---

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