Thep Excel

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

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

=RowExpression.From(function as function) as record

By ThepExcel AI Agent
15 December 2025

Function Metrics


Popularity
5/10

Difficulty
4/10

Usefulness
6/10

Syntax & Arguments

=RowExpression.From(function as function) as record

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

How it works

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

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

Examples

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

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

Result:

[
Kind = "Binary",
Operator = "Equals",
Left = RowExpression.Column("CustomerName"),
Right = [Kind = "Constant", Value = "ALFKI"]
]

ตัวอย่างที่ 2: แปลงนิพจน์คำนวณและเปรียบเทียบ (Arithmetic + Comparison)
RowExpression.From(each [Qty] * [Price] > 1000)
นิพจน์ที่ซับซ้อนกว่าจะแปลงเป็น nested AST โดยโครงสร้างสะท้อนลำดับการประเมิน (evaluation order) คูณ Qty กับ Price ก่อน แล้วเปรียบเทียบกับ 1000
Power Query Formula:

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

Result:

[
Kind = "Binary",
Operator = "GreaterThan",
Left = [
Kind = "Binary",
Operator = "Multiply",
Left = RowExpression.Column("Qty"),
Right = RowExpression.Column("Price")
],
Right = [Kind = "Constant", Value = 1000]
]

ตัวอย่างที่ 3: แปลงนิพจน์แบบ if/then/else
let Expr = each if [IsActive] then [Score] else 0 in RowExpression.From(Expr)
ถ้านิพจล์มีเงื่อนไข if/then/else ผลลัพธ์ AST จะมีโหนด Kind = "If" โดยแยกเป็น Condition, ThenExpression และ ElseExpression ซึ่งช่วยให้ระบบภายในนำไปวิเคราะห์หรือแปลงต่อได้ง่าย
Power Query Formula:

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

Result:

[
Kind = "If",
Condition = RowExpression.Column("IsActive"),
ThenExpression = RowExpression.Column("Score"),
ElseExpression = [Kind = "Constant", Value = 0]
]

ตัวอย่างที่ 4: แปลงนิพจน์ Invocation (Function Call)
RowExpression.From(each Text.Length([ProductName]) > 10)
เมื่อใช้ฟังก์ชันอื่น (เช่น Text.Length) ภายในนิพจน์ AST จะมีโหนด Kind = "Invocation" ที่บันทึกชื่อฟังก์ชันและ arguments
Power Query Formula:

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

Result:

[
Kind = "Binary",
Operator = "GreaterThan",
Left = [
Kind = "Invocation",
Function = "Text.Length",
Arguments = {RowExpression.Column("ProductName")}
],
Right = [Kind = "Constant", Value = 10]
]

FAQs

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

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

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

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

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

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

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

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

Resources & Related

Additional Notes

RowExpression.From แปลงฟังก์ชัน lambda (เช่น each [Column] = “ALFKI”) ให้เป็น AST (abstract syntax tree) ที่เก็บโครงสร้างของนิพจน์ไว้เป็น record โดยระบบจะ normalize ให้เป็นรูปแบบมาตรฐาน คือแทนที่ parameter reference ด้วย RowExpression.Row และ column reference ด้วย RowExpression.Column

ที่เจ๋งคือ ได้ AST ออกมาแล้วคุณสามารถ “อ่าน” โครงสร้างเงื่อนไขได้โดยโปรแกรม ไม่ใช่แค่ข้อความธรรมดา เช่น ดูได้ว่า operator ที่ใช้คืออะไร เงื่อนไขประกอบด้วยส่วนไหนบ้าง จำนวนความลึกของการซ้อน ฯลฯ เพื่อให้คุณสร้างเครื่องมือที่ “เข้าใจ” เงื่อนไขอย่างแท้จริง

ส่วนตัวผมแล้ว ฟังก์ชันนี้มีประโยชน์กับการสร้าง query builder, optimizer, หรือ analyzer ที่ต้อง “คิด” เกี่ยวกับโครงสร้างเงื่อนไข แต่ไม่ค่อยใช้ในงานแปลงข้อมูลแบบปกตินั่นแหละ 😎

Leave a Reply

Your email address will not be published. Required fields are marked *