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 สอน Python / Programming
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

แนวทางการใช้ Python ใน Power BI

Categories 📂

Power BI, Python

Tags 🏷️

power bi, python

จากที่เราได้เรียนรู้การใช้ Python เบื้องต้นกันมาบ้างแล้วในหลายๆ ตอน คราวนี้เราจะมาใช้ Python กันในโปรแกรม Power BI กันบ้าง ซึ่งในบทความนี้เพื่อนๆ จะได้เรียนรู้ว่ามันเอามาใช้ด้วยกันได้ยังไง ในลักษณะไหน ต้องทำอะไรบ้าง? โดยตัวอย่างที่แสดงจะไม่ได้ทำอะไรซับซ้อนเพื่อความเข้าใจง่ายนะครับ (คือเรื่องที่ทำให้ดูใน Python ในบทความนี้ ทำใน Power Query โดยตรงก็ได้ แค่จะแสดงให้เห็น Concept เฉยๆ)

สารบัญ

  • เตรียมความพร้อม
  • ใช้ Python ในขั้นตอน Get Data
  • ใช้ Python ในขั้นตอน Transform
  • ใช้ Python ใน Visual
  • สรุป

เตรียมความพร้อม

ก่อนอื่น เราจะต้อง Download แล้ว Install Python ลงเครื่องคอมพิวเตอร์ของเราก่อน (ใช้แค่ Collab เหมือนเดิมไม่ได้แล้ว) ไม่งั้นมันจะใช้ Python ใน Power BI ไม่ได้นะครับ

ซึ่งตอน Install ผมแนะนำให้ติ๊กว่าให้เอา Python add เข้า PATH ของเครื่องด้วยนะครับ

จากนั้น ให้ ทำการ Install Package ที่เกี่ยวข้อง และคิดว่าจะใช้งานใน Power BI ด้วย โดยใส่คำสั่งเหล่านี้ที่ Command Prompt ของ Windows (ไม่ใช่ของ Python นะ)

pip install pandas 
pip install matplotlib 
pip install numpy 
pip install seaborn

พอ Install ทุกอย่างที่จำเป็นหมดแล้ว ก็เข้าโปรแกรม Power BI ได้ ซึ่งเมื่อลองเข้าไปใน Option จะเห็นว่า มันมองเห็น Python ที่เรา Install ลงในเครื่องแล้ว

ส่วนเรื่องของ IDE จะปล่อยเป็นค่า Default ก็ได้ แต่ของผมเลือก Other แล้ว browse ไปที่ Microsoft Visual Studio Code ตามตำแหน่งนี้ C:\Users\ชื่อUser\AppData\Local\Programs\Microsoft VS Code\Code.exe

ใช้ Python ในขั้นตอน Get Data

เราจะลองใช้ Python ในขั้นตอนของการ Get Data โดยไปที่ Get Data -> More…-> Other -> Python Script

Concept คือ Power Query จะมองเห็นตัวแปรที่เก็บ DataFrame ของ Python แยกเป็นคนละ Object กัน เช่น ใส่คำสั่งนี้ลงไปใน Script

import pandas as pd
 TestVar1=pd.read_csv("https://raw.githubusercontent.com/ThepExcel/download/master/ThepExcelsample.csv")
 data = {'แอปเปิ้ล': [1, 3, 6, 8],'มะละกอ': [10, 30, 50, 90]}
 TestVar2=pd.DataFrame(data)

พอกด ok จะเห็นเป็น 2 Object ดังนี้ (สังเกตว่ามองไม่เห็นตัวแปร data ที่เป็น Dictionary)

ให้เลือก TestVar1 แล้วกด Transform เพื่อทำงานต่อ

สังเกตว่าใน step Source จะมีคำสั่ง = Python.Execute( xxxx ) โผล่มา ซึ่งคำสั่งนี้ใช้ได้ใน Power Query ของ Power BI เท่านั้น ไม่สามารถเอาไปใช้ใน Power Query ของ Excel ได้นะครับ

ใช้ Python ในขั้นตอน Transform

พอ Load Data เข้ามาได้แล้ว เราก็สามารถ Transform โน่นนี่นั่นใน Power Query Editor ได้ตามปกติเลย เช่น ในที่นี้ผมจะ Filter ผู้ขายเป็น sales ก เท่านั้น

จากนั้นเวลาเราจะใช้ Python ทำงานต่อ เราสามารถไปที่ Transform -> Run Python Script ได้

