教學課程:使用 Power BI Desktop 來分析 Facebook 資料Tutorial: Analyze Facebook data by using Power BI Desktop

在本教學課程中,您將了解如何從 Facebook 匯入資料,並用於 Power BI Desktop。In this tutorial, you learn how to import data from Facebook and use it in Power BI Desktop. 您將連線 Power BI Facebook 頁面並從中匯入資料、將轉換套用至匯入的資料,然後在報表視覺效果中使用資料。You'll connect and import data from the Power BI Facebook page, apply transformations to the imported data, and use the data in report visualizations.

警告

由於 Facebook 應用程式權限限制,此文章中所述的連接器功能目前無法正常運作。Due to Facebook App permission restrictions, the connector capabilities described in this article are not currently working properly. 我們正與 Facebook 合作,以盡快恢復此功能。We’re working with Facebook to return this functionality as soon as possible.

連線至 Facebook 頁面Connect to a Facebook page

本教學課程使用來自 Microsoft Power BI Facebook 頁面的資料。This tutorial uses data from the Microsoft Power BI Facebook page. 除了個人的 Facebook 帳戶之外,您不需要任何特殊認證就能連線此頁面並匯入資料。You don't need any special credentials to connect and import data from this page except for a personal Facebook account.

  1. 開啟 Power BI Desktop,接著在 [開始使用] 對話方塊中選取 [取得資料] ,或在 [首頁] 功能區索引標籤中選取 [取得資料] ,然後選取 [更多] 。Open Power BI Desktop and select Get data in the Getting Started dialog box, or in the Home ribbon tab, select Get Data and then select More.

  2. 在 [取得資料] 對話方塊中,從 [線上服務] 群組選取 [Facebook] ,然後選取 [連線] 。In the Get Data dialog box, select Facebook from the Online Services group, and then select Connect.

    取得資料

    對話方塊隨即出現,提醒您使用第三方服務的風險。A dialog box appears to alert you to the risks of using a third-party service.

    第三方服務警告

  3. 選取 [繼續] 。Select Continue.

  4. 在 [Facebook] 文字方塊中輸入名稱 microsoftbi 作為 使用者名稱,從 [連線] 下拉式清單中選取 [貼文] ,然後選取 [確定] 。In the Facebook dialog box, enter the page name microsoftbi as the user name, select Posts from the Connection dropdown, and then select OK.

    連線

  5. 當提示輸入認證時,請登入 Facebook 帳戶,並允許 Power BI 存取您的帳戶。When prompted for credentials, sign in to your Facebook account, and allow Power BI access to your account.

    認證

    連線到 Power BI Facebook 頁面之後,您就會看見頁面貼文資料的預覽。After you connect to the Power BI Facebook page, you see a preview of the page's posts data.

    資料預覽

使匯入的資料成形和轉換Shape and transform the imported data

假設您想要查看並顯示一段時間內具有最多留言的貼文,但您注意到在貼文資料預覽中,created_time 資料難以閱讀和理解,且完全沒有任何留言資料。Suppose you want to see and show which posts have the most comments over time, but you notice in the posts data preview that the created_time data is hard to read and understand, and there's a lack of comments data. 若要充分利用資料,您需要執行某種資料成形和清理作業。To pull the most out of it, perform some shaping and cleansing of the data. 若要執行此操作,請使用 Power BI Desktop 的 Power Query 編輯器,在將資料匯入 Power BI Desktop 之前或之後加以編輯。To do so, use the Power BI Desktop Power Query Editor to edit the data, before or after importing it into Power BI Desktop.

分割日期/時間資料行Split the date/time column

