---
title: Compression.Type – ระบุประเภทการบีบอัดข้อมูล
url: https://www.thepexcel.com/functions/power-query/enumerations/compression-type/
type: function-explainer
program: Power Query
syntax: "{{ Compression.[Type] }}"
date: 2025-12-04
updated: 2025-12-26
scores:
  popularity: 4
  difficulty: 2
  usefulness: 4
---

# Compression.Type – ระบุประเภทการบีบอัดข้อมูล

> Compression.Type เป็น enumeration ใน Power Query ที่ช่วยกำหนดประเภทการบีบอัดข้อมูล เช่น GZip, Deflat

## คำอธิบาย

Compression.Type เป็น enumeration ใน Power Query ที่ช่วยกำหนดประเภทการบีบอัดข้อมูล เช่น GZip, Deflate, Snappy, Brotli, LZ4, Zstandard เมื่อทำงานกับ Binary.Compress หรือ Binary.Decompress

## Syntax

```excel
{{ Compression.[Type] }}
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| Type | Yes | text |  | ชนิดการบีบอัดจากรายการต่อไปนี้: None, GZip, Deflate, Snappy, Brotli, LZ4, Zstandard |

## ตัวอย่าง

### 1. ใช้ GZip ในการบีบอัด

```excel
let
    TextData = "Hello World",
    BinaryData = Text.ToBinary(TextData, BinaryEncoding.Utf8),
    Compressed = Binary.Compress(BinaryData, Compression.GZip)
in
    Compressed
```

**ผลลัพธ์:** `ข้อมูล binary ที่บีบอัดด้วย GZip`

แปลง text เป็น binary จากนั้นบีบอัดด้วย Compression.GZip ผลลัพธ์คือ binary ที่เล็กลง สะดวกสำหรับส่งข้อมูลทางเครือข่าย

### 2. ยกเลิกการบีบอัด Deflate

```excel
let
    CompressedBinary = #binary({115, 103, 200, 7, 194, 20, 134, 36}),
    Decompressed = Binary.Decompress(CompressedBinary, Compression.Deflate),
    TextResult = Binary.ToText(Decompressed, BinaryEncoding.Utf8)
in
    TextResult
```

**ผลลัพธ์:** `ข้อความต้นฉบับที่ยกเลิกการบีบอัดแล้ว`

ใช้ Compression.Deflate เพื่อบอก Binary.Decompress ว่าข้อมูลถูกบีบอัดด้วย Deflate จากนั้นแปลง binary กลับเป็น text ด้วย Binary.ToText

### 3. ตรวจสอบข้อมูล API ที่บีบอัดด้วย Brotli

```excel
let
    ApiResponse = Json.Document(Web.Contents("https://api.example.com/data")),
    CompressedData = ApiResponse[data],
    Decompressed = Binary.Decompress(CompressedData, Compression.Brotli),
    Result = Binary.ToText(Decompressed, BinaryEncoding.Utf8)
in
    Result
```

**ผลลัพธ์:** `ข้อมูล JSON ที่ยกเลิกการบีบอัดแล้ว`

บางตัว API ส่งข้อมูลบีบอัดด้วย Brotli เพื่อประหยัด bandwidth ใช้ Compression.Brotli เพื่อแตกไฟล์ให้ใช้งานได้

### 4. เลือกประเภทการบีบอัดแบบไดนามิก

```excel
let
    BinaryData = #binary({31, 139, 8, 0, 0, 0, 0, 0, 0, 255}),
    CompressionType = Compression.GZip,
    SafeDecompress = try
        Binary.Decompress(BinaryData, CompressionType)
    otherwise
        #binary({})
in
    SafeDecompress
```

**ผลลัพธ์:** `ข้อมูลที่ยกเลิกการบีบอัด หรือ empty binary ถ้าเกิดข้อผิดพลาด`

บันทึก Compression.Type ไว้ในตัวแปรเพื่อให้เปลี่ยนได้ง่าย และใช้ try...otherwise เพื่อจัดการข้อผิดพลาดถ้าชนิดบีบอัดไม่ตรงกัน

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

- ผมแนะนำให้เริ่มจาก Compression.GZip หากไม่แน่ใจว่าข้อมูลบีบอัดแบบไหน มันปลอดภัยและใช้ได้ทั่วไป

- ใช้ try...otherwise ครอบ Binary.Decompress ไว้เสมอ หากเลือก compression type ผิด จะได้ข้อมูล null แทนที่จะ Error หยุดทั้ง query

- ส่วนตัวผมใช้ Compression.Deflate เมื่อทำงานกับไฟล์ ZIP และ Compression.GZip สำหรับ Web API ที่ส่งข้อมูล compressed

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

**Q: ต้องเลือกประเภทการบีบอัดไหน GZip หรือ Deflate?**

มันขึ้นอยู่กับว่าข้อมูลของคุณถูกบีบอัดด้วยวิธีไหนมา ผมแนะนำให้ดูเอกสาร API หรือถามผู้ให้บริการข้อมูล ถ้าไม่แน่ใจให้เริ่มจาก GZip ก่อน เพราะใช้ได้กว้างที่สุด

**Q: ใช้ Compression.None สำหรับอะไร?**

ผมใช้ Compression.None เวลาข้อมูลไม่ได้บีบอัดเลย แต่ว่า Binary.Decompress ต้องการค่า enum นี้ อย่างไรก็ตาม ถ้าข้อมูลไม่ได้บีบอัด ก็ไม่จำเป็นต้องใช้ Binary.Decompress หลายที

**Q: Snappy vs LZ4 vs Zstandard ต่างกันยังไง?**

ผมพูดให้เข้าใจง่ายๆ: Snappy เร็วสุด แต่บีบอัดไม่ได้ดีเท่า LZ4 สมดุล Zstandard เจ๋งสุด บีบอัดดีและเร็ว แต่บางระบบอาจไม่รองรับ ถ้าลูกค้าบอกมา ก็ใช้ที่พวกเขาต้องการ

**Q: จะรู้ได้ยังไงว่าข้อมูลที่ได้มาบีบอัดด้วยแบบไหน?**

ดู file header หรือขอเอกสารจาก API provider ส่วนตัวผมมักถามถึง Slack หรือ email ถ้าไม่แน่ใจ ใช้เวลานิดหน่อยแต่ดีกว่า Error ร่วมราม

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

- [Microsoft Learn: Compression.Type](https://learn.microsoft.com/en-us/powerquery-m/Compression.Type) _(official)_
- [Microsoft Learn: Binary.Compress](https://learn.microsoft.com/en-us/powerquery-m/binary-compress) _(official)_
- [Microsoft Learn: Binary.Decompress](https://learn.microsoft.com/en-us/powerquery-m/binary-decompress) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/enumerations/compression-type/](https://www.thepexcel.com/functions/power-query/enumerations/compression-type/)_
