Web.Contents เป็นฟังก์ชันสำหรับดาวน์โหลดเนื้อหาจากเว็บไซต์หรือแหล่งข้อมูลบนอินเทอร์เน็ตผ่าน URL และคืนค่าเป็นข้อมูลแบบ binary data ที่สามารถนำไปประมวลผลต่อได้
.
รองรับการเชื่อมต่อและเรียกใช้งาน REST API อย่างครบถ้วนพร้อมระบบการยืนยันตัวตนผ่านทางตัวเลือกต่างๆ เช่น การกำหนดค่า Headers สำหรับส่งข้อมูลเพิ่มเติมในคำขอ การใช้ Query parameters สำหรับการกรองข้อมูลและส่งพารามิเตอร์ และการระบุ ApiKeyName เพื่อจัดเก็บคีย์การเข้าถึงอย่างปลอดภัย
.
เหมาะอย่างยิ่งสำหรับการเชื่อมต่อกับบริการเว็บและจุดเชื่อมต่อ API แบบไดนามิก โดยใช้ความสามารถของ RelativePath และตัวเลือก Query เพื่อสร้าง URL แบบยืดหยุ่นและทำให้กระบวนการรีเฟรชข้อมูลทำงานได้อย่างราบรื่นและมีประสิทธิภาพในระบบ Power BI Service
=Web.Contents(url as text, optional options as nullable record) as binary
=Web.Contents(url as text, optional options as nullable record) as binary
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| url | text | Yes | ที่อยู่ URL ของแหล่งข้อมูลบนเว็บที่ต้องการดาวน์โหลดและดึงข้อมูลเข้ามา สามารถระบุเป็น HTTP protocol หรือ HTTPS protocol ก็ได้ โดยระบบจะทำการ upgrade จาก HTTP ไปเป็น HTTPS ให้โดยอัตโนมัติเพื่อความปลอดภัย สำหรับกรณีที่ต้องการสร้าง dynamic URLs ที่เปลี่ยนแปลงได้ตามสถานการณ์ ควรระบุเฉพาะ base URL ที่คงที่ไว้ในพารามิเตอร์นี้ แล้วใช้พารามิเตอร์ RelativePath สำหรับระบุส่วนของ path ที่เปลี่ยนแปลงแบบไดนามิก | |
| options | nullable record | Optional | null | Record ที่มี optional properties สำหรับควบคุม HTTP request behavior รวมถึง Query, Headers, Timeout, RelativePath, ApiKeyName, Content, ExcludedFromCacheKey, IsRetry, และ ManualStatusHandling |
| Query (in options) | record | Optional | null | Record ของ query parameters ที่จะเพิ่มใน URL โดย Power Query จะทำ URL encoding ให้อัตโนมัติ เหมาะสำหรับสร้าง dynamic API calls โดยไม่ต้องทำ string concatenation ด้วยตัวเอง |
| ApiKeyName (in options) | text | Optional | null | ชื่อ (ไม่ใช่ค่า) ของ API key parameter สำหรับ authentication ค่าจริงจะถูกดึงจาก Power Query credentials (Web API authentication) และแทรกเข้าไปใน request อัตโนมัติ เพื่อความปลอดภัยและป้องกันการเปิดเผย credentials ใน M code |
| Headers (in options) | record | Optional | null | Record ของ HTTP headers ที่ต้องการส่งไปพร้อม request เช่น Content-Type, Authorization (Bearer token), Accept, User-Agent เป็นต้น field names ที่มี special characters ต้องใช้ #”Header-Name” syntax |
| Timeout (in options) | duration | Optional | #duration(0, 0, 1, 40) | ระยะเวลา timeout สำหรับ HTTP request ใช้ #duration() function เพื่อระบุค่า เช่น #duration(0, 0, 5, 0) สำหรับ 5 นาที ค่า default คือ 100 วินาที เหมาะสำหรับ API endpoints ที่ response ช้า |
| ExcludedFromCacheKey (in options) | list | Optional | null | List ของ HTTP header keys ที่ไม่ต้องการนำมาคำนวณใน cache key เหมาะสำหรับ headers ที่เปลี่ยนแปลงบ่อยแต่ไม่ส่งผลต่อ response เช่น request timestamp หรือ correlation IDs |
| IsRetry (in options) | logical | Optional | false | ตั้งค่าเป็น true เพื่อบังคับให้ดึงข้อมูลใหม่และไม่ใช้ cached response มีประโยชน์เมื่อต้องการ fresh data หรือเมื่อทดสอบ API changes |
| ManualStatusHandling (in options) | list | Optional | null | List ของ HTTP status codes ที่ต้องการจัดการเอง แทนที่จะใช้ built-in error handling ของ Power Query เช่น {404, 500} เหมาะสำหรับ custom error handling logic |
| RelativePath (in options) | text | Optional | null | Path ที่จะเพิ่มต่อท้าย base URL ก่อนทำ request ใช้สำหรับสร้าง dynamic endpoints โดยที่ Power Query ยังคงรู้จัก base URL เป็น data source เดียวกัน ซึ่งช่วยให้ refresh ใน Power BI Service ทำงานได้ถูกต้อง |
| Content (in options) | binary | Optional | null | Request body สำหรับ POST request เมื่อระบุ Content จะเปลี่ยน HTTP method จาก GET เป็น POST อัตโนมัติ มักใช้ร่วมกับ Json.FromValue() เพื่อแปลง record เป็น JSON binary สำหรับส่งไปยัง REST APIs |
ใช้ Web.Contents ดาวน์โหลดไฟล์ CSV, JSON, XML หรือ Excel จาก web URL แล้วนำมาประมวลผลด้วย Table functions, Json.Document หรือ Xml.Document
ดึงข้อมูลจาก REST API endpoints โดยใช้ Query parameters สำหรับ filtering และ pagination ร่วมกับ Json.Document เพื่อแปลง JSON response เป็น table structure
ใช้ Content parameter ส่ง JSON payload ไปยัง API endpoint เพื่อ create, update หรือ query ข้อมูลแบบ complex โดยใช้ Headers สำหรับ authentication และ content type
ใช้ ApiKeyName parameter สำหรับ authenticate กับ APIs ที่ต้องการ API key โดยเก็บ key ใน Power Query credentials แทนการฝังใน M code เพื่อความปลอดภัยและให้ refresh ทำงานได้ใน Power BI Service
สร้าง dynamic API endpoints โดยใช้ RelativePath และ Query parameters เพื่อให้ Power Query รู้จัก base URL และทำ refresh ได้อัตโนมัติ เหมาะสำหรับ APIs ที่มี resource IDs หรือ date ranges ที่เปลี่ยนแปลง
let Source = Web.Contents("https://api.example.com/data.json"), JsonData = Json.Document(Source) in JsonDatalet
Source = Web.Contents("https://api.example.com/data.json"),
JsonData = Json.Document(Source)
in
JsonData
Record หรือ List ขึ้นกับโครงสร้าง JSON
let BaseUrl = "https://api.example.com/products", Category = "electronics", MinPrice = 100, Response = Web.Contents( BaseUrl, [ Query = [ category = Category, m…let
BaseUrl = "https://api.example.com/products",
Category = "electronics",
MinPrice = 100,
Response = Web.Contents(
BaseUrl,
[
Query = [
category = Category,
minPrice = Number.ToText(MinPrice),
limit = "50"
]
]
),
JsonResponse = Json.Document(Response)
in
JsonResponse
JSON response จาก API พร้อม query string: ?category=electronics&minPrice=100&limit=50
let ApiUrl = "https://api.example.com/secure/data", Response = Web.Contents( ApiUrl, [ Headers = [ #"Content-Type" = "application/json", #"Authorization" = "Bea…let
ApiUrl = "https://api.example.com/secure/data",
Response = Web.Contents(
ApiUrl,
[
Headers = [
#"Content-Type" = "application/json",
#"Authorization" = "Bearer YOUR_TOKEN_HERE",
#"Accept" = "application/json"
]
]
),
JsonData = Json.Document(Response),
DataTable = Table.FromRecords(JsonData[results])
in
DataTable
Table จาก authenticated API response
let BaseUrl = "https://api.example.com/v1", ResourceType = "customers", ResourceId = "12345", Response = Web.Contents( BaseUrl, [ RelativePath = ResourceType &…let
BaseUrl = "https://api.example.com/v1",
ResourceType = "customers",
ResourceId = "12345",
Response = Web.Contents(
BaseUrl,
[
RelativePath = ResourceType & "/" & ResourceId,
Query = [
include = "orders,profile",
format = "json"
]
]
),
JsonData = Json.Document(Response)
in
JsonData
Customer data จาก URL: https://api.example.com/v1/customers/12345?include=orders,profile&format=json
let ApiUrl = "https://api.example.com/search", SearchCriteria = [ keyword = "Power Query", category = "tutorials", limit = 20 ], JsonPayload = Json.FromValue(Se…let
ApiUrl = "https://api.example.com/search",
SearchCriteria = [
keyword = "Power Query",
category = "tutorials",
limit = 20
],
JsonPayload = Json.FromValue(SearchCriteria),
Response = Web.Contents(
ApiUrl,
[
Headers = [
#"Content-Type" = "application/json",
#"Accept" = "application/json"
],
Content = JsonPayload
]
),
JsonResponse = Json.Document(Response),
ResultsTable = Table.FromList(
JsonResponse[results],
Splitter.SplitByNothing(),
null,
null,
ExtraValues.Error
)
in
ResultsTable
Table จาก POST API response พร้อม search results
let ApiUrl = "https://api.example.com/data", Response = Web.Contents( ApiUrl, [ RelativePath = "customers/search", Query = [ country = "TH", status = "active" ]…let
ApiUrl = "https://api.example.com/data",
Response = Web.Contents(
ApiUrl,
[
RelativePath = "customers/search",
Query = [
country = "TH",
status = "active"
],
ApiKeyName = "api_key"
]
),
JsonData = Json.Document(Response),
CustomersTable = Table.FromRecords(JsonData[customers])
in
CustomersTable
Table ของ customer data พร้อม URL: https://api.example.com/data/customers/search?country=TH&status=active&api_key=******
let ApiUrl = "https://api.example.com/large-dataset", Response = Web.Contents( ApiUrl, [ Query = [limit = "10000"], Timeout = #duration(0, 0, 10, 0), IsRetry =…let
ApiUrl = "https://api.example.com/large-dataset",
Response = Web.Contents(
ApiUrl,
[
Query = [limit = "10000"],
Timeout = #duration(0, 0, 10, 0),
IsRetry = true,
Headers = [
#"Accept-Encoding" = "gzip",
#"User-Agent" = "PowerQuery/1.0"
]
]
),
JsonData = Json.Document(Response),
BufferedData = Binary.Buffer(Response)
in
JsonData
JSON data พร้อมการจัดการ timeout 10 นาที และ bypass cache
Web.Contents คืนค่า binary data จาก URL เหมาะสำหรับดึงไฟล์ JSON, CSV, XML หรือเรียก REST APIs ส่วน Web.Page ใช้สำหรับ parse HTML content และแปลงเป็น tables จาก HTML tables ใน webpage
.
ถ้าต้องการดึงข้อมูลจาก API ใช้ Web.Contents ร่วมกับ Json.Document แต่ถ้าต้องการ scrape ข้อมูลจาก HTML tables ใช้ Web.Page ครับ
สาเหตุหลักคือ Power BI Service มองว่า URL ที่ต่างกันเป็นคนละ data source เมื่อใช้ text concatenation สร้าง dynamic URLs
.
แนะนำให้ใช้ RelativePath และ Query parameters แทนการทำ & “?param=” & value เพื่อให้ Power Query รู้จัก base URL เป็น data source เดียวกัน นอกจากนี้ต้องแน่ใจว่า credentials ถูกตั้งค่าใน Power BI Service สำหรับ base URL นั้นนะครับ
ใช้ ApiKeyName parameter แทนการฝัง API key ใน M code โดยระบุเฉพาะชื่อ parameter (เช่น “api_key”) แล้วตั้งค่า Web API credentials ใน Power Query โดยใส่ URL และ API key value
.
ค่าจริงจะถูกแทรกเข้าไปใน request อัตโนมัติโดยไม่แสดงใน M formula วิธีนี้ปลอดภัยและทำให้ publish ไปยัง Power BI Service ได้โดยไม่เปิดเผย credentials ครับ 😎
ระบุ Content parameter ใน options record จะเปลี่ยน HTTP method จาก GET เป็น POST อัตโนมัติ
.
โดยทั่วไปจะใช้ Json.FromValue() แปลง M record เป็น JSON binary แล้วส่งเป็น Content พร้อมตั้งค่า Headers = [#”Content-Type” = “application/json”] สำหรับบอก server ว่าเป็น JSON payload หมายเหตุ: POST requests ใน Power Query จำเป็นต้องใช้ anonymous authentication ครับ
Web.Contents เองไม่รองรับ query folding เพราะเป็นการดาวน์โหลดข้อมูลจาก web endpoint ที่ไม่มี query engine
.
แต่ถ้า API รองรับ server-side filtering/pagination คุณสามารถใช้ Query parameters หรือ RelativePath เพื่อส่ง filters ไปให้ API ทำงานฝั่ง server ซึ่งให้ผลลัพธ์คล้าย query folding โดยลด data transfer และเพิ่มความเร็วครับ
RelativePath ช่วยให้ Power Query รู้จัก base URL และ dynamic parts แยกจากกัน
.
เมื่อใช้ text concatenation เช่น BaseUrl & “/customers/” & ID จะทำให้แต่ละ URL ถูกมองเป็นคนละ data source ทำให้ refresh ใน Power BI Service ล้มเหลวหรือต้องตั้งค่า credentials หลายครั้ง ส่วน RelativePath จะทำให้ Power Query รู้ว่าทุก endpoint ใช้ credentials เดียวกันจาก base URL ครับ
ใช้ ManualStatusHandling parameter เพื่อระบุ HTTP status codes ที่ต้องการจัดการเอง เช่น [ManualStatusHandling = {404, 500}] จะทำให้ Power Query ไม่ throw error เมื่อเจอ status code เหล่านี้
.
แล้วใช้ try…otherwise เพื่อ handle errors และคืนค่า default หรือใช้ Web.Contents(…)[Response][StatusCode] เพื่อตรวจสอบ status code แล้วทำ conditional logic ตามต้องการครับ
Binary.Buffer(Web.Contents(url)) จะ cache binary response ใน memory หลังจากดาวน์โหลดครั้งแรก
.
ถ้ามีการอ้างอิง result หลายครั้งใน query จะไม่ต้องดาวน์โหลดซ้ำ เหมาะสำหรับ API responses ขนาดใหญ่ที่ใช้ในหลาย steps หรือเมื่อต้องการลด API calls แต่ระวังการใช้ memory มากเกินไปถ้าไฟล์ใหญ่มากนะครับ 😅
Web.Contents เป็น Power Query function ที่ใช้ดึงข้อมูลจากเว็บไซต์และเรียกใช้งาน REST API แบบครบวงจร
.
ที่เจ๋งคือมันรองรับทั้งการดาวน์โหลดไฟล์ต่างๆ (HTML, CSV, JSON) และการเชื่อมต่อ API endpoints ที่มีระบบยืนยันตัวตน
.
ส่วนตัวผมใช้บ่อยมากเลยครับ โดยเฉพาะตอนต้องดึงข้อมูลจาก REST APIs มาทำงานใน Power BI 😎
ฟังก์ชันนี้จะคืนค่าเป็นข้อมูลประเภท binary data จากที่อยู่ URL ที่คุณระบุ และยังรองรับการตั้งค่าพารามิเตอร์เพิ่มเติมผ่าน options record ซึ่งช่วยให้คุณสามารถควบคุมพฤติกรรมของ HTTP request ได้อย่างละเอียดและแม่นยำ เช่น การส่งข้อมูล Headers สำหรับการระบุข้อมูลเพิ่มเติม การกำหนด Query parameters สำหรับการกรองข้อมูล การตั้งค่าระยะเวลา Timeout เพื่อรองรับการเชื่อมต่อที่ใช้เวลานาน และการใช้ RelativePath เพื่อสร้าง dynamic URLs ที่เหมาะสมกับการทำงานแบบรีเฟรชอัตโนมัติในระบบ Power BI Service
การใช้งาน Web.Contents มีความสำคัญอย่างยิ่งในการทำงานด้าน data integration หรือการรวมข้อมูลจากระบบภายนอกเข้ามาในระบบของเรา โดยเฉพาะอย่างยิ่งเมื่อต้องเชื่อมต่อกับ REST APIs ที่มีความต้องการด้านความปลอดภัยและการยืนยันตัวตน เช่น การใช้ API keys สำหรับการเข้าถึงข้อมูล การใช้ Bearer tokens สำหรับการยืนยันสิทธิ์ หรือการกำหนด custom HTTP headers เพื่อระบุข้อมูลเฉพาะในการเชื่อมต่อ การตั้งค่าเหล่านี้จะช่วยให้ Power Query สามารถรีเฟรชข้อมูลได้อัตโนมัติและต่อเนื่องทั้งใน Power BI Service และใน Excel โดยไม่ต้องมีการตั้งค่าซ้ำหรือป้อนข้อมูลการยืนยันตัวตนทุกครั้ง
Web.Contents รองรับการทำงานทั้งแบบ GET requests และ POST requests ซึ่งเป็นรูปแบบการเรียกใช้งาน API ที่พบเห็นได้บ่อยที่สุด โดย GET request จะเกิดขึ้นเมื่อคุณไม่ได้ระบุพารามิเตอร์ Content ในการเรียกใช้งาน เหมาะสำหรับการดึงข้อมูลจากเซิร์ฟเวอร์โดยไม่ต้องส่งข้อมูลใดๆ ไปพร้อมกับคำขอ ในขณะที่ POST request จะถูกใช้งานโดยอัตโนมัติเมื่อคุณระบุพารามิเตอร์ Content เช่น การส่ง JSON payload หรือข้อมูลอื่นๆ ไปยัง API endpoint ซึ่งเหมาะสำหรับการส่งข้อมูลไปยัง API endpoints เพื่อสร้าง อัปเดต หรือค้นหาข้อมูลตามเงื่อนไขที่ซับซ้อน
สำหรับการทำงานที่ต้องการการยืนยันตัวตนกับ APIs คุณควรใช้พารามิเตอร์ ApiKeyName แทนการฝัง API key ลงใน M code โดยตรง เพื่อความปลอดภัยและป้องกันการเปิดเผยข้อมูลสำคัญ ค่าของ API key จริงๆ จะถูกจัดเก็บอย่างปลอดภัยใน Power Query credentials และระบบจะแทรกเข้าไปใน request อัตโนมัติโดยไม่แสดงในโค้ด M formula ที่เขียน วิธีการนี้ช่วยให้คุณสามารถแชร์โค้ดหรือเผยแพร่รายงานไปยัง Power BI Service ได้อย่างปลอดภัยโดยไม่ต้องกังวลเรื่องการรั่วไหลของข้อมูล credentials
การใช้พารามิเตอร์ RelativePath และ Query parameters ช่วยให้คุณสามารถสร้าง dynamic URLs ได้อย่างถูกต้องและมีประสิทธิภาพ โดย Power Query จะทำการ URL encoding ให้อัตโนมัติและยังสามารถจดจำได้ว่า base URL ใดเป็น data source เดียวกัน ซึ่งจะช่วยให้ระบบ refresh behavior ทำงานได้ดีและราบรื่นใน Power BI Service โดยไม่ต้องตั้งค่า credentials ซ้ำสำหรับทุก URL ที่เกิดจากการสร้างแบบไดนามิก วิธีการนี้ดีกว่าการใช้ text concatenation มากเพราะช่วยให้ระบบสามารถทำ scheduled refresh ได้อย่างถูกต้อง
Web.Contents มักจะถูกใช้งานร่วมกับฟังก์ชันอื่นๆ ในหลายรูปแบบ เช่น Json.Document สำหรับแปลงข้อมูล JSON response ให้เป็น record หรือ table structure, Json.FromValue สำหรับแปลง M record เป็น JSON binary เพื่อส่งเป็น POST request payload, Web.Page สำหรับการแยกวิเคราะห์เนื้อหา HTML content และแปลงเป็น tables, และ Binary.Buffer สำหรับ cache ผลลัพธ์ไว้ใน memory เพื่อลดการดาวน์โหลดซ้ำ นอกจากนี้ยังสามารถใช้ร่วมกับ Table.FromRecords และ Table.FromList สำหรับแปลง JSON arrays เป็น Power Query tables, Table.FromColumns สำหรับจัดรูปแบบข้อมูลในรูปแบบที่ต้องการ, Number.ToText สำหรับแปลงตัวเลขเป็น text เพื่อใช้ใน query parameters, Splitter.SplitByNothing สำหรับการแยกข้อมูล list, AccessControlEntry.ConditionToIdentities และ AdoDotNet.DataSource สำหรับการเชื่อมต่อข้อมูลที่ต้องการการยืนยันตัวตนอย่างปลอดภัย