教學課程:使用 Power BI Desktop 的 Facebook 分析Tutorial: Facebook analytics using Power BI Desktop

本教學課程中,您將了解如何將資料從 Facebook匯入並視覺化。In this tutorial you learn how to import and visualize data from Facebook. 在本教學課程中,您將學習如何連接到特定的 Facebook 頁面 (Power BI 頁面)、套用資料轉換步驟,並建立一些視覺效果。During the tutorial you'll learn how to connect to a specific Facebook page (the Power BI page), apply data transformation steps, and create some visualizations.

以下是您要執行的步驟:Here are the steps you'll take:

  • 工作 1: 連接至 Facebook 網頁Task 1: Connect to a Facebook Page
  • 工作 2:使用報表檢視建立視覺效果Task 2: Create visualizations using the Report view

    • 步驟 1:建立樹狀圖視覺效果Step 1: Create a Treemap visualization
  • 工作 3:在查詢檢視中塑造資料Task 3: Shape data in the Query view

    • 步驟 1:將日期時間資料行分成兩個Step 1: Split the date-time column into two
    • 步驟 2:加入相關資料表中的彙總值Step 2: Add an aggregate value from a related table
  • 工作 4:使用報表檢視建立其他視覺效果Task 4: Create additional visualizations using the Report view

    • 步驟 1:載入查詢到您的報表Step 1: Load the query to your report
    • 步驟 2:建立折線圖和橫條圖Step 2: Create a Line chart and a Bar chart

工作 1:連線至 Facebook 網頁Task 1: Connect to a Facebook page

