ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link เกี่ยวกับ AI
    • รวม Link Coding
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

Power BI ตอนที่ 08: สร้าง Data Model ที่เหมาะสม

Categories 📂

Data Model

Tags 🏷️

ในที่สุดก็มาถึงตอนที่เป็น Concept สำคัญอีกตอนหนึ่ง นั่นก็คือเรื่องของ Data Model นั่นเอง

ถ้าใครได้ลองทำ Visual ในตอนที่ 4 ก็จะรู้ว่า เราสามารถลาก Field ข้ามตารางมาลงใน Visual ได้ถ้าตารางนั้นได้มีการผูก Relationship กันไว้แล้ว และทิศทางของการ Filter ข้อมูลจะวิ่งตามทิศทางของลูกศรใน Relationship ด้วย

พอพูดถึงเรื่อง Data Model แล้ว การออกแบบ Data Model ที่ดี จะช่วยให้ Performance ของงานเราเร็วขึ้น และสามารถเขียนสูตร DAX ได้ง่ายขึ้นด้วย

สารบัญ

  • Star Schema: Data Model ที่ Power BI ชอบที่สุด
  • ปั้นเกล็ดหิมะให้เป็นดาว
    • เอาตาราง Geography ไปไว้ในตาราง Stores
    • เอาตาราง ProductSubcategory ไปไว้ในตาราง Product
    • เอาตาราง ProductCategory ไปไว้ในตาราง Product
  • สรุปก่อนจะ Close & Apply
  • แล้ว Data Model มีกี่แบบ?
  • ตอนนี้จบเท่านี้
  • สารบัญ Series Power BI

Star Schema: Data Model ที่ Power BI ชอบที่สุด

และจากคำแนะนำของกูรูระดับโลกในเรื่อง DAX อย่าง Marco Russo และ Alberto Ferrari ได้แนะนำไว้ว่า Data Model ที่ Power BI ชอบมากที่สุดคือ Data Model ที่เรียกว่า Star Schema ซึ่งมีหน้าตาแบบนี้ครับ

Star Schema คือรูปแบบที่มีตารางหลัก (เรียกว่าตาราง Fact) อยู่ตรงกลาง แล้วล้อมรอบลากเส้นเชื่อมกับตารางอ้างอิง (เรียกว่าตาราง Dimension)

Tips : ความสัมพันธ์บนเส้นจะมีเลข 1 กับ * ซึ่งมีความหมายดังนี้
(สมมติว่าดูตาราง Product ที่มีตัวเชื่อมกับตาราง fSales เป็น ProductID)

  • 1 คือ ฝั่งที่มีค่า Field ที่เป็น Key ไม่ซ้ำกัน (มี 1 ตัว)
    • อยู่ฝั่ง Dimension เพราะฝั่งนั้น ProductID จะไม่มีทางซ้ำกัน
  • * คือ ฝั่งที่มีค่า Field ที่เป็น Key ซ้ำกันได้
    • อยู่ฝั่ง Fact เพราะ ProductID มีสิทธิ์ซ้ำกันได้ เพราะการขายของแต่ละครั้งก็ขายสินค้าเดียวกันได้จริงมะ

ทีนี้ถ้าเรามาดู Data Model ของเรา ณ ตอนนี้…

มันก็คล้ายๆ Star Schema อยู่นะ แต่กลายเป็นว่าตาราง Dimension มันซ้อนกันหลายชั้นเลย ออกมาจะคล้ายๆ แบบนี้ เค้าเรียกกันว่าเป็น Model แบบ Snowflake

ปั้นเกล็ดหิมะให้เป็นดาว

ดังนั้นสิ่งที่เราจะต้องทำคือ แก้ Data Model ของเราจาก Snowflake ให้เป็น Star Schema โดยการรวบตาราง Dimension ที่ซ้อนๆ กันอยู่ให้เหลือแค่ชั้นเดียวพอ ดังนี้

และวิธีที่จะทำแบบนั้นได้ก็คือการใช้คำสั่ง Merge ใน Power Query นั่นเองครับ ซึ่ง Merge เป็นวิธีการที่สามารถดึงข้อมูลจากอีกตารางนึงมาใส่ในอีกตารางนึงได้ (มองว่าคล้ายๆ VLOOKUP ก็ได้นะ แต่มันไม่เหมือนเป๊ะ)

