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 ตอนที่ 14: Context Transition และ พลังแฝงใน Measure

context transition

Categories 📂

DAX Formula

Tags 🏷️

calculate, context transition, filter context, row context

จากในตอนที่แล้วเราได้ทำความรู้จัก CALCULATE กันไปแล้วว่ามันเป็นฟังก์ชันที่เอาไว้เปลี่ยน Filter Context ได้ตามต้องการ ในตอนนี้เราจะมาทำความรู้จักพลังที่แท้จริงของมัน ซึ่งก็คือ

  • CALCULATE สามารถเปลี่ยน Row Context ให้เป็น Filter Context ได้ เรียกว่า Context Transition
  • Measure ทุกตัวมี CALCULATE แฝงอยู่ข้างในเสมอ

สารบัญ

  • ไฟล์ประกอบ
  • CALCULATE กับ Context Transition
  • Measure ทุกตัวมี CALCULATE แฝงอยู่ข้างในเสมอ
  • CALCULATE กับ Row Context ของ Iterator
    • TotalRevenue เฉลี่ยรายร้านค้า
    • TotalRevenue เฉลี่ยราย Category สินค้า
    • TotalProfit ที่มากสุดรายสินค้า
  • สารบัญ Series Power BI

ไฟล์ประกอบ

ใช้ไฟล์เดิมจากตอนก่อนหน้าได้เลยนะครับ หรือจะใช้อันนี้ก็ได้ ซึ่งเรามี Data Model แบบนี้

มาดูเรื่องแรกกันก่อนครับ

CALCULATE กับ Context Transition

หากเรานำ CALCULATE ไปใช้ในบริบทที่มี Row Context มันจะเปลี่ยน Row Context นั้นให้กลายเป็น Filter Context … ฟังแล้วไม่เข้าใจเลยซักนิดใช่มั้ยครับ? งั้นมาดูตัวอย่างดีกว่าครับ

สมมติว่าผมไปที่ Data View แล้วไปที่ Table dStores แล้วสร้างคอลัมน์ใหม์โดยที่ผมอยากจะแสดงยอดขายของแต่ Store ไว้ในคอลัมนี้

ผมสามารถทำได้โดยกด New Column แล้วใส่สูตรแบบนี้ได้

StoreRevenue = CALCULATE([TotalRevenue])

ผลลัพธ์ที่ได้มันจะใช้ CALCULATE คำนวณ TotalRevenue โดยเปลี่ยน Filter Context ให้เป็นไปตามค่าในแถวเดียวกับมัน

สมมติว่าเราลองวิเคราะห์ Filter ของบรรทัดแรกจะมีดังนี้

  • StoreKey=1
  • StoreType=Store
  • StoreName=Contoso Seattle No.1 Store
  • Status=On
  • CloseReason=blank
  • EmployeeCount=17
  • SellingAreaSize=462
  • GeographyType=City
  • ContinentName=North America
  • RegionCountryName=United Sates

พอมัน Filter ตามนี้ก็จะได้ว่า StoreRevenue = 1,615,303.83

นี่แหละคือการเปลี่ยน Row Context เป็น Filter Context ซึ่งเรียกว่า Context Transition ซึ่งทำให้ผลคำนวณ Revenue แต่ละบรรทัดออกมาไม่เท่ากัน เพราะว่ามันมี Filter ไม่เหมือนกันนั่นเอง

ระวัง! ถ้าคิดดูให้ดู เวลาเราคำนวณแบบนี้ หากในตารางมีคอลัมน์ที่ Unique ไม่ซ้ำกัน ผลลัพธ์ก็จะไม่มีปัญหา แต่ถ้าในตารางดันมีบรรทัดที่ Duplicate กันทั้งแถว ค่าที่ได้จากการคำนวณจะเบิ้ลทันที

Measure ทุกตัวมี CALCULATE แฝงอยู่ข้างในเสมอ

ประเด็นถัดไปก็คือ Measure ทุกตัวมี CALCULATE แฝงอยู่เสมอ อันนี้เข้าใจง่ายครับ

แปลว่า การเขียนสูตรว่า

StoreRevenue = CALCULATE([TotalRevenue])

สามารถเขียนเหลือแค่ Measure ก็ได้ เพราะมันมี CALCUATE อยู่ในตัวอยู่แล้ว เช่น

StoreRevenue = [TotalRevenue]

ซึ่งจะได้ผลลัพธ์เท่าเดิมทุกประการ แต่เขียนสั้นลงไปอีก

และนี่ก็คือความลับอีก 2 ข้อที่เกี่ยวกับ CALCULATE ครับ จะเห็นว่ามันสั้นๆ ดูเรียบง่าย ไม่มีอะไร แต่พอใช้งานจริง หลายๆ คนอาจลืมนึกถึงเรื่องพวกนี้ ลืมนึกไปว่า Measure มี CALCULATE อยู่ และมันก็จะเกิด Context Transition ด้วย

เพื่อให้เห็นความสามารถที่ลึกซึ้งมากขึ้น ลองมาดูตัวอย่างการใช้ CALCULATE กับ Row Context ที่น่าสนใจขึ้นดีกว่าครับ

CALCULATE กับ Row Context ของ Iterator