首先,分隔 created_time 資料行中的日期和時間值,使其更容易閱讀。First, separate the date and time values in the created_time column to be more readable.

  1. 在 Facebook 資料預覽中,選取 [編輯] 。In the Facebook data preview, select Edit.

    資料預覽編輯

    Power BI Desktop 的 Power Query 編輯器會在新視窗中開啟,並顯示來自 Power BI Facebook 頁面的資料預覽。The Power BI Desktop Power Query Editor opens in a new window and displays the data preview from the Power BI Facebook page.

    Power Query 編輯器

  2. 選取 created_time 資料行。Select the created_time column. 請注意,該資料行是 文字 資料類型,在資料行標頭中會以 ABC 圖示來表示。Notice that it's a Text data type, as denoted by an ABC icon in the column header. 以右鍵按一下標頭,並在下拉式清單中選取 [分割資料行] > [依分隔符號] 。Right-click the header and select Split Column > By Delimiter in the drop-down list. 或者,您也可以在功能區 [首頁] 索引標籤的 [轉換] 群組下方,選取 [分割資料行] > [依分隔符號] 。Or, select Split Column > By Delimiter under the Transform group in the Home tab of the ribbon.

    依分隔符號分割資料行

  3. 在 [依分隔符號分割資料行] 對話方塊中,從下拉式清單中選取 [自訂] 、在輸入欄位中輸入 T (作為 created_time 值時間部分開頭的字元),然後選取 [確定] 。In the Split Column by Delimiter dialog box, select Custom from the dropdown, enter T (the character that starts the time part of the created_time values) in the input field, and then select OK.

    [依分隔符號分割資料行] 對話方塊

    該資料行會分割成兩個資料行,各自包含 T 分隔符號前後的字串。The column splits into two columns that contain the strings before and after the T delimiter. 新資料行分別命名為 created_time.1created_time.2The new columns are named created_time.1 and created_time.2, respectively. Power BI 已自動偵測到並將第一個資料行的資料類型變更為 Date,以及將第二個資料行變更為 Time,且已將日期和時間值格式化,使其更容易閱讀。Power BI has automatically detected and changed the data types to Date for the first column and Time for the second column, and formatted the date and time values to be more readable.

  4. 重新命名這兩個資料行。Rename the two columns. 選取 created_time.1 資料行,然後從功能區 [轉換] 索引標籤的 [任何資料行] 群組中選取 [重新命名] 。Select the created_time.1 column, and then select Rename in the Any Column group of the Transform tab in the ribbon. 或者,您也可以按兩下資料行標頭並輸入新的資料行名稱 created_dateOr, double-click the column header and enter the new column name, created_date. created_time.2 重複執行此操作,並將其命名為 created_timeRepeat for the created_time.2 column, and rename it created_time.

    新的日期和時間資料行

展開巢狀資料行Expand the nested column

