---
title: Function.Invoke – เรียกใช้ฟังก์ชันแบบไดนามิก
url: https://www.thepexcel.com/functions/power-query/function-values/function-invoke/
type: function-explainer
program: Power Query
syntax: "= Function.Invoke(function as function, args as list) as any"
date: 2025-12-12
updated: 2025-12-23
scores:
  popularity: 4
  difficulty: 7
  usefulness: 5
---

# Function.Invoke – เรียกใช้ฟังก์ชันแบบไดนามิก

> Function.Invoke เรียกใช้ฟังก์ชันโดยส่งรายการอาร์กิวเมนต์เป็นพารามิเตอร์ ใช้สำหรับการเรียกฟังก์ชันแบบ

## คำอธิบาย

Function.Invoke เรียกใช้ฟังก์ชันโดยส่งรายการอาร์กิวเมนต์เป็นพารามิเตอร์ ใช้สำหรับการเรียกฟังก์ชันแบบไดนามิก

## Syntax

```excel
= Function.Invoke(function as function, args as list) as any
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| function | Yes | function |  | ฟังก์ชันที่ต้องการเรียกใช้ (เช่น Record.FieldNames, List.Sum, Text.Combine) |
| args | Yes | list |  | รายการ (list) ที่ประกอบด้วยอาร์กิวเมนต์ที่ต้องการส่งให้ฟังก์ชัน |

## ตัวอย่าง

### 1. เรียกใช้ Record.FieldNames ด้วย Function.Invoke

```excel
let
    MyRecord = [A = 1, B = 2, C = 3],
    Result = Function.Invoke(Record.FieldNames, {MyRecord})
in
    Result
```

**ผลลัพธ์:** `{"A", "B", "C"}`

เรียกฟังก์ชัน Record.FieldNames โดยส่งเรคอร์ด MyRecord ผ่านรายการ {MyRecord} ผลลัพธ์คือรายชื่อฟิลด์ทั้งหมด

### 2. เรียกใช้ Text.Combine ด้วยหลาย argument

```excel
let
    Words = {"Hello", "Power", "Query"},
    Separator = " ",
    Result = Function.Invoke(Text.Combine, {Words, Separator})
in
    Result
```

**ผลลัพธ์:** `"Hello Power Query"`

Function.Invoke ส่ง argument สองตัว: รายการคำและตัวคั่น เหมือนกับ Text.Combine(Words, Separator)

### 3. เรียกฟังก์ชันแบบไดนามิก ตามเงื่อนไข

```excel
let
    Numbers = {1, 2, 3, 4, 5},
    Operation = "Sum",
    ChosenFunction = if Operation = "Sum" then List.Sum else List.Average,
    Result = Function.Invoke(ChosenFunction, {Numbers})
in
    Result
```

**ผลลัพธ์:** `15 (ถ้า Operation="Sum")`

ตัวอย่างการใช้ Function.Invoke เพื่อเลือกฟังก์ชันแบบไดนามิก ตามค่าตัวแปร Operation

### 4. ใช้กับ List.Sum โดยตรง

```excel
let
    Numbers = {10, 20, 30},
    Result = Function.Invoke(List.Sum, {Numbers})
in
    Result
```

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

Function.Invoke(List.Sum, {Numbers}) มีผลเหมือน List.Sum(Numbers) แต่เรียกไดนามิก

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

- Function.Invoke มีประโยชน์มากในการเขียน wrapper function ที่รองรับหลายฟังก์ชัน

- ใช้ร่วมกับ if...then...else ได้ สำหรับการเลือกฟังก์ชันแบบเงื่อนไข

- ถ้าต้องเรียก custom function ที่คุณเขียนเอง ก็ส่ง function reference ลงไปได้เหมือนกัน

- เป็นฟังก์ชันสำหรับ advanced users ที่ต้องเขียน complex logic หรือ dynamic workflows

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

**Q: ต้องใช้ Function.Invoke หรือใช้การเรียกฟังก์ชันตรงๆได้?**

ถ้ารู้ฟังก์ชันแน่นอน ให้เรียกตรงๆเลยจะชัดเจนกว่า แต่ถ้าต้องเลือกฟังก์ชันตามเงื่อนไข จึงใช้ Function.Invoke

**Q: args ต้องใส่อาร์กิวเมนต์ในรูปแบบ list เสมอหรือ?**

ใช่ args ต้องเป็น list เสมอ แม้ว่าฟังก์ชันจะมี argument ตัวเดียว ก็ต้องใส่ {argument} ในรูปแบบรายการ

**Q: ใช้ได้กับฟังก์ชันที่มี optional argument หรือไม่?**

ได้ แต่ต้องใส่ argument ในลำดับที่ถูกต้อง ถ้าไม่ใช้ optional argument ก็ไม่ต้องใส่ในรายการ

**Q: เวอร์ชัน Power Query ไหนเริ่มมี Function.Invoke?**

Function.Invoke มีมาตั้งแต่ Power Query 2016 ขึ้นไป

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

- [Microsoft Learn: Function.Invoke](https://learn.microsoft.com/en-us/powerquery-m/function-invoke) _(official)_
- [Power Query How: Function Reference](https://powerquery.how) _(article)_

---

_Source: [https://www.thepexcel.com/functions/power-query/function-values/function-invoke/](https://www.thepexcel.com/functions/power-query/function-values/function-invoke/)_