สมมติว่าผมอยากจะแสดงยอดขายเฉลี่ยของแต่ละ Store โดยแบ่งตาม Product Category เราจะทำยังไง?

ก่อนอื่นผมจะต้องทำ Visual ที่มี Product Category อยู่ที่ Row ไว้ก่อน (เพราะเราจะแบ่งตาม Product Category ไง) ดังนี้

ทีนี้เราจะทำให้ Values หรือการคำนวณ คำนวณแบบไหนล่ะ? เราอยากได้ยอดขายเฉลี่ย ถ้าเราลาก Total Revenue ลงมา มันก็คือยอดขายรวม หรือถ้าเราลาก Field ยอดขายลงมาแล้วกด Average มันก็คือยอดขายเฉลี่ยแต่ละ Transaction ไม่ใช่ยอดขายเฉลี่ยของแต่ละร้านค้า…

ทางออกคือ การสร้าง Measure ยอดขายเฉลี่ยของแต่ละร้านขึ้นมา

Concept คือ เราจะใช้ Iterator เช่น AVERAGEX มาช่วย

AVERAGEX ( <Table>, <Expression> )

AVERAGEX จะทำการคำนวณ <Expression> ในแต่ละแถวของ <Table> (เรียกว่า Iterate) จากนั้นจำไว้ในใจ (สร้างตารางจำลอง) พอครบทั้ง Table แล้วก็เอามา Average กันซะ

สูตรที่จะใช้คือ

AverageStoreSales = AVERAGEX(DISTINCT(dStores[StoreKey]),[TotalRevenue])

Table คือ DISTINCT(dStores[StoreKey]) ซึ่งก็คือ Store แต่ละร้านแบบไม่ซ้ำกัน

Expression คือ [TotalRevenue] ซึ่งคือ Measure และมันมี CALCULATE แฝงอยู่ข้างใน ทำให้เกิด Context Transition ในการคำนวณ [TotalRevenue] ของ StoreKey แต่ละบรรทัดนั่นเอง

พอคำนวณ TotalRevenue ของแต่ละ StoreKey ได้ก็จับมา Average กันให้เรา จบ.. ได้ผลลัพธ์แบบนี้

แต่ละสินค้าก็จะมียอดขายเฉลี่ยต่อร้านไม่เท่ากัน และตรง Total ก็คือยอดขายเฉลี่ยต่อร้านแบบที่ไม่ได้แบ่งตามสินค้า (พูดง่ายๆ ก็คือรวมสินค้าทุกประเภทนั่นแหละ) เลขมันก็เลยเยอะ

การใช้ CALCULATE ผสมกับพวก SUMX AVERAGEX MAXX RANKX เพื่อแปลง Row Context เป็น Filter Context เป็นอะไรที่ใช้ในชีวิตจริงบ่อยมาก

จะเห็นว่า TotalRevenue คำนวณจากตาราง fSales ดังนั้นจึงสามารถ Filter จากตารางอื่นได้เมื่อเราใช้ พวก SUMX AVERAGEX MAXX สร้างตารางจำลองขึ้นมา แล้วเกิด Context Transition ขึ้น

ยกตัวอย่างเช่น

TotalRevenue เฉลี่ยรายร้านค้า

AverageStoreRevenue = 
AVERAGEX(DISTINCT(dStores[StoreKey]),[TotalRevenue])

TotalRevenue เฉลี่ยราย Category สินค้า

AvgProductCategoryRevenue = 
AVERAGEX( DISTINCT(dProduct[ProductCategory]) , [TotalRevenue] )

TotalProfit ที่มากสุดรายสินค้า

MaxProductCategoryProfit = 
MAXX( DISTINCT(dProduct[ProductCategory]) ,[TotalGrossProfit] )

ซึ่งจะเห็นว่าเราสามารถสร้างมุมมองต่างๆ เพิ่มได้ไม่จำกัด หาก Measure หลักนั้นคำนวณจากข้อมูลซึ่งอยู่ที่ Fact Table แต่ถ้า Measure หลักไม่ได้คำนวณจากคอลัมน์ใน Fact Table จะต้องทำตามในบทความนี้ครับ

และนี่ก็คือความเจ๋งของการที่เรารู้ DAX ครับ เพราะเป็นเรื่องที่ PivotTable แบบปกติใน Excel ทำไม่ได้เลย ต้องเขียน DAX Measure ใน PowerPivot หรือ Power BI เท่านั้น ^^

สารบัญ 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
แชร์ความรู้ให้เพื่อนๆ ของคุณ
100    
100    

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

  • Facebook
  • YouTube

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

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

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

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

  • วิธีผสม Model สูตรผสมแบบโดนใจใน Stable Diffusion [Part7]
  • สอนทำรูปตัวเองคู่กับสาว ใน Stable Diffusion [Part6]
  • สอน Train Model ตัวเองใน Stable Diffusion [Part5]
  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • บันได 10 ขั้น เริ่มเรียนรู้ M Code ขุมพลังของ Power Query
  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI

บทความแนะนำ

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

  • 10 Levels การเขียนสูตร Excel ในยุคใหม่
  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ 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 len link logic lookup match matrix mcode m code merge mid overview paste pivot power query row sort speed split stable diffusion substitute table text time tips trim vba vlookup

Archives

  • April 2023 (8)
  • 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