ดังนั้นเราจะกด Home -> Edit Queries เพื่อเข้าไปแก้ไข Query ของเราซะ

เอาตาราง Geography ไปไว้ในตาราง Stores

เลือกที่ตารางที่ต้องการจะเอาอันอื่นมารวมด้วย นั่นคือตาราง Stores แล้วเลือก Merge Queries ดังรูป

ตอนแรกข้อมูลคอลัมน์ Geography จะออกมาเป็น Table ก่อน ให้เรากด Expand Data ออกมาว่าจะเอา Field ไหนบ้างดังรูป

จากนั้นมันจะแตกข้อมูล 3 คอลัมน์นั้นออกมา ต่อไปให้เราลบ Field ชื่อ GeographyKey ออกจากตาราง Stores ซะ เพราะไม่ต้องใช้แล้ว (ก็เราดึงข้อมูลจากตาราง Geography มาแล้วนี่ เลยไม่ต้องมีตัวเชื่อมแล้ว)

แล้วก็เปลี่ยนชื่อ Query ให้เป็น dStores ซะ (ใส่ตัว d นำหน้าจะได้เห็นว่าเป็น Dimension)

จากนั้นสิ่งที่เราต้องทำต่อคือ ตั้งค่าว่าไม่ต้องเอาตาราง Geography ออกไปใน Model แล้วโดยการคลิ๊กขวาแล้วเลือกให้เครื่องหมายถูกหน้า Enable Load หายไปซะ (แปลว่าไม่ต้องโหลด)

ถ้าเราเคยโหลดข้อมูลออกไปใน Data Model แล้วมาเอา Enable Load ออก มันจะขึ้นข้อความมาเตือนว่าข้อมูลจะหายไปนะ

ซึ่งก็ให้กด continue ไป เพราะเราต้องการให้ตารางนั้นหายไปนี่แหละ ถูกแล้ว

เอาตาราง ProductSubcategory ไปไว้ในตาราง Product

ต่อไปเราก็จะทำคล้ายๆ เดิม คือ เลือกตาราง Product ก่อน แล้วไปที่ Merge Query กับ ProductSubcategory ก่อน โดยตัวเชื่อมคือ ProductSubcategoryKey

จากนั้น Expand Data ออกมา เหมือนเดิมจะพบว่าชื่อ Field ProductSubcategory ดันมี .1 ติดมา เป็นเพราะเดิมทีชื่อมันซ้ำกับชื่อตาราง มันเลยงง ดังนั้นให้ดับเบิ้ลคลิ๊กเปลี่ยนชื่อ Field เอา .1 ออกไปด้วยนะ เดี๋ยวไม่สวย

พอเรามีข้อมล ProductCategoryKey แล้ว เราก็พร้อมที่จะเอาตาราง ProductCategory ไปไว้ในตาราง Product ซะที

เอาตาราง ProductCategory ไปไว้ในตาราง Product

เลือกที่ตาราง Product แล้ว Merge Query เหมือนเดิม โดยตัวเชื่อมคือ ProductCategoryKey

พอ Expand ข้อมูลออกมาแล้ว อย่าลืมเปลี่ยนชื่อ Field ให้เรียบร้อย และลบ Key ที่ไม่ใช้แล้วออกไปซะ เช่น ProductSubcategoryKey และ ProductCategoryKey

จากนั้นเปลี่ยนชื่อ Query นี้เป็น dProduct และเอา Enable Load ของ ProductCategory และ ProductSubcategory ออกไปซะ

สรุปก่อนจะ Close & Apply

  • เราจะไม่โหลด Geography, ProductCategory, ProductSubcategory แล้ว
  • และมีการเปลี่ยนชื่อตาราง Dimension ทั้งหมดให้ขึ้นต้นด้วยตัว d (จะได้เห็นชัดๆ)
  • และชื่อตารางหลักที่เป็น Fact ให้ขึ้นต้นด้วยตัว f

จากนั้นกด Close & Apply

จะเห็นว่าตอนนี้เราได้ Data Model แบบ Star Schema เรียบร้อยแล้ววว

แล้ว Data Model มีกี่แบบ?

