RowExpression.From คืนค่า AST (abstract syntax tree) ของตัวฟังก์ชันที่ส่งเข้า โดยทำให้เป็น “row expression” ที่มีรูปแบบมาตรฐาน เหมาะสำหรับงานภายใน (internal) และการวิเคราะห์/แปลงนิพจน์ เช่น เงื่อนไขที่ใช้กรองแถว (each [Col] = “X”)
=RowExpression.From(function as function) as record
=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 |
ใช้ RowExpression.From เพื่อดูโครงสร้างของนิพจน์ (AST) เมื่อจำเป็นต้องตรวจสอบ/แปลงเงื่อนไขในระดับโครงสร้างข้อมูล
RowExpression.From(each [CustomerName] = "ALFKI")=RowExpression.From(each [CustomerName] = "ALFKI")
[
Kind = "Binary",
Operator = "Equals",
Left = RowExpression.Column("CustomerName"),
Right = [Kind = "Constant", Value = "ALFKI"]
]
RowExpression.From(each [Qty] * [Price] > 1000)=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]
]
let Expr = each if [IsActive] then [Score] else 0 in RowExpression.From(Expr)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]
]
RowExpression.From(each Text.Length([ProductName]) > 10)=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]
]
จริงๆ นี่คือฟังก์ชนสำหรับผู้พัฒนาเท่านั้น ใช้แค่เมื่อคุณต้องการ “ตรวจสอบ” โครงสร้างเงื่อนไขจากภายใน สำหรับงานแปลงข้อมูลแบบปกติ (ส่วนใหญ่) คุณใช้ Table.SelectRows ตรงตัวได้เลย ไม่ต้องมาคิดถึง AST
ไม่ต่างกันหรอก ฟังก์ชันทั้งคู่ทำงานเหมือนๆ กัน ต่างกันแค่ชื่อเท่านั้น เลือก RowExpression.From สำหรับแถว (row) และ ItemExpression.From สำหรับรายการ (item) เพื่อให้โค้ดของคุณอ่านเข้าใจง่ายกว่านั่นแหละ
RowExpression.From สร้าง AST ด้วย node type พื้นฐานเหล่านี้: Constant (ค่าตัวเลข/ข้อความ), Invocation (เรียกฟังก์ชัน), Unary (ลบ, not), Binary (บวก, คูณ, เปรียบเทียบ), If (เงื่อนไข if/then/else), และ FieldAccess (อ้างถึง column) ก็เหล่านี้แหละครับ
ฟังก์ชัน lambda ต้องมี argument เดียวและ valid ตัวอักษร ถ้าส่ง invalid lambda เข้าไป Power Query จะ error ครับ ทำการทดสอบฟังก์ชัน lambda ของคุณก่อนสักครั้งเพื่อให้แน่ใจว่า work
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 ที่ต้อง “คิด” เกี่ยวกับโครงสร้างเงื่อนไข แต่ไม่ค่อยใช้ในงานแปลงข้อมูลแบบปกตินั่นแหละ 😎