จากนั้นมันจะมี comment บอกว่า #’dataset’ holds the input data for this script นั่นก็แปลว่าตัวแปรชื่อว่า dataset เป็นตัวเก็บข้อมูลก่อนที่จะ run Script ล่าสุดนี้นั่นเอง

เช่น ถ้าผมเขียนใน Script ว่า

#'dataset' holds the input data for this script
dataset2=dataset.copy()  #ลองสร้าง data เป็นอีกตัวนึง
dataset2['ยอดขาย']=dataset2['ราคาต่อชิ้น']*dataset2['จำนวนชิ้น']      #สร้างคอลัมน์ยอดขาย

ถ้ามันขึ้นเตือนเรื่อง Privacy ให้เลือกทุกอันเป็น Public ให้หมดแล้วกด ok

ก็จะมี Step Run Python script โผล่มา โดยเห็นผลลัพธ์เป็น 2 Object แยกจากกัน ซึ่ง dataset2 จะมีคอลัมน์ยอดขายเพิ่มขึ้นมาด้วย

สมมติว่าเราจะเอา dataset2 นี่แหละไปทำงานต่อ ก็คลิ๊กที่ทำคำว่า table ได้เลย เพื่อ drill down ลงไปใน object นั้น

จากนั้นกด Close & Apply เพื่อเอา Data เข้าไปใน Data Model ได้เลย

ใช้ Python ใน Visual

เราสามารถใช้ Python สร้าง Visual ได้ด้วย ซึ่งผมจะใช้ Matplotlib ในการสร้างแล้วกัน

ก่อนอื่นให้คลิ๊กที่ Visual ที่มี icon เป็นคำว่า Py ได้เลย (Py=Python)

มันจะบอกว่าให้ลาก Field ลงมาที่ block Values เพื่อเริ่มเขียน Script ได้ งั้นเราก็ลากเลย เอา TXID, ยอดขาย, สินค้า, วิธีชำระเงิน ลงมาแล้วกัน

สังเกตว่ามันมีการ Comment บอกเราว่า dataset ของเราได้คัดเลือกเอา Field 4 ตัวนั้นลงมาแล้ว และมีการกำจัดข้อมูลที่ซ้ำออกให้ด้วย (นี่คือสาเหตุที่ผมต้องเอา TXID ลงมา ไม่งั้นมันลบข้อมูลหายเหี้ยนแน่)

จากนั้นใน Script ดังนี้เพื่อลองสร้าง Boxplot ของยอดขายขึ้นมา แล้วกด Run Script

# Paste or type your script code here:
import matplotlib.pyplot as plt 
plt.boxplot(dataset['ยอดขาย'])
plt.show()

พอได้กราฟออกมา เราสามารถสร้าง Visual ตัวอื่นให้มา Interact กับกราฟที่เกิดจาก Python ได้ด้วย เช่น ผมเอาลูกค้ามาเป็น Slicer มันก็ยังสามารถ Filter ข้อมูลในกราฟของ Python ได้ด้วย

ทีนี้จะทำกราฟได้ดีแค่ไหน ก็ขึ้นกับความสามารถด้าน Python ของเพื่อนๆ แล้วล่ะ

สรุป

สรุปแล้ว เราสามารถใช้ Python ได้ตั้งแต่ขั้นตอน Get Data / Transform Data และ การทำ Visual ด้วยเลย ซึ่งหากใช้ Python ได้คล่องๆ ก็จะทำอะไรได้มากกว่าที่ Power BI ปกติทำได้อีกมาก ที่ผมคิดว่าเหมาะ เช่น การทำ Web Scrapping รวมถึงการใช้ Regex แบบซับซ้อน เป็นต้น (แต่ตัวอย่างนี้ยังไม่ได้แสดงถึงจุดนั้น)

ไว้ผมหัด Python เก่งๆ แล้วจะมาแสดงตัวอย่างเคสที่เหมาะกับการใช้ Python ให้ดูอีกทีนะครับ

ว่าแล้วผมก็ขอไปหัดใช้ Python ต่อก่อนล่ะครับ^^

แชร์ความรู้ให้เพื่อนๆ ของคุณ
540    
540    

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

  • Facebook
  • YouTube

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

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

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

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

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

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน 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 table data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro irr len link logic lookup match m code merge mid overview paste pivot power query row sort speed split substitute table text textjoin time tips trim vba vlookup

Archives

  • 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