在這項工作中,您要從 Microsoft Power BI Facebook 網站 (URL 是:https://www.facebook.com/microsoftbi) 匯入資料.In this task you import data from the Microsoft Power BI Facebooksite (here's the URL: https://www.facebook.com/microsoftbi ).

任何人都可以連接到該頁面上,並遵循下列步驟 - 不需要任何特殊的認證 (除了您自己的 Facebook 帳戶以外,您在此步驟中會用到)。Anyone can connect to that page, and follow these steps - no special credentials (other than your own Facebook account, which you use in this step) are required.

  1. 在 [開始使用] 對話方塊或 [常用] 功能區索引標籤中,選取 [取得資料]。In the Getting Started dialog or in the Home ribbon tab, select Get Data.
  2. [取得資料] 對話方塊隨即出現,讓您從各種資料來源中選取。The Get Data dialog appears, letting you select from all sorts of data sources. 從 [其他] 群組選取 [Facebook] 。Select Facebook from the Other group.

    當您選取 [連接] 時,對話方塊隨即出現,警示您使用協力廠商服務的風險。When you select Connect, a dialog appears to alert you to the risks of using a third-party service.

  3. 當您選取 [繼續] 後, Facebook 對話方塊隨即出現,您可將頁面名稱 (microsoftbi) 貼到 [使用者名稱] 文字方塊。When you select Continue, the Facebook dialog box appears where you can paste the page name (microsoftbi) into the Username text box. 從 [連線] 下拉式清單選取 [貼文] 。Select Posts from the Connection drop down.

  4. 按一下 [確定] 。Click OK.
  5. 當系統提示您輸入認證時,請使用您的 Facebook 帳戶登入,並允許 Power BI 存取您的帳戶。When prompted for credentials, sign in using your Facebook account and allow Power BI access through your account.

建立對於頁面連線後,您會看到模型載入資料。After establishing a connection to the page, you will see the data being loaded in the model.

其中 查詢編輯器 會顯示資料。From there, Query Editor displays the data. 查詢編輯器 是 Power BI Desktop 的一部分,但在個別視窗中載入,也是在資料連線上執行轉換的地方。Query Editor is part of Power BI Desktop, but loads in a separate window, and is where you perform all your transformations on your data connections.

當資料為您所需時,您可以將它載入 Power BI Desktop。When your data is how you want it, you can load it into Power BI Desktop. 從 [常用] 功能區選取 [載入並關閉]。Select Load & Close from the Home ribbon.

您會看到一個對話方塊,顯示將資料載入 Power BI Desktop 資料模型的進度。You'll see a dialog that displays the progress of loading the data into the Power BI Desktop data model.

載入之後,您將會進入 [報表] 檢視,其中會在右邊的 [欄位] 清單列出資料表的資料行。Once loaded, you’ll be taken to the Report view where the columns from the table are listed in the Field list on the right.

工作 2:使用報表檢視建立視覺效果Task 2: Create visualizations using the Report view

現在您已從頁面載入資料,您可以使用視覺效果,快速且輕鬆地深入了解您的資料。Now that you have landed the data from the page, you can quickly and easily gain insights about your data, using visualizations.

步驟 1 :建立樹狀圖視覺效果Step 1: Create a Treemap visualization

建立視覺效果不難,我們只要從 [欄位清單] 拖曳欄位,並放置在 [報表畫布]。Creating a visualization is easy, we just drag a field from the Field list and drop it on the Report canvas.

將 [type] 欄位拖曳到 [報表] 畫布。Drag the type field onto the Report canvas. Power BI Desktop 會在 [報表畫布] 中建立新的視覺效果。Power BI Desktop creates a new visualization in the Report canvas. 接下來,將 [type] 從 [欄位] (您剛才拖曳至 [報表] 畫布的相同欄位) 拖曳至 [值] 區域,以建立 [橫條圖] 視覺效果。Next, drag type from Fields (the same field you just dragged onto the Report canvas) onto the Value area to create a Bar visualization.

我們可以從 [視覺效果] 窗格選取不同的圖示,輕鬆變更視覺效果的類型。We can easily change the type of visualization by selecting a different icon from the Visualization pane. 讓我們將類型變更為 [樹狀圖] ,方法是從 [視覺效果] 選取其圖示,如下圖所示。Let's change the type to a Treemap by selecting its icon from Visualizations, as shown in the following image.

接下來讓我們加入圖例,然後變更資料點的色彩。Next, let's add a legend, then change the color of a data point. 在 [視覺效果] 窗格中選取 [格式] 圖示;[格式] 圖示看起來像畫刷。Select the Format icon in the Visualizations pane; the Format icon looks like a paintbrush.

當您選取 [圖例] 旁的向下箭號時,此區段會展開,以顯示如何自訂所選取視覺效果的圖例。When you select the down arrow next to Legend, the section expands to show how to customize the legend for the selected visualization. 在本案例中,我們執行下列選項:In this case, we made the following selections:

  • 將 [圖例] 滑桿移動至 [開啟] 讓圖例出現moved the Legend slider to On so a legend would appear
  • 從 [圖例位置] 下拉式清單選取 [右邊] selected Right from the Legend Position drop-down
  • 將 [標題] 滑桿移動至 [開啟] ,讓圖例的標題出現moved the Title slider to On as well, so a title for the legend will appear
  • 在 [類型] 輸入圖例的標題typed in type for the title of the legend

在下圖中,已經進行這些設定,且會在視覺效果中反映。In the following image, those settings are already made and reflected in the visualization.

接下來讓我們變更其中一個資料點的色彩。Next, let's change the color of one of the data points. 這個連結資料點應該是藍色,所以很接近超連結的常見色彩。The link data point should be blue, so it's closer to the common color for hyperlinks.

選取 [資料色彩] 旁邊的箭頭,以展開該區段。Select the arrow next to Data Colors to expand that section. 資料點隨即顯示,同時在每個顏色旁都有選取項目箭頭,讓我們為每個資料點選取不同色彩。The data points are shown, with selection arrows beside each color that allows us to select a different color for each data point.

當您按一下任何資料點旁 [色彩] 方塊的向下箭號時,[色彩選擇] 對話方塊隨即出現,讓您選擇色彩。When you click on the color box down arrow beside any data point, a color selection dialog appears, letting you choose your color. 在此情況下,我們將選擇淺藍色。In this case, we'll choose light blue.

這樣看起來好多了。That's better. 在下圖中,您可以看到色彩如何套用至視覺效果中的資料點,且圖例也會自動更新,如同其在 [資料色彩] 區段的色彩一樣。In the following image, you can see how the color is applied to the data point in the visualization, and that the legend is also automatically updated, as is its color in the Data Colors section.

工作 3:將資料表中的資料定形Task 3: Shape data in the table

現在您已匯入選取的資料表,而且您開始以視覺化方式檢視,您可能會發現您需要執行各種資料圖形和清理步驟,以充分運用您的資料。Now that you have imported the table selected and you start to visualize it, you may notice you need to perform various data shaping and cleansing steps in order to get the most out of your data.

步驟 1 :將日期時間資料行分成兩個Step 1: Split the date-time column into two

在此步驟中,您將分割 created_time 資料行,以取得日期和時間值。In this step, you will split the created_time column to get both the date and time values. 每當您進入 Power BI Desktop,並且想要修改現有的查詢,您就需要啟動 [查詢編輯器] 。Whenever you're in Power BI Desktop and you want to modify an existing query, you need to launch Query Editor. 若要這樣做,請從 [主資料夾] 索引標籤選取 [編輯查詢] 。To do that, select Edit Queries from the Home tab.

  1. 在 [查詢編輯器] 方格中,捲動至右方,直到您找到 created_time 資料行In the Query Editor grid, scroll to the right until you find the created_time column
  2. 以滑鼠右鍵按一下 [查詢預覽] 方格中的資料行標頭,然後按一下 [分割資料行]> [依分隔符號] 以分割資料行。Right-click a column header in the Query Preview grid, and click Split Column > By Delimiter to split the columns. 在分隔符號下拉式清單中選擇 [自訂],然後輸入 "T"。請注意這項作業也適用於 [常用] 功能區索引標籤的 [管理資料行] 群組。Chose Custom in the delimiter drop down and enter “T” Note that this operation is also available in the Home ribbon tab, in the Manage Columns group.

  3. 將建立的資料行分別重新命名為 created_datecreated_timeRename the created columns to created_date and created_time respectively.
  4. 選取新的資料行 created_time,**** 然後在 [查詢檢視] 功能區中,瀏覽至 [加入資料行] 索引標籤,並在 [日期和時間] 群組下選取 [時間] > [小時]Select the new column, created_time, **** and in the Query view ribbon, navigate to the Add Column tab and select Time>Hour under the From Date & Time group. 這會新增新的資料行,其中只有時間的小時部分。This will add a new column that is only the hour component of the time.

  5. 將新的 Hour 資料行類型變更為 [整數],方法是瀏覽至 [常用] 索引標籤,並選取 [資料類型] 下拉式清單,或以滑鼠右鍵按一下資料行,並選取 [轉換] > [整數]Change the type of the new Hour column to Whole Number, by navigating to the Home tab and selecting the Data Type drop down or by right-clicking the column and selecting Transform>Whole Number.

步驟 2 :加入相關資料表中的彙總值Step 2: Add an aggregate value from a related table

在此步驟中,您會加入巢狀值的共用計數,以便您使用於視覺效果中。In this step, you add the count of shares from the nested value so that you can use it in the visualizations.

  1. 繼續向右捲動,直到您看到 [shares] 資料行。Continue scrolling to the right until you see the shares column. 巢狀的值表示我們需要做其他轉換才能取得實際的值。The nested value indicates that we need to do another transform in order to get the actual values.
  2. 在資料行標頭右上角選取 圖示以開啟 [展開/彙總] 產生器。In the top right of the column header, select the icon to open the Expand/Aggregate builder. 選取 [計數] 並點擊 [確定] 。Select count and hit OK. 這會在資料表新增每個資料列的共用計數。This will add the count of the shares for each row in our table.

    資料載入之後,將資料行重新命名為 shares ,方法是用滑鼠右鍵按一下資料行名稱,以滑鼠右鍵在資料行或在 [查詢檢視] 功能區上按一下,選取 [轉換] 索引標籤和 [任何資料行] 群組下的 [重新命名] 。After the data loads, rename the column to shares by double clicking on the column name, right clicking the column or in the Query view ribbon, select Rename under the Transform tab and Any Column group.

  3. 最後,變更新的 shares 資料行類型為 整數Finally, change the type of the new shares column to Whole Number. 您可以選取資料行並變更類型,方法是以滑鼠右鍵按一下資料行,並選取 [轉換] >[整數] 或 **** 瀏覽至 [常用] 索引標籤,並選取 [資料類型] 下拉式清單。With the column selected, the type can be changed by right-clicking the column and selecting Transform>Whole Number or **** by navigating to the Home tab and selecting the Data Type drop down or.

已建立的查詢步驟Query steps created

當您在查詢檢視中執行轉換時,會建立查詢步驟並列於 [查詢設定] 窗格的 [適用步驟] 清單中。As you perform transformations in the Query view, query steps are created and listed in the Query Settings pane, in the APPLIED STEPS list. 每個查詢步驟都有對應的查詢公式,也稱為 "M" 語言。Each query step has a corresponding Query formula, also known as the "M" language.

工作Task 查詢步驟Query step 公式Formula
連接至 Facebook 來源Connect to a Facebook source 來源Source Facebook.Graph ("https://graph.facebook.com/microsoftbi/posts";)Facebook.Graph ("https://graph.facebook.com/microsoftbi/posts";)
分割資料行 以取得您需要的值Split Columns to get the values you need 依分隔符號分割資料行Split Column by Delimiter Table.SplitColumn (Source,"created_time",Splitter.SplitTextByDelimiter("T"),{"created_time.1", "created_time.2"})Table.SplitColumn (Source,"created_time",Splitter.SplitTextByDelimiter("T"),{"created_time.1", "created_time.2"})
變更新資料行的類型 (自動步驟)Change Type of the new columns (automatic step) 變更的類型Changed Type Table.TransformColumnTypes (#"依分隔符號分割資料行",{{"created_time.1", type date}, {"created_time.2", type time}})Table.TransformColumnTypes (#"Split Column by Delimiter",{{"created_time.1", type date}, {"created_time.2", type time}})
資料行重新命名Rename **a column** 重新命名的資料行Renamed Columns Table.RenameColumns (#"變更的類型",{{"created_time.1", "created_date"}, {"created_time.2", "created_time"}})Table.RenameColumns (#"Changed Type",{{"created_time.1", "created_date"}, {"created_time.2", "created_time"}})
插入資料行**Insert **a column** 插入的小時Inserted Hour Table.AddColumn (#"重新命名的資料行", "Hour", each Time.Hour([created_time]), type number)Table.AddColumn (#"Renamed Columns", "Hour", each Time.Hour([created_time]), type number)
變更類型**Change Type ** 變更的類型 1Changed Type1 Table.TransformColumnTypes (#"插入的小時",{{"Hour", type text}})Table.TransformColumnTypes (#"Inserted Hour",{{"Hour", type text}})
展開巢狀表格中的值**Expand **values in a nested table** 展開 [shares]Expand shares Table.ExpandRecordColumn (#"變更的類型 1", "shares", {"count"}, {"shares.count"})Table.ExpandRecordColumn (#"Changed Type1", "shares", {"count"}, {"shares.count"})
資料行重新命名Rename **the column** 重新命名資料行1Renamed Columns1 Table.RenameColumns (#"展開 shares",{{"shares.count", "shares"}})Table.RenameColumns (#" Expand shares",{{"shares.count", "shares"}})
變更類型Change Type 已變更的類型 2Changed Type2 Table.TransformColumnTypes (#"重新命名的資料行 1",{{"shares", Int64.Type}})Table.TransformColumnTypes (#"Renamed Columns1",{{"shares", Int64.Type}})

工作 4:使用報表檢視建立其他視覺效果Task 4: Create additional visualizations using the Report view

現在我們已將資料轉換為圖形,供我們接下來的分析所用,我們可以將產生的資料表載入報表,並建立其他視覺效果。Now that we have converted the data into the shape that we need for the rest of our analysis, we can load the resulting table into our Report and create additional visualizations.

步驟 1 :載入查詢到您的報表Step 1: Load the query to your report

若要將查詢結果載入至報表,我們要從 [查詢編輯器] 選取 [載入並關閉]。In order to load the query results to the report, we need to select Load & Close from Query Editor. 這會將我們所做的變更載入 Power BI Desktop,並關閉 [查詢編輯器] 。This will load our changes into Power BI Desktop, and close Query Editor.

在 Power BI Desktop,必須確定我們是否位於 [報表] 檢視。In Power BI Desktop, we need to make sure we're in Report view. 從 Power BI Desktop 中的左工具列選取上方圖示。Select the top icon from the left bar in Power BI Desktop.

步驟 2 :建立折線圖和橫條圖Step 2: Create a Line chart and a Bar chart

若要建立視覺效果,我們可以將欄位從 [欄位清單] 拖曳到 [報表畫布] 。In order to create a visualization, we can drag fields from the Field list and drop them in the Report canvas.

  1. 將 [共用] 欄位拖曳到 [報表] 畫布,建立橫條圖。Drag the shares field onto the Report canvas, which creates a bar chart. 然後將 created_date 拖曳到圖表上,而 Power BI Desktop 會將視覺效果變更為 [折線圖]。Then drag created_date onto the chart, and Power BI Desktop changes the visualization to a Line Chart.

  2. 下一步,將 [共用] 欄位拖曳至 [報表畫布] 。Next, drag the shares field and drop it in the Report canvas. 現在將 [小時] 欄位拖曳到 [欄位清單] 下的 [軸] 區段。Now drag the Hour field into the Axis section under the Field List.

  3. 我們可以在 [視覺效果] 窗格按一下不同的圖示,輕鬆變更視覺效果的類型。We can easily change the type of visualization by clicking on a different icon in the Visualization pane. 下圖的箭號指向橫條圖圖示。The arrow in the image below points to the Bar Chart icon.

  4. 請將視覺效果類型變更為 [橫條圖] 。Change the visualization type to Bar Chart.
  5. [橫條圖] 隨即建立,但軸卻不是我們所要的 - 我們想要從另一方向 (從高到低) 進行排序。The Bar Chart is created, but the axis isn't what we want - we want it sorted in the other direction (from high to low). 選取 [Y 軸] 旁邊的向下箭頭,以展開該區段。Select the down arrow next to Y-Axis to expand that section. 我們要將軸的類型從 [連續] 變更為 [類別] ,因此它將依我們所需方式排序 (下圖顯示我們選擇之前的軸 - 請看後續的圖,了解我們想要的樣子)。We need to change the type of axis from Continuous to Categorical, so it'll sort how we want it (the image below shows the axis before we make the selection - check out the subsequent image for how we want it to look).

這樣看起來好多了。That's better. 現在我們在此頁面上有三個視覺效果,我們可以視需要設定大小以填滿報表頁面。And now we have three visualizations on this page, which we can size as we want to fill up the report page.

如您所見,自訂報表中的視覺效果很容易,所以可以依您想要的方式呈現資料。As you can see, it's easy to customize visualizations in your report, so you can present the data in the way that you want. 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 使用者共用該儀表板。Once your report is ready, you can upload it to Power BI and create dashboards based on it, which you can share with other Power BI users.

您可以在這裡下載本教學課程的最終結果You can download the end result of this tutorial here

還可以從何處取得更多資訊?Where else can I get more information?