---
title: Table.Last – ดึงข้อมูลแถวสุดท้าย
url: https://www.thepexcel.com/functions/power-query/table-functions/table-last/
type: function-explainer
program: Power Query
syntax: "Table.Last(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.Last – ดึงข้อมูลแถวสุดท้าย

> Table.Last ใช้ดึงแถวสุดท้ายของตารางออกมาเป็น Record หรือคืนค่าเริ่มต้นถ้าตารางว่างเปล่า

## คำอธิบาย

Table.Last ใช้ดึงแถวสุดท้ายของตารางออกมาเป็น Record หรือคืนค่าเริ่มต้นถ้าตารางว่างเปล่า

## Syntax

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

## Arguments

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

## ตัวอย่าง

### 1. ตัวอย่างพื้นฐาน - ดึงแถวสุดท้าย

```excel
let
    Source = Table.FromRecords({
        [CustomerID = 1, Name = "Bob", Sales = 100],
        [CustomerID = 2, Name = "Jim", Sales = 200],
        [CustomerID = 3, Name = "Paul", Sales = 300]
    }),
    LastRow = Table.Last(Source)
in
    LastRow
```

**ผลลัพธ์:** `[CustomerID = 3, Name = "Paul", Sales = 300]`

ดึงแถวสุดท้ายของตารางออกมาเป็น Record ที่มีค่า CustomerID = 3, Name = "Paul", Sales = 300

### 2. ตัวอย่างกับ Default Value

```excel
let
    EmptyTable = Table.FromRecords({}),
    Result = Table.Last(EmptyTable, [CustomerID = 0, Name = "No Data", Sales = 0])
in
    Result
```

**ผลลัพธ์:** `[CustomerID = 0, Name = "No Data", Sales = 0]`

เมื่อตารางว่างเปล่า ฟังก์ชันจะคืนค่า default value แทนแล้ว error ออกมา

### 3. การดึงค่าสำหรับการประมวลผลต่อ

```excel
let
    Source = Table.FromRecords({
        [Date = "2025-01-01", Price = 100],
        [Date = "2025-01-02", Price = 105],
        [Date = "2025-01-03", Price = 102]
    }),
    LastRecord = Table.Last(Source),
    LatestPrice = LastRecord[Price]
in
    LatestPrice
```

**ผลลัพธ์:** `102`

ดึงแถวสุดท้ายแล้วเข้าถึงคอลัมน์เฉพาะด้วย [ColumnName] เพื่อได้ค่าที่ต้องการ

### 4. ใช้กับ List.Last สำหรับเปรียบเทียบ

```excel
let
    Table1 = Table.FromRecords({
        [ID = 1, Value = 10],
        [ID = 2, Value = 20]
    }),
    List1 = {10, 20, 30},
    TableLastRecord = Table.Last(Table1),
    ListLastValue = List.Last(List1)
in
    {TableLastRecord, ListLastValue}
```

**ผลลัพธ์:** `{[ID = 2, Value = 20], 30}`

Table.Last ใช้กับ Table (คืน Record) ขณะที่ List.Last ใช้กับ List (คืนค่าเดี่ยว)

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

- ใช้ร่วมกับ Table.Sort() เพื่อดึงค่าสูงสุดหรือต่ำสุด: Table.Sort(Source, {"Price", Order.Descending}) |> Table.Last()

- ระบุ default value ที่มีความหมาย เช่น [Status = "No Data"] แทน null เพื่อให้โค้ดอ่านง่ายกว่า

- ใช้ as any ในการรับค่า return แล้วตรวจเช็คประเภทข้อมูล (type check) ก่อนการประมวลผลต่อ

- ถ้าต้องการหลายแถว ใช้ Table.LastN(table, count) แทน

- ใช้เป็นส่วนของการ chain operations: Source |> Table.Sort({"Date", Order.Descending}) |> Table.Last() เพื่อดึงข้อมูลล่าสุด

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

**Q: ถ้าตารางว่างเปล่าจะเกิดอะไร?**

ถ้าไม่ระบุ default value และตารางว่าง จะเกิด error "Expression.Error: The table is empty." แนวทางแก้คือระบุ default value เสมอเมื่อมีความเป็นไปได้ที่ตารางจะว่าง เช่น Table.Last(Source, null) หรือ Table.Last(Source, [DefaultRecord])

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

Table.First ดึงแถวแรก Table.Last ดึงแถวสุดท้าย ทั้งคู่คืนค่าเป็น Record และใช้ optional default value เหมือนกัน

**Q: ต่างกับ List.Last อย่างไร?**

Table.Last ทำงานกับตารางและคืน Record ส่วน List.Last ทำงานกับลิสต์และคืนค่าเดี่ยว (scalar value)

**Q: สามารถใช้ในการเรียงข้อมูลแบบ Sort แล้วดึงสุดท้ายได้ไหม?**

ได้ แนะนำให้ Sort ข้อมูลก่อน แล้วใช้ Table.Last เพื่อดึงค่าที่สูงสุดหรือต่ำสุด เช่น Table.Sort(Source, {"Price", Order.Descending}) |> Table.Last() จะได้ราคาสูงสุด

**Q: มีฟังก์ชันอื่นที่คล้ายไหม?**

ใช่ Table.FirstN, Table.LastN, Table.Max, Table.Min ล้วนใช้ในการเลือกหรือค้นหาข้อมูลในตารางเหมือนกัน

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

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

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

- [Microsoft Learn - Official Documentation](https://learn.microsoft.com/en-us/powerquery-m/table-last) _(official)_
- [PowerQuery.how - Community Reference](https://powerquery.how/table-last/) _(article)_
- [Table.FirstN & Table.LastN - Related Functions](https://learn.microsoft.com/en-us/powerquery-m/table-firstn) _(official)_

---

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