日期和時間資料現在會以您想要的方式呈現,只要展開巢狀資料行即可公開留言資料。Now that the date and time data are as you want them, you can expose comments data by expanding a nested column.

  1. 選取 object_link 資料行頂端的 展開圖示 圖示,以開啟 [展開/彙總] 對話方塊。Select the expand icon icon at the top of the object_link column to open the Expand/Aggregate dialog box. 選取 [connections] (連接),然後選取 [確定] 。Select connections, and then select OK.

    展開 object_link

    資料行標題會變更為 object_link.connectionsThe column heading changes to object_link.connections.

  2. 選取 object_link.connections 資料行上方的 展開圖示圖示、然後依序選取 [留言] 和 [確定] 。Select the expand icon icon at the top of the object_link.connections column, select comments, and then select OK. 資料行標題會變更為 object_link.connections.commentsThe column heading changes to object_link.connections.comments.

  3. 選取 object_link.connections.comments 資料行上方的 展開圖示 圖示,但這次在對話方塊中選取 [彙總] 而不是 [展開] 。Select the expand icon icon at the top of the object_link.connections.comments column, and this time select Aggregate instead of Expand in the dialog box. 選取 [# id 的計數] ,然後選取 [確定] 。Select # Count of id, and then select OK.

    彙總留言

    資料行現在會顯示每個訊息的留言數目。The column now displays the number of comments for each message.

  4. object_link.connections.comments.id 的計數 資料行重新命名為 Number of comments (留言數目)。Rename the Count of object_link.connections.comments.id column to Number of comments.

  5. 選取 [Number of comments] (留言數目) 資料行標頭旁的向下箭號,然後選取 [遞減排序] 以查看已排序的貼文 (從最多留言數目到最少留言數目)。Select the down arrow next to the Number of comments column header and select Sort Descending to see the posts sorted from most to fewest comments.

    每個訊息的留言

檢閱查詢步驟Review query steps

當您在 Power Query 編輯器中使資料成形並轉換時,每個步驟都會記錄於 [Power Query 編輯器] 視窗右側 [查詢設定] 窗格的 [套用的步驟] 區域中。As you shape and transform data in the Power Query Editor, each step is recorded in the Applied Steps area of the Query Settings pane at the right side of the Power Query Editor window. 您可以反向逐步執行 [套用的步驟] 來查看所做變更,並視需要編輯、刪除或重新排列這些步驟。You can step back through the Applied Steps to see exactly what changes you made, and edit, delete, or rearrange them if necessary. 修改這些步驟時請小心,因為變更先前的步驟可能會中斷後續步驟。Use caution when modifying these steps, because changing preceding steps can break later steps.

套用到目前為止的資料轉換之後,[套用的步驟] 看起來應該如下:After applying the data transformations so far, your Applied Steps should appear as follows:

套用的步驟

提示

[套用的步驟] 本質上是以 Power Query M 公式語言所撰寫的公式。Underlying the Applied Steps are formulas written in the Power Query M formula language. 若要查看和編輯公式,請在功能區 [常用] 索引標籤的 [查詢] 群組中選取 [進階編輯器] 。To see and edit the formulas, select Advanced Editor in the Query group of the Home tab of the ribbon.

匯入轉換的資料Import the transformed data

當您處理完資料之後,請在功能區的 [首頁] 索引標籤中選取 [關閉並套用] > [關閉並套用] ,以將資料匯入至 Power BI Desktop。When you're satisfied with the data, select Close & Apply > Close & Apply in the Home tab of the ribbon to import it into Power BI Desktop.

關閉並套用

對話方塊會顯示將資料載入至 Power BI Desktop 資料模型的進度。A dialog box displays the progress of loading the data into the Power BI Desktop data model.

正在載入資料

載入資料之後,資料會在 [報表] 檢視中顯示為 [欄位] 窗格中的新查詢。Once the data is loaded, it appears in the Report view as a new query in the Fields pane.

螢幕擷取畫面,其中顯示稱為 Query1 的查詢其可用欄位。

在報表視覺效果中使用資料Use the data in report visualizations

現在您已從 Facebook 頁面匯入資料,便可以使用視覺效果,快速且輕鬆地深入解析您的資料。Now that you have imported data from the Facebook page, you can quickly and easily gain insights about your data by using visualizations. 建立視覺效果很簡單,只要選取一個欄位,或將欄位從 [欄位] 清單拖曳至報表畫布即可。Creating a visualization is easy, just select a field or drag it from the Fields pane onto the report canvas.

建立橫條圖Create a bar chart

  1. 在 Power BI Desktop [報表] 檢視中,從 [欄位] 窗格選取 [訊息] ,或將其拖曳至報表畫布。In Power BI Desktop Report view, select message from the Fields pane, or drag it onto the report canvas. 畫布上會出現一個顯示所有貼文訊息的資料表。A table showing all post messages appears on the canvas.

    顯示 [報表] 檢視的螢幕擷取畫面,其中包含訊息清單。

  2. 選取該資料表之後,也請選取 [欄位] 窗格中的 [Number of comments] (留言數目) ,或將其拖曳到資料表。With that table selected, also select Number of comments from the Fields pane, or drag it into the table.

  3. 在 [視覺效果] 窗格中,選取 堆疊橫條圖 圖示。Select the Stacked bar chart icon in the Visualizations pane. 資料表會變更為橫條圖,並顯示每個貼文的留言數目。The table changes to a bar chart showing the number of comments per post.

    橫條圖

  4. 選取視覺效果旁邊的 [更多選項] (...),然後選取 [排序依據] > [Number of comments] (留言數目) ,以便依遞減的留言數來排序資料表。Select More options (...) next to the visualization, and then select Sort by > Number of comments to sort the table by descending number of comments.

    請注意,大部分留言都會與 (空白) 訊息建立關聯 (這些貼文可能是故事、連結、影片或其他非文字內容)。Notice that the most comments were associated with (Blank) messages (these posts may have been stories, links, videos, or other non-text content).

  5. 若要篩選出空白列,請從 [篩選] 窗格選取 [訊息是 (全部)] 、選取 [全選] ,然後選取 [(空白)] 以將其取消選取。To filter out the blank rows, select message is (All) from the Filters pane, select Select all, and then select (Blank) to deselect it.

    [篩選] 項目會變更為 [訊息不是 (空白)] ,而 (空白) 列會從圖表視覺效果中消失。The Filters pane entry changes to message is not (Blank), and the (Blank) row disappears from the chart visualization.

    篩選出空白列

將資料表格式化Format the chart

視覺效果變得越來越有趣了,但您在圖表中看不到大部分貼文內容。The visualization is getting more interesting, but you can't see much of the post text in the chart. 顯示更多貼文內容:To show more of the post text:

  1. 使用圖表視覺效果的控點,盡可能將圖表大小調整到最大。Use the handles on the chart visualization to resize the chart to be as large as possible.

  2. 選取圖表後,選取 [視覺效果] 窗格中的 格式 圖示 (滾筒刷)。With the chart selected, select the Format icon (paint roller) in the Visualizations pane.

  3. 選取 Y 軸 旁的向下箭號,然後將 [最大容量] 滑桿拖曳到最右邊 (50% )。Select the down arrow next to Y axis, and drag the Maximum size slider all the way to the right (50%).

  4. 將 [文字大小] 降至 10 pt 以容納更多文字。Reduce the Text size to 10 pt to fit more text.

    格式變更

    圖表現在會顯示更多貼文內容。The chart now shows more of the post content.

    顯示更多貼文

圖表的 X 軸 (留言數目) 不會顯示實際值,且看不到圖表底部。The x axis (number of comments) of the chart doesn't show exact values, and looks lost at the bottom of the chart. 讓我們改用資料標籤:Let's use data labels instead:

  1. 選取 格式 圖示,然後將 X 軸 旁的滑桿設為 [關閉] 。Select the Format icon, and then set the slider for X axis to Off.

  2. 將 [資料標籤] 滑桿設為 [開啟] 。Select the Data labels slider to On.

    現在圖表會顯示每個貼文的確切留言數目。Now the chart shows the exact number of comments for each post.

    套用資料標籤

編輯資料類型Edit the data type

看起來好多了,但資料標籤全都有 .0 小數位數,這可能會造成失焦與誤解,因為 [貼文數目] 必須是整數。That's better, but all the data labels have a .0 decimal place, which is distracting and misleading, because Number of posts must be a whole number. 若要修正這個問題,您需要將 [貼文數目] 資料行的資料類型變更為 整數To fix them, you need to change the data type of the Number of posts column to Whole Number:

  1. 以滑鼠右鍵按一下 [欄位] 窗格中的 [Query1] (或將滑鼠暫留在其上方),然後選取 [更多選項] (...)。Right-click Query1 in the Fields pane, or hover over it and select More options (...).

  2. 從操作功能表中,選取 [編輯查詢] 。From the context menu, select Edit query. 或者,從功能區 [首頁] 索引標籤的 [外部資料] 群組中,選取 [編輯查詢] > [編輯查詢] 。Or, select Edit Queries > Edit Queries from the External data group of the Home tab in the ribbon.

  3. 從 [Power Query 編輯器] 視窗中選取 [Number of comments] (留言數目) 資料行,並遵循下列其中一個步驟來變更資料類型:From the Power Query Editor window, select the Number of comments column, and change the data type by following one of these steps:

    • 選取 [Number of comments] (留言數目) 資料行標頭旁的 1.2 圖示,然後從下拉式清單中選取 [整數] Select the 1.2 icon next to the Number of comments column header, and then select Whole number from the drop-down list
    • 以滑鼠右鍵按一下資料行標頭,然後選取 [變更類型] > [整數] 。Right-click the column header, and then select Change Type > Whole Number.
    • 選取 [資料類型: 十進位數字] (在 [首頁] 索引標籤的 [轉換] 群組中,或 [轉換] 索引標籤的 [任何資料行] 群組中),然後選取 [整數] 。Select Data type: Decimal Number in the Transform group of the Home tab, or in the Any Column group of the Transform tab, and then select Whole Number.

    資料行標頭中的圖示會變更為 123,其表示 整數 資料類型。The icon in the column header changes to 123, denoting a Whole Number data type.

    變更資料類型

  4. 若要套用變更,請選取 [檔案] > [關閉並套用] (或 [檔案] > [套用] ) 以讓 [Power Query 編輯器] 視窗保持開啟。To apply the changes, select File > Close & Apply, or File > Apply to keep the Power Query Editor window open.

    載入變更之後,圖表上的資料標籤就會變成整數。After the changes load, the data labels on the chart become whole numbers.

    具有整數的圖表

建立日期交叉分析篩選器Create a date slicer

假設您想要以視覺化方式顯示貼文一段時間內的留言數目。Suppose you want to visualize the number of comments on posts over time. 您可以建立交叉分析篩選器視覺效果,以篩選不同時間範圍的圖表資料。You can create a slicer visualization to filter the chart data to different time frames.

  1. 選取畫布的空白區域,然後選取 [視覺效果] 窗格中的 交叉分析篩選器 圖示。Select a blank area of the canvas, and then select the Slicer icon in the Visualizations pane.

    空白的交叉分析篩選器視覺效果隨即出現。A blank slicer visualization appears.

    選取交叉分析篩選器圖示

  2. 從 [欄位] 窗格選取 created_date 欄位,或將其拖曳到新的交叉分析篩選器。Select the created_date field from the Fields pane, or drag it into the new slicer.

    交叉分析篩選器會根據欄位的 日期 資料類型而變更為日期範圍滑桿。The slicer changes to a date range slider, based on the field's Date data type.

    日期範圍滑桿交叉分析篩選器

  3. 移動滑桿控點來選取不同的日期範圍,並注意圖表資料如何據以篩選。Move the slider handles to select different date ranges, and note how the chart data filters accordingly. 您也可以選取交叉分析篩選器中的日期欄位並輸入特定日期,或從快顯行事曆中來選擇。You can also select the date fields in the slicer and type in specific dates, or choose them from a calendar popup.

    進行資料配量

將視覺效果格式化Format the visualizations

為圖表提供更清楚且更吸引人的標題:Give the chart a more descriptive and attractive title:

  1. 選取圖表後,請選取 [視覺效果] 窗格中的 格式 圖示,然後選取 標題 旁的下拉式箭號以將其展開。With the chart selected, select the Format icon in the Visualizations pane, and then select the drop-down arrow next to Title to expand it.

  2. 將 [標題文字] 變更為 Comments per post (每篇貼文的留言)。Change the Title text to Comments per post.

  3. 選取 [字型色彩] 旁的下拉式箭號,然後選取綠色以搭配視覺效果的綠色列。Select the drop-down arrow next to Font color, and select a green color to match the green bars of the visualization.

  4. 將 [文字大小] 增加到 10 pt,並將 [字型家族] 變更為 Segoe (Bold)Increase the Text size to 10 pt, and change the Font family to Segoe (Bold).

  5. 嘗試使用其他格式化選項和設定,以變更視覺效果的外觀。Experiment with other formatting options and settings to change the appearance of your visualizations.

    視覺效果

建立更多視覺效果Create more visualizations

如您所見,很容易就能自訂報表中的視覺效果,以便依您想要的方式來呈現資料。As you can see, it's easy to customize visualizations in your report to present the data in ways that you want. 例如,嘗試使用匯入的 Facebook 資料來建立此折線圖,以顯示一段時間內留言數目。For example, try using the imported Facebook data to create this line chart showing the number of comments over time.

折線圖

Power BI Desktop 提供從頭到尾流暢的體驗,包括從各種資料來源取得資料、讓資料成形以符合您的分析需求,乃至於以豐富且互動的方式將此資料視覺化。Power BI Desktop provides a seamless end-to-end experience, from getting data from a wide range of data sources and shaping it to meet your analysis needs, to visualizing this data in rich and interactive ways. 準備好報表之後,您可以將其上傳至 Power BI 服務並依此建立儀表板,並與其他 Power BI 使用者共用。When your report is ready, you can upload it to the Power BI service and create dashboards based on it to share with other Power BI users.

後續步驟Next steps