Table.ExpandListColumn ใช้สำหรับขยายคอลัมน์ที่เก็บข้อมูลแบบ List ให้เป็นแถวแยก โดยแต่ละรายการใน List จะกลายเป็นแถวใหม่ ส่วนข้อมูลในคอลัมน์อื่นจะถูกทำซ้ำตามจำนวน
=Table.ExpandListColumn(table as table, column as text) as table
=Table.ExpandListColumn(table as table, column as text) as table
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
| table | table | Yes | ตารางที่ต้องการขยายคอลัมน์ที่เป็น List | |
| column | text | Yes | ชื่อคอลัมน์ (ในรูป Text) ที่เก็บข้อมูลแบบ List ที่ต้องการขยาย |
จัดการข้อมูล JSON Array ที่ Import เข้ามา
แตกข้อมูล Tags หรือ Categories ที่อยู่ในช่องเดียวกัน
เตรียมข้อมูลสำหรับการวิเคราะห์รายรายการย่อย
let Source = #table( {"ProductID", "Tags"}, {{1, {"Electronics", "Gadgets"}}, {2, {"Home"}}, {3, {"Kitchen", "Appliances", "Useful"}}} ), Expanded = Table.Expan…let
Source = #table(
{"ProductID", "Tags"},
{{1, {"Electronics", "Gadgets"}}, {2, {"Home"}}, {3, {"Kitchen", "Appliances", "Useful"}}}
),
Expanded = Table.ExpandListColumn(Source, "Tags")
in
Expanded
ProductID | Tags
1 | Electronics
1 | Gadgets
2 | Home
3 | Kitchen
3 | Appliances
3 | Useful
let Source = #table( {"Name", "Hobbies"}, {{"Alice", {"Reading", "Gaming"}}, {"Bob", null}, {"Charlie", {}}} ), Expanded = Table.ExpandListColumn(Source, "Hobbi…let
Source = #table(
{"Name", "Hobbies"},
{{"Alice", {"Reading", "Gaming"}}, {"Bob", null}, {"Charlie", {}}}
),
Expanded = Table.ExpandListColumn(Source, "Hobbies")
in
Expanded
Name | Hobbies
Alice | Reading
Alice | Gaming
Bob | null
Charlie | null
let Source = #table( {"OrderID", "Items"}, {{ 1, {[ProductName = "Laptop", Qty = 1], [ProductName = "Mouse", Qty = 2]} }} ), ExpandList = Table.ExpandListColumn…let
Source = #table(
{"OrderID", "Items"},
{{
1,
{[ProductName = "Laptop", Qty = 1], [ProductName = "Mouse", Qty = 2]}
}}
),
ExpandList = Table.ExpandListColumn(Source, "Items"),
ExpandRecord = Table.ExpandRecordColumn(ExpandList, "Items", {"ProductName", "Qty"})
in
ExpandRecord
OrderID | ProductName | Qty
1 | Laptop | 1
1 | Mouse | 2
let Source = #table( {"ID", "Colors", "Sizes"}, {{ 1, {"Red", "Blue"}, {"S", "M", "L"} }} ), ExpandColors = Table.ExpandListColumn(Source, "Colors"), ExpandSize…let
Source = #table(
{"ID", "Colors", "Sizes"},
{{
1,
{"Red", "Blue"},
{"S", "M", "L"}
}}
),
ExpandColors = Table.ExpandListColumn(Source, "Colors"),
ExpandSizes = Table.ExpandListColumn(ExpandColors, "Sizes")
in
ExpandSizes
ID | Colors | Sizes
1 | Red | S
1 | Red | M
1 | Red | L
1 | Blue | S
1 | Blue | M
1 | Blue | L
นี่เป็นการป้องกันการสูญเสียแถว ถ้า List ว่าง ฟังก์ชันจะสร้าง null แถวหนึ่ง เพื่อไม่ให้หายไปทั้งแถว ส่วนตัวผมชอบจุดนี้เพราะไม่ต้องกังวลว่าจะหาที่ไปของข้อมูล 😎
ต้องทำแบบ Sequential (ทีละคอลัมน์) ไม่มีวิธีขยายหลายคอลัมน์พร้อมกันในครั้งเดียว ส่วนตัวผมแนะนำให้สร้าง Step แยกสำหรับแต่ละการขยาย ทำให้อ่านและ Debug ง่ายขึ้น
ใช้ Table.ExpandListColumn ก่อน (เพื่อขยาย List) แล้วตามด้วย Table.ExpandRecordColumn (เพื่อแตกผ้าของ Record) นี่คือเทคนิคทั่วไปสำหรับจัดการ Nested Data
ไม่แตก เพราะมีค่าเดียวอยู่แล้ว หากต้องการเพิ่มแถว ต้องให้ List มี 2 รายการขึ้นไป
Table.ExpandListColumn ใช้สำหรับขยายคอลัมน์ที่มีข้อมูลแบบ List ให้เป็นหลายแถว โดยแต่ละรายการใน List จะกลายเป็นแถวแยก ข้อมูลในคอลัมน์อื่นๆ จะถูกทำซ้ำตามจำนวนรายการในแต่ละ List
ที่เจ๋งคือถ้าเจอ null หรือ List ว่าง มันจะ convert เป็น null แถวเดียว ไม่ใช่ลบทิ้ง ซึ่งช่วยให้เราไม่สูญเสียข้อมูล
ส่วนตัวผมใช้ฟังก์ชันนี้บ่อยมากตอนต้องจัดการ JSON หรือ API response ที่มีข้อมูล List ซ้อนอยู่ โดยปกติจะใช้คู่กับ Table.ExpandRecordColumn เพื่อ”ระเบิด”ข้อมูลซ้อนกันหลายชั้น 😎