---
title: Expression.Evaluate – ประเมินผลนิพจน์ M แบบไดนามิก
url: https://www.thepexcel.com/functions/power-query/expression-functions/expression-evaluate/
type: function-explainer
program: Power Query
syntax: "Expression.Evaluate(document as text, optional environment as nullable record) as any"
date: 2025-12-12
updated: 2025-12-20
scores:
  popularity: 4
  difficulty: 9
  usefulness: 6
---

# Expression.Evaluate – ประเมินผลนิพจน์ M แบบไดนามิก

> Expression.Evaluate ใช้สำหรับประเมินผลนิพจน์ M ที่เก็บไว้เป็นข้อความ โดยสามารถระบุตัวแปรและฟังก์ชันท

## คำอธิบาย

Expression.Evaluate ใช้สำหรับประเมินผลนิพจน์ M ที่เก็บไว้เป็นข้อความ โดยสามารถระบุตัวแปรและฟังก์ชันที่ใช้ได้ในสภาพแวดล้อม ทำให้สามารถเขียนโค้ด M แบบไดนามิก

## Syntax

```excel
Expression.Evaluate(document as text, optional environment as nullable record) as any
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| document | Yes | text |  | นิพจน์ M ที่เก็บไว้เป็นข้อความสตริง ต้องเป็นโค้ด M ที่ถูกต้องตามไวยากรณ์ |
| environment | No | record | null | record ที่ประกอบด้วยตัวแปรและฟังก์ชันที่ต้องการให้เข้าถึงได้ในนิพจน์ (เช่น [x = 5, y = 10] หรือ #shared สำหรับสภาพแวดล้อมทั่วโลก) |

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: การคำนวณแบบง่าย

```excel
= Expression.Evaluate("1 + 1")
```

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

ประเมินผลการบวกแบบง่าย โดยไม่ต้องมีสภาพแวดล้อม ผลลัพธ์คือเลข 2

### 2. ตัวอย่างที่ 2: ใช้ตัวแปรจาก environment

```excel
= Expression.Evaluate("x + y", [x = 17, y = 25])
```

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

ส่งตัวแปร x และ y ผ่าน environment record เพื่อใช้ในนิพจน์ ผลลัพธ์คือ 42

### 3. ตัวอย่างที่ 3: เรียกใช้ฟังก์ชัน List.Sum

```excel
= Expression.Evaluate("List.Sum({1, 2, 3})", [List.Sum = List.Sum])
```

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

ถ้าไม่ส่ง List.Sum ในสภาพแวดล้อม จะเกิดข้อผิดพลาด 'List.Sum' doesn't exist ต้องใส่ [List.Sum = List.Sum] เพื่อให้ฟังก์ชันสามารถใช้ได้

### 4. ตัวอย่างที่ 4: ใช้ #shared สำหรับสภาพแวดล้อมทั่วโลก

```excel
= Expression.Evaluate("List.Sum({5, 10, 15})", #shared)
```

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

#shared ให้เข้าถึงฟังก์ชัน Power Query ทั้งหมด (global environment) ทำให้ไม่ต้องเขียน [List.Sum = List.Sum] ซ้ำ

### 5. ตัวอย่างที่ 5: let expression แบบไดนามิก

```excel
= Expression.Evaluate("let a=3, b=4, c=a*b in c")
```

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

ประเมินผล let...in expression ที่เก็บไว้เป็นข้อความ ผลลัพธ์คือ 12 (3 * 4) ทำให้สามารถสร้างโค้ด M ที่มีการประกาศตัวแปรแบบไดนามิก

### 6. ตัวอย่างที่ 6: สตริงต่อเนื่องกัน

```excel
= Expression.Evaluate(
    Expression.Constant("abc") & " & " & Expression.Identifier("x"), 
    [x = "def"]
  )
```

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

ใช้ Expression.Constant() เพื่อแปลงค่าเป็นโค้ด M และ Expression.Identifier() สำหรับตัวแปร จากนั้นต่อเข้าด้วยกัน ผลลัพธ์คือสตริง "abcdef"

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

- ใช้ #shared ถ้าต้องการให้นิพจน์เข้าถึงฟังก์ชัน Power Query ทั่วโลก ง่ายกว่าการเขียน [FunctionName = FunctionName] ซ้ำๆ

- Expression.Constant() ใช้สำหรับแปลงค่าเป็นโค้ด M ที่ถูกต้องตามไวยากรณ์ เช่น Expression.Constant("abc") ได้ "\"abc\"" (เพราะข้อความต้องมีอัญประลัญ)

- Expression.Identifier() ใช้สำหรับสร้างชื่อตัวแปรหรือฟังก์ชันแบบไดนามิก

- ระวังประสิทธิภาพ: Expression.Evaluate ช้ากว่าโค้ด M ที่เขียนตรงๆ ใช้เฉพาะเมื่อต้องการสร้างนิพจน์แบบไดนามิก

- สามารถเก็บ M expression ในไฟล์ (เช่น Custom function) แล้วเรียกใช้ผ่าน Expression.Evaluate ได้ มีประโยชน์ในการนำโค้ดกลับมาใช้

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

**Q: Expression.Evaluate ต้องใช้ environment parameter เสมอหรือ**

ไม่จำเป็น ถ้านิพจน์ไม่มีการอ้างอิงตัวแปรหรือฟังก์ชันใดๆ สามารถไม่ส่ง environment ได้ เช่น Expression.Evaluate("1 + 1") ใช้ได้เลย แต่ถ้ามีการอ้างอิงฟังก์ชัน เช่น List.Sum ต้องใส่ในสภาพแวดล้อมหรือใช้ #shared

**Q: #shared และ record ที่ส่งเป็น environment ต่างกันอย่างไร**

#shared ให้เข้าถึงฟังก์ชันและตัวแปรทั่วโลกของ Power Query ส่วน record ที่ส่งเป็น environment มีแต่สิ่งที่ระบุเท่านั้น ถ้าต้องใช้ฟังก์ชัน List.Sum ด้วย ต้องใส่ [List.Sum = List.Sum] ในการส่ง record

**Q: สามารถใช้ Expression.Evaluate ในแต่ละแถวของตาราง (row context) ได้หรือ**

ได้ แต่ต้องระวังเรื่องประสิทธิภาพ การประเมินผลนิพจน์แต่ละแถวจะช้ากว่าการใช้โค้ด M ที่เขียนตรงๆ ใช้เมื่อจำเป็นจริงๆ เท่านั้น

**Q: ถ้า document มีไวยากรณ์ผิด จะเกิดอะไร**

จะได้ข้อผิดพลาด (error) ที่บอกตำแหน่งและประเภทของข้อผิดพลาด ต้องตรวจสอบนิพจน์ที่ส่งให้เป็นโค้ด M ที่ถูกต้อง

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

- [Expression.Constant – ส่งค่าเป็นโค้ด M](https://www.thepexcel.com/functions/power-query/expression-functions/expression-constant/)
- [Expression.Identifier – สร้างตัวแทนโค้ด M สำหรับชื่อตัวแปร](https://www.thepexcel.com/functions/power-query/expression-functions/expression-identifier/)
- [List.Sum – รวมค่าตัวเลขในรายการ](https://www.thepexcel.com/functions/power-query/list-functions/list-sum/)
- [Text.Combine – รวมข้อความหลายรายการเป็นข้อความเดียว](https://www.thepexcel.com/functions/power-query/text-functions/text-combine/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)

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

- [Microsoft Learn: Expression.Evaluate](https://learn.microsoft.com/en-us/powerquery-m/expression-evaluate) _(official)_
- [PowerQuery.how: Expression.Evaluate](https://powerquery.how/expression-evaluate/) _(article)_
- [Chris Webb's BI Blog: Expression.Evaluate() In Power Query/M](https://blog.crossjoin.co.uk/2015/02/06/expression-evaluate-in-power-querym/) _(article)_
- [SQLBI: Power Query Evaluate the Environment](https://www.sumproduct.com/blog/article/power-query-blogs/power-query-evaluate-the-environment) _(article)_
- [Expressions, Values, and Let Expression](https://learn.microsoft.com/en-us/powerquery-m/expressions-values-and-let-expression) _(official)_

---

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