Basic Power BI ตอนที่ 3: สร้าง Relationship ระหว่างตารางให้ถูกต้องตั้งแต่ต้น
สรุปจากคลิปของช่อง Lin Davoy ซีรีส์ Basic Power BI ตอนที่ 3 ต่อจากตอนที่ 1 ที่พาไปรู้จักการนำเข้าข้อมูลและสร้างกราฟง่ายๆ และตอนที่ 2 ที่พาไป Clean ข้อมูลด้วย Power Query มาถึงตอนที่ 3 นี้เป็นหัวใจสำคัญของการทำ Data Model ใน Power BI นั่นคือการสร้าง Relationship หรือความสัมพันธ์ระหว่างตาราง เพราะถ้าตั้งค่าตรงนี้ผิด ตัวเลขในกราฟที่ออกมาอาจจะผิดทันทีโดยที่เราไม่รู้ตัวด้วยซ้ำ
ทำไมต้องแยกตารางแล้วมาผูก Relationship แทนที่จะรวมเป็นตารางเดียว
ในมุมคนทำงานจริง หลายคนที่เพิ่งเริ่มใช้ Power BI มักถามว่าทำไมไม่รวมทุกอย่างไว้ในตารางเดียวให้จบๆ ไป Lin อธิบายว่าการแยกตาราง เช่น ตารางสินค้า ตารางลูกค้า และตารางรายการขาย แล้วผูกด้วย Relationship จะช่วยให้ไฟล์มีขนาดเล็กลง อัปเดตข้อมูลง่ายขึ้น และลดโอกาสที่ข้อมูลจะซ้ำซ้อนหรือขัดแย้งกันเอง ซึ่งเป็นแนวคิดเดียวกับที่เรียกว่า Star Schema ที่มืออาชีพด้าน Data นิยมใช้กัน
Cardinality คืออะไร ทำไมต้องรู้จัก One-to-Many
เวลาลาก Relationship เชื่อมสองตารางใน Power BI ระบบจะถามว่าความสัมพันธ์เป็นแบบไหน ระหว่าง One-to-Many, One-to-One หรือ Many-to-Many Lin เน้นว่ากรณีที่เจอบ่อยที่สุดในงานจริงคือ One-to-Many เช่น ตารางสินค้า 1 แถวต่อสินค้า 1 ชิ้น ไปจับคู่กับตารางรายการขายที่มีสินค้าชิ้นเดียวกันถูกขายหลายครั้ง การเข้าใจตรงนี้ถูกต้องตั้งแต่ต้นจะช่วยป้องกันปัญหาตัวเลขบวกซ้ำหรือ Sum ผิดที่ตามมาทีหลัง
Cross Filter Direction ตัวเลือกที่มือใหม่มักตั้งผิด
อีกจุดที่ Lin ย้ำเป็นพิเศษคือ Cross Filter Direction หรือทิศทางที่ Filter จะไหลผ่านไปมาระหว่างตาราง ถ้าตั้งเป็น Single หมายความว่า Filter จะไหลไปทางเดียว แต่ถ้าตั้งเป็น Both จะไหลได้สองทาง ซึ่งฟังดูสะดวกแต่ในทางปฏิบัติอาจทำให้เกิดผลลัพธ์ที่ไม่คาดคิดหรือ Ambiguous Relationship ได้ Lin แนะนำว่าสำหรับมือใหม่ควรเริ่มจาก Single Direction ไปก่อน แล้วค่อยปรับเป็น Both เฉพาะกรณีที่จำเป็นจริงๆ เท่านั้น
ข้อผิดพลาดยอดฮิตที่ทำให้ตัวเลขในกราฟผิด
ปัญหาที่ Lin เจอบ่อยจากคนที่เพิ่งหัด Power BI คือการลืมเช็คว่าคอลัมน์ที่ใช้เชื่อม Relationship มีค่าไม่ซ้ำกันในตารางฝั่ง One หรือเปล่า ถ้าคอลัมน์นั้นมีค่าซ้ำทั้งสองฝั่ง Power BI จะสร้างเป็น Many-to-Many โดยอัตโนมัติซึ่งเสี่ยงทำให้ตัวเลขในกราฟบวกผิดเพี้ยนไปจากความเป็นจริง ทางแก้คือกลับไปเช็คที่ Power Query ให้แน่ใจว่าตาราง Dimension อย่างตารางสินค้าหรือตารางลูกค้ามีรหัสไม่ซ้ำกันเสมอ
| ประเภท Cardinality | ความหมาย | ใช้เมื่อไหร่ |
|---|---|---|
| One-to-Many (1:*) | 1 แถวในตารางหนึ่ง จับคู่กับหลายแถวในอีกตาราง | กรณีปกติที่สุด เช่น 1 สินค้า มีหลายรายการขาย |
| One-to-One (1:1) | 1 แถวจับคู่กับ 1 แถวเท่านั้น | ตารางแยกรายละเอียดเพิ่มเติมของ Entity เดียวกัน |
| Many-to-Many (*:*) | หลายแถวจับคู่กับหลายแถว | ควรเลี่ยงถ้าเป็นไปได้ เพราะเสี่ยงตัวเลขซ้ำซ้อนในกราฟ |
คุ้มไหมที่จะเรียนรู้เรื่อง Relationship ให้ลึก
คำตอบจาก Lin คือคุ้มมากและจำเป็นด้วยซ้ำ เพราะ Relationship คือรากฐานที่ทุกกราฟและทุก Measure ใน Power BI ยืนอยู่บน ถ้าฐานตรงนี้ผิด ต่อให้สูตร DAX เทพแค่ไหนตัวเลขที่ออกมาก็ยังผิดอยู่ดี การลงทุนเวลาทำความเข้าใจ Cardinality และ Cross Filter Direction ตั้งแต่ต้น จะช่วยประหยัดเวลาแก้บั๊กตัวเลขผิดในอนาคตได้มากกว่าหลายเท่าตัว
Basic Power BI Part 3: Building Table Relationships The Right Way
This is summarized from the Lin Davoy channel, Basic Power BI Part 3. It follows Part 1, which covered importing data and building simple charts, and Part 2, which covered cleaning data with Power Query. Part 3 covers what may be the most important step in building a Power BI data model: creating relationships between tables. Get this step wrong and the numbers in your charts can be wrong immediately, often without you even noticing.
Why split data into separate tables instead of one big table
In practice, many people who are new to Power BI ask why not just put everything into a single table and be done with it. Lin explains that splitting data into separate tables, such as a product table, a customer table, and a sales table, then linking them with relationships keeps the file smaller, makes updates easier, and reduces the chance of duplicate or conflicting data. This is the same idea behind what is known as a star schema, a structure widely used by data professionals.
What cardinality means and why one-to-many matters
When you drag a relationship to link two tables in Power BI, it asks what kind of relationship it is: one-to-many, one-to-one, or many-to-many. Lin stresses that the most common case in real work is one-to-many, for example one row per product in a product table matching many rows in a sales table where the same product is sold multiple times. Getting this right from the start prevents double-counted totals and incorrect sums later on.
Cross filter direction, a setting beginners often get wrong
Another point Lin emphasizes is cross filter direction, which controls how filters flow between tables. Setting it to Single means filters flow one way only, while Both allows filters to flow in both directions. Both sounds convenient, but in practice it can produce unexpected results or ambiguous relationships. Lin recommends beginners start with Single direction and only switch to Both when there is a genuine need for it.
The most common mistake that breaks chart numbers
A problem Lin sees often with Power BI beginners is forgetting to check whether the column used to link a relationship actually has unique values on the one side of the relationship. If that column has duplicate values on both sides, Power BI will automatically create a many-to-many relationship, which risks inflating the numbers in charts. The fix is to go back into Power Query and make sure dimension tables, like a product or customer table, always have a unique key.
| Cardinality Type | What It Means | When To Use It |
|---|---|---|
| One-to-Many (1:*) | One row in a table matches many rows in another table | The most common case, e.g. one product has many sales records |
| One-to-One (1:1) | One row matches exactly one row | A table that splits off extra detail about the same entity |
| Many-to-Many (*:*) | Many rows match many rows | Avoid where possible, it risks double-counted numbers in charts |
Is it worth learning relationships in depth
Lin answer is a clear yes, and it is arguably essential. Relationships are the foundation every chart and every measure in Power BI stands on. If that foundation is wrong, even the most clever DAX formula will still produce wrong numbers. Investing time upfront to understand cardinality and cross filter direction saves far more time than it costs, compared with debugging wrong numbers later.
ติดตามข่าวสาร Data & AI ได้ที่:
Facebook: https://www.facebook.com/davoytech
YouTube: https://www.youtube.com/@linlinmhee
📌 อย่าลืมกด Like, Share และ Subscribe เป็นกำลังใจให้ด้วยนะคะ!
💬 เพื่อนๆ เคยเจอปัญหาตัวเลขในกราฟ Power BI บวกซ้ำหรือผิดเพี้ยนเพราะตั้ง Relationship ผิดไหมคะ มาคอมเมนต์เล่าให้ฟังหน่อยว่าเจอปัญหาแบบไหนแล้วแก้ยังไง
#Davoy #DavoyTech #Linlinmhee #เรียนAI #คอร์สAI #DataAndAI #PowerBI #BasicPowerBI #DataModel #Relationship