ในความเป็นจริงแล้ว Data Model ที่จะใช้ใน Power BI ไม่ได้มีแค่ Star Schema นะครับ มันสามารถมีได้อีกหลายอย่างเลย เช่น

  • กรณีที่ต้องมี Fact Table มากกว่า 1 ตัว (เพราะเป็น Fact คนละเรื่องกัน) เราจะพยายามทำให้ใช้ตาราง Dimension ร่วมกัน (เช่น Actual vs Budget หรือ การ Order vs การการขายของ)
  • กรณีที่ตาราง Date อันเดียว เชื่อมกับ Field วันที่หลาย Field ที่อยู่ในตารางเดียวกันก็มีอีก…

แต่สำหรับพื้นฐานสุดๆ ผมแนะนำให้หักใช้ Power BI กับ Model Star Schema ง่ายๆ นี้ก่อนครับ ใครที่สนใจแบบ advance ขึ้นลองดูคลิปของ SQLBI อันนี้ก่อนได้ (ฟรี)

ตอนนี้จบเท่านี้

เดี๋ยวตอนถัดไป เราจะมาสร้างตารางวันที่ด้วยสูตร DAX กันล่ะ!

สารบัญ Series Power BI

  • POWER BI ตอนที่ 01: POWER BI คืออะไร?
  • POWER BI ตอนที่ 02: พื้นฐาน EXCEL ที่สำคัญก่อนจะเรียนรู้ POWER BI
  • POWER BI ตอนที่ 03: ภาพรวมการใช้งาน POWER BI DESKTOP
  • POWER BI ตอนที่ 04: สร้าง REPORT แรก ใน POWER BI
  • POWER BI ตอนที่ 05: วิธีการ DRILL เพื่อเจาะลึกข้อมูลใน REPORT
  • POWER BI ตอนที่ 06: การปรับแต่งสีใน VISUAL ด้วย CONDITIONAL FORMAT
  • POWER BI ตอนที่ 07: เริ่ม GET DATA ตั้งแต่ไฟล์ยังว่างเปล่า
  • POWER BI ตอนที่ 08: สร้าง DATA MODEL ที่เหมาะสม
  • POWER BI ตอนที่ 09: สร้าง DATE TABLE ด้วย DAX
  • POWER BI ตอนที่ 10: เรียนรู้ DAX เบื้องต้น
  • POWER BI ตอนที่ 11: เรียนรู้ DAX Table Function – FILTER
  • POWER BI ตอนที่ 12: DISTINCT, VALUES, ALL และผองเพื่อน
  • POWER BI ตอนที่ 13: CALCULATE ฟังก์ชันที่ทรงพลังที่สุดใน DAX
  • Power BI ตอนที่ 14: Context Transition และ พลังแฝงใน Measure
  • Power BI ตอนที่ 15: วิธีดึงค่าจาก Slicer มาคำนวณใน Report
  • Power BI ตอนที่ 16 : เดินทางข้ามเวลาไปกับ Time Intelligence DAX Function
  • Power BI ตอนที่ 17 : วิธีทำรายงานเทียบเป้าหมาย Target vs Actual
  • Power BI ตอนที่ 18 : วิธีการกระจายเป้า Allocate Target ด้วย DAX
  • Power BI ตอนที่ 19 : การปรับ Cross Filter Direction เพื่อคำนวณค่าในตาราง Dimension
  • ส่วนเสริม
  • การคำนวณต้นทุนแบบ FIFO ด้วย DAX
  • แสดงข้อมูลสรุปแบบ Top N + Others (ฉบับเทพเอ็กเซล)
  • การวิเคราะห์ Event ที่มีช่วงเวลาเริ่มต้นกับสิ้นสุดคนละวัน
  • เปรียบเทียบ MAX vs LASTDATE ในภาษา DAX

ใครสนใจอยากเรียนเป็นคลิปวีดีโอ ผมมีคอร์สออนไลน์ที่สอน Power BI ตั้งแต่พื้นฐาน สามารถไปดูรายละเอียดได้ที่นี่

This image has an empty alt attribute; its file name is Powerful-Data-Power-BI-1024x538.png
แชร์ความรู้ให้เพื่อนๆ ของคุณ
499    
499    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI
  • วิธีกำหนดท่าทางแบบให้ได้ดั่งใจด้วย ControlNet ใน Stable Diffusion [Part4]
  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย Excel Power Query

Categories

Tags

ai collection concepts copy database Data Model data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro inventory len link logic lookup match m code merge mid overview paste pivot power query right row solver sort speed split substitute table text time tips trim vba vlookup

Archives

  • April 2023 (3)
  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT