---
title: Expression.Constant – ส่งค่าเป็นโค้ด M
url: https://www.thepexcel.com/functions/power-query/expression-functions/expression-constant/
type: function-explainer
program: Power Query
syntax: "{{ Expression.Constant(value as any) as text }}"
date: 2025-12-12
updated: 2025-12-26
scores:
  popularity: 3
  difficulty: 7
  usefulness: 4
---

# Expression.Constant – ส่งค่าเป็นโค้ด M

> Expression.Constant แปลงค่าใด ๆ ให้เป็นข้อความโค้ด M ที่ถูกต้องทางไวยากรณ์ ใช้เมื่อต้องสร้างโค้ด M แ

## คำอธิบาย

Expression.Constant แปลงค่าใด ๆ ให้เป็นข้อความโค้ด M ที่ถูกต้องทางไวยากรณ์ ใช้เมื่อต้องสร้างโค้ด M แบบไดนามิก

## Syntax

```excel
{{ Expression.Constant(value as any) as text }}
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| value | Yes | any |  | ค่าใด ๆ ที่ต้องการแปลงเป็นข้อความโค้ด M (ตัวเลข ข้อความ วันที่ list record ฯลฯ) |

## ตัวอย่าง

### 1. แปลงตัวเลขให้เป็นโค้ด

```excel
{{ Expression.Constant(123) }}
```

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

ตัวเลข 123 ถูกแปลงเป็นข้อความ "123" ซึ่งเป็นรูปแบบที่ถูกต้องในโค้ด M

### 2. แปลงข้อความให้เป็นโค้ด

```excel
{{ Expression.Constant("hello") }}
```

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

ข้อความ "hello" ถูกแปลงเป็น """hello""" (escaped quotes) เพื่อให้ใช้ได้ใน M code

### 3. แปลงวันที่ให้เป็นโค้ด

```excel
{{ Expression.Constant(#date(2025, 12, 26)) }}
```

**ผลลัพธ์:** `"#date(2025, 12, 26)"`

วันที่ถูกแปลงเป็นรูปแบบ M date literal พร้อมสัญลักษณ์ # เพื่อให้สามารถใช้ในโค้ดได้

### 4. ใช้กับ list

```excel
{{ Expression.Constant({1, 2, 3}) }}
```

**ผลลัพธ์:** `{1, 2, 3}`

List ถูกแปลงเป็นข้อความที่แสดงรูปแบบ M list literal ที่ถูกต้อง

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

- ผมแนะนำให้คิดว่า Expression.Constant เป็น 'serializer' ของโค้ด M - มันแปลงค่าเป็นข้อความเพื่อให้ inject ลงในโค้ด M อื่นได้

- ส่วนตัวผม มักจะใช้คู่กับ Expression.Evaluate ดังนี้ Expression.Constant สร้าง text แล้ว Expression.Evaluate ประมวลผล text นั้นให้กลายเป็นค่าที่ใช้งานได้

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

**Q: ทำไมต้องใช้ Expression.Constant ไม่สามารถนำค่ามาต่อกับ text ธรรมชาติได้หรือ?**

ผมเห็นคำถามนี้บ่อย ส่วนหนึ่งเพราะว่าข้อมูลบางประเภท เช่น text ต้องมี quote ด้านข้าง และบางประเภท เช่น date ต้องมีสัญลักษณ์พิเศษ Expression.Constant จัดการทุกเรื่องให้อัตโนมัติ ถ้า concat ธรรมชาติ อาจจะมี syntax error ได้

**Q: ใช้ได้กับค่าอะไรบ้าง?**

ปกติใช้ได้กับค่า constant ที่เฉพาะเจาะจง เช่น number text logical date time duration list record ถ้าเป็นฟังก์ชัน (function values) บางประเภท อาจจะทำให้เกิด error ได้เพราะฟังก์ชันไม่ใช่ constant

**Q: ผลลัพธ์เป็น text ใช่ไหม ต้องทำอะไรไหม?**

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

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

- [Microsoft Learn - Expression.Constant](https://learn.microsoft.com/en-us/powerquery-m/expression-constant) _(official)_
- [Power Query M Function Reference](https://learn.microsoft.com/en-us/powerquery-m/) _(official)_

---

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