---
title: RowExpression.From – สร้าง Row Expression (AST) จากฟังก์ชัน
url: https://www.thepexcel.com/functions/power-query/table-functions/rowexpression-from/
type: function-explainer
program: Power Query
syntax: RowExpression.From(function as function) as record
date: 2025-12-15
updated: 2025-12-25
scores:
  popularity: 5
  difficulty: 4
  usefulness: 6
---

# RowExpression.From – สร้าง Row Expression (AST) จากฟังก์ชัน

> RowExpression.From คืนค่า AST (abstract syntax tree) ของตัวฟังก์ชันที่ส่งเข้า โดยทำให้เป็น "row expr

## คำอธิบาย

RowExpression.From คืนค่า AST (abstract syntax tree) ของตัวฟังก์ชันที่ส่งเข้า โดยทำให้เป็น "row expression" ที่มีรูปแบบมาตรฐาน เหมาะสำหรับงานภายใน (internal) และการวิเคราะห์/แปลงนิพจน์ เช่น เงื่อนไขที่ใช้กรองแถว (each [Col] = "X")

## Syntax

```excel
RowExpression.From(function as function) as record
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| function | Yes | function |  | ฟังก์ชัน lambda แบบ 1-argument (เช่น each [Column] = "ALFKI") ที่ต้องการแปลงเป็น AST ค่าพารามิเตอร์จะถูกแทนที่ด้วย RowExpression.Row และ column reference จะถูกแทนที่ด้วย RowExpression.Column |

## เคสการใช้งาน

### วิเคราะห์เงื่อนไขกรองแถวแบบภายใน

ใช้ RowExpression.From เพื่อดูโครงสร้างของนิพจน์ (AST) เมื่อจำเป็นต้องตรวจสอบ/แปลงเงื่อนไขในระดับโครงสร้างข้อมูล

_เหมาะกับ:_ internal-ast

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: แปลงเงื่อนไขเท่ากับ (Equals)

```excel
RowExpression.From(each [CustomerName] = "ALFKI")
```

**ผลลัพธ์:** `[
    Kind = "Binary",
    Operator = "Equals",
    Left = RowExpression.Column("CustomerName"),
    Right = [Kind = "Constant", Value = "ALFKI"]
]`

ส่ง lambda (each [CustomerName] = "ALFKI") เข้าไป RowExpression.From จะได้ record ที่มีโครงสร้าง Binary expression โดย Left คือ column reference และ Right คือค่าคงที่ "ALFKI" ที่ใช้เปรียบเทียบ

### 2. ตัวอย่างที่ 2: แปลงนิพจน์คำนวณและเปรียบเทียบ (Arithmetic + Comparison)

```excel
RowExpression.From(each [Qty] * [Price] > 1000)
```

**ผลลัพธ์:** `[
    Kind = "Binary",
    Operator = "GreaterThan",
    Left = [
        Kind = "Binary",
        Operator = "Multiply",
        Left = RowExpression.Column("Qty"),
        Right = RowExpression.Column("Price")
    ],
    Right = [Kind = "Constant", Value = 1000]
]`

นิพจน์ที่ซับซ้อนกว่าจะแปลงเป็น nested AST โดยโครงสร้างสะท้อนลำดับการประเมิน (evaluation order) คูณ Qty กับ Price ก่อน แล้วเปรียบเทียบกับ 1000

### 3. ตัวอย่างที่ 3: แปลงนิพจน์แบบ if/then/else

```excel
let Expr = each if [IsActive] then [Score] else 0 in RowExpression.From(Expr)
```

**ผลลัพธ์:** `[
    Kind = "If",
    Condition = RowExpression.Column("IsActive"),
    ThenExpression = RowExpression.Column("Score"),
    ElseExpression = [Kind = "Constant", Value = 0]
]`

ถ้านิพจล์มีเงื่อนไข if/then/else ผลลัพธ์ AST จะมีโหนด Kind = "If" โดยแยกเป็น Condition, ThenExpression และ ElseExpression ซึ่งช่วยให้ระบบภายในนำไปวิเคราะห์หรือแปลงต่อได้ง่าย

### 4. ตัวอย่างที่ 4: แปลงนิพจน์ Invocation (Function Call)

```excel
RowExpression.From(each Text.Length([ProductName]) > 10)
```

**ผลลัพธ์:** `[
    Kind = "Binary",
    Operator = "GreaterThan",
    Left = [
        Kind = "Invocation",
        Function = "Text.Length",
        Arguments = {RowExpression.Column("ProductName")}
    ],
    Right = [Kind = "Constant", Value = 10]
]`

เมื่อใช้ฟังก์ชันอื่น (เช่น Text.Length) ภายในนิพจน์ AST จะมีโหนด Kind = "Invocation" ที่บันทึกชื่อฟังก์ชันและ arguments

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

- จับกู่ RowExpression.From กับ RowExpression.Column ทำให้คุณสามารถ "อ่าน" และ "แปลง" โครงสร้างเงื่อนไขได้อย่างลึกลง

- อยากเห็น AST ออกมาชัดเจนมั้ย ใช้ JSON.Document กับผลลัพธ์ของ RowExpression.From ดีกว่า record ดูเป็นรูปแบบ JSON ได้ชัดเจน

- สร้าง query analyzer, query validator, หรือ query optimizer ด้วยฟังก์ชันนี้ได้ครับ ลองสร้าง utility ที่ "เข้าใจ" เงื่อนไขของผู้ใช้แบบลึก

- ItemExpression.From ทำงานเหมือนกัน ต่างกันแค่ชื่อ หากต้องการใช้ร่วม ให้เลือกชื่อที่ตรงกับบริบทของคุณ (row vs item) เท่านั้น

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

**Q: RowExpression.From เหมาะกับผู้ใช้ทั่วไปไหม?**

จริงๆ นี่คือฟังก์ชนสำหรับผู้พัฒนาเท่านั้น ใช้แค่เมื่อคุณต้องการ "ตรวจสอบ" โครงสร้างเงื่อนไขจากภายใน สำหรับงานแปลงข้อมูลแบบปกติ (ส่วนใหญ่) คุณใช้ Table.SelectRows ตรงตัวได้เลย ไม่ต้องมาคิดถึง AST

**Q: RowExpression.From กับ ItemExpression.From ต่างกันไหม?**

ไม่ต่างกันหรอก ฟังก์ชันทั้งคู่ทำงานเหมือนๆ กัน ต่างกันแค่ชื่อเท่านั้น เลือก RowExpression.From สำหรับแถว (row) และ ItemExpression.From สำหรับรายการ (item) เพื่อให้โค้ดของคุณอ่านเข้าใจง่ายกว่านั่นแหละ

**Q: AST ประกอบด้วย Node type อะไรบ้าง?**

RowExpression.From สร้าง AST ด้วย node type พื้นฐานเหล่านี้: Constant (ค่าตัวเลข/ข้อความ), Invocation (เรียกฟังก์ชัน), Unary (ลบ, not), Binary (บวก, คูณ, เปรียบเทียบ), If (เงื่อนไข if/then/else), และ FieldAccess (อ้างถึง column) ก็เหล่านี้แหละครับ

**Q: ถ้านิพจล์มี error หรือไม่ valid จะเกิดอะไร?**

ฟังก์ชัน lambda ต้องมี argument เดียวและ valid ตัวอักษร ถ้าส่ง invalid lambda เข้าไป Power Query จะ error ครับ ทำการทดสอบฟังก์ชัน lambda ของคุณก่อนสักครั้งเพื่อให้แน่ใจว่า work

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

- [RowExpression.Column – จัดการตาราง](https://www.thepexcel.com/?post_type=function-explainer&p=37931)
- [ItemExpression.From – จัดการตาราง](https://www.thepexcel.com/?post_type=function-explainer&p=37930)

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

- [Microsoft Learn: RowExpression.From](https://learn.microsoft.com/en-us/powerquery-m/rowexpression-from) _(official)_
- [Microsoft Learn: RowExpression.Column](https://learn.microsoft.com/en-us/powerquery-m/rowexpression-column) _(official)_
- [Microsoft Learn: ItemExpression.From](https://learn.microsoft.com/en-us/powerquery-m/itemexpression-from) _(official)_

---

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