教學課程:使用 Power BI Desktop 的 Facebook 分析Tutorial: Facebook analytics 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 頁面Connect to a Facebook page

本教學課程使用來自 Microsoft Power BI Facebook 頁面 (https://www.facebook.com/microsoftbi) 的資料。This tutorial uses data from the Microsoft Power BI Facebook page (https://www.facebook.com/microsoftbi). 除了個人的 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, or in the Home ribbon tab, select Get Data and then select More....

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

    取得資料

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

    第三方服務警告

  3. 選取 [繼續]。Select Continue. [Facebook] 對話方塊隨即出現。The Facebook dialog box appears.

  4. 在 [使用者名稱] 文字方塊中輸入或貼上頁面名稱 microsoftbi、從 [連接] 下拉式清單中選取 [貼文],然後選取 [確定]。Type or paste the page name microsoftbi into the Username text box, 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 through your account.

    認證

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

    資料預覽

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

您想要查看並顯示歷來具有最多留言的貼文,但您注意到在貼文資料預覽中,created_time 資料難以閱讀和理解,而且完全沒有任何留言資料。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 is no comments data at all. 您需要執行某種資料成形和清理作業,才能充分利用資料。You need to perform some shaping and cleansing of the data to pull the most out of it. 您可以使用 Power BI Desktop 的 Power Query 編輯器,在將資料匯入至 Power BI Desktop 之前或之後加以編輯。You can 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 圖示來表示。Note that it is currently a Text data type, denoted by an ABC icon in the column header. 以滑鼠右鍵按一下標頭,然後在下拉式清單中選取 [分割資料行] > [依分隔符號],或在功能區 [首頁] 索引標籤的 [轉換] 下方選取 [分割資料行] > [依分隔符號]。Right-click the header and select Split Column > By Delimiter in the dropdown, or select Split Column > By Delimiter under Transform in the Home tab of the ribbon.

    依分隔符號分割資料行

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

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

    該資料行會分割成兩個資料行,各自包含 T 分隔符號前後的字串,並分別命名為 created_time.1created_time.2The column splits into two columns that contain the strings before and after the T delimiter and are named created_time.1 and created_time.2, respectively. 請注意,Power BI 已自動偵測到並將第一個資料行的資料類型變更為 Date,以及將第二個資料行變更為 Time,而且已將日期和時間值格式化,使其更容易閱讀。Note that 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. 按兩下各個資料行標頭,或選取每個資料行,然後選取功能區中 [轉換] 索引標籤之 [任何資料行] 群組中的 [重新命名],接著分別輸入新的資料行標頭 created_datecreated_time,藉以將資料行重新命名。Rename the columns by double-clicking each column header, or selecting each column and then selecting Rename in the Any Column Group of the Transform tab in the ribbon, and typing new column headers created_date and created_time, respectively.

    新的日期和時間資料行

展開巢狀資料行Expand the nested column

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

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

    展開 object_link

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

  2. 再次選取 object_link.connections 資料行上方的 展開圖示 圖示、選取 [comments] (留言),然後選取 [確定]。Again 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 header and select Sort Descending to see the Posts sorted from most to fewest comments.

    每個訊息的留言

檢閱查詢步驟Review query steps

當您在 Power Query編輯器中使資料成形並轉換時,每個步驟都會記錄於 [Power Query 編輯器] 視窗右側 [查詢設定] 窗格的 [套用的步驟] 區域中。As you shaped and transformed data in the Power Query Editor, each step was 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 (although this can be risky, because changing preceding steps can break later steps).

套用資料轉換之後,套用的步驟看起來應該如下:After applying the data transformations so far, your Applied Steps should look like the following:

套用的步驟

提示

套用的步驟本質上是以 Power Query 語言 (亦稱為 M 語言) 所撰寫的公式。Underlying the Applied Steps are formulas written in the Power Query Language, also known as the M 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 are 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 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 list.

新增查詢

在報表視覺效果中使用資料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 list onto the report canvas.

建立橫條圖Create a bar chart

  1. 在 Power BI Desktop 報表檢視中,從欄位清單選取 [message] (訊息),或將它拖曳至畫布。In Power BI Desktop Report view, select message from the field list, or drag it onto the 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 list, 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 the ellipsis (...) in the upper right of the visualization, and then select Sort by Number of comments to sort the table by descending number of comments.

    依留言數目排序

  5. 請注意,大部分留言都會與空白訊息相關聯 (這些貼文可能是報導、連結、影片或其他非文字內容)。Note that the most comments were associated with Blank messages (these posts may have been stories, links, videos, or other non-text content). 若要篩選出空白列,請選取 [視覺效果] 窗格底部 [篩選] 下方的 [message (全部)]、選取 [全選],然後選取 [空白] 以將之取消選取。To filter out the Blank row, select message (all) under Filters at the bottom of the Visualizations pane, select Select All, and then select Blank to deselect it. [篩選] 項目會變更為 [message 不是 (空白)],而空白列會從圖表視覺效果中消失。The Filters 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. 使用圖表視覺效果的控點,盡可能將圖表大小調整到最大。Using the handles on the chart visualization, 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 slider next to Maximum size all the way to the right (50%).

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

    格式變更

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

    顯示更多貼文

圖表的 X 軸 (留言數目) 不會顯示實際值,而且看不到圖表底部。The X axis (number of comments) of the chart does not show exact values, and looks lost at the bottom of the chart. 您決定改用資料標籤。You decide to use data labels instead.

  1. 選取格式圖示,然後選取 X 軸旁的滑桿來將它關閉Select the Format icon, and then select the slider next to X-axis to turn it Off.

  2. 選取資料標籤旁的滑桿以將之開啟Select the slider next to Data labels to turn them On. 現在圖表會顯示每個貼文的確切留言數目。Now the chart shows the exact number of comments for each post.

    套用資料標籤

編輯資料類型Edit the data type

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

  1. 若要編輯資料類型,以滑鼠右鍵按一下 [欄位] 清單中的 Query1,或將滑鼠停留在它的上方,並選取更多選項省略符號 (...),然後選取 [編輯查詢]。To edit the data type, right-click Query1 in the Fields list, or hover over it and select the More options ellipsis (...), and then select Edit Query. 您也可以從功能區 [首頁] 索引標籤的 [外部資料] 區域中選取 [編輯查詢],然後從下拉式清單中選取 [編輯查詢]。You can also select Edit Queries from the External data area of the Home tab in the ribbon, and then select Edit Queries from the dropdown. Power BI Desktop 的 Power Query 編輯器 會在另一個視窗中開啟。The Power BI Desktop Power Query Editor opens in a separate window.

    從 [欄位] 清單編輯查詢 從功能區編輯查詢

  2. 在 Power Query 編輯器中,選取 Number of comments (留言數目) 資料行,並透過下列其中一種方式,將資料類型變更為整數In the Power Query Editor, select the Number of comments column, and change the data type to Whole Number by either:

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

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

    變更資料類型

  3. 選取 [關閉並套用] (或僅 [套用]) 以套用變更,同時讓 Power Query 編輯器視窗保持開啟。Select Close & Apply, or just Apply to apply the changes while keeping the Power Query Editor window open. 一旦載入變更之後,圖表上的資料標籤就會變成整數。Once the changes load, the data labels on the chart become whole numbers.

    具有整數的圖表

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

您想要以視覺化方式顯示貼文歷來的留言數目。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. 按一下畫布的空白區域,然後選取 [視覺效果] 窗格中的交叉分析篩選器圖示Click 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 list, 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

您決定要為圖表提供更清楚且更吸引人的標題。You decide to give the chart a more descriptive and attractive title.

  1. 選取圖表之際,選取格式圖示,然後選取下拉式箭號以展開標題With the chart selected, select the Format icon, and select the dropdown arrow to expand Title.
  2. 將 [標題文字] 變更為 Comments per post (每篇貼文的留言)。Change the Title text to Comments per post.
  3. 選取 [字型色彩] 旁的下拉式箭號,然後選取綠色以搭配視覺效果的綠色列。Select the dropdown arrow next to Font color, and select a green color to match the green bars of the visualization.
  4. 將 [文字大小] 增加到 10,並將 [字型家族] 變更為 Segoe (Bold)Increase the Text size to 10, and change the Font family to Segoe (Bold).

將圖表標題格式化

嘗試使用其他格式化選項和設定,以變更視覺效果的外觀。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 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 Power BI service and create dashboards based on it, which you can share with other Power BI users.

後續步驟Next steps