教程︰使用 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. 其他组中选择 FacebookSelect 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.

类型字段拖放到报表画布中。Drag the type field onto the Report canvas. Power BI Desktop 将在报表画布中创建新的可视化效果。Power BI Desktop creates a new visualization in the Report canvas. 然后,将类型字段(与你刚拖报表画布的字段相同)拖放到区域以创建条形图可视化效果。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

在此步骤中,你将拆分创建_时间列以获取日期和时间值。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. 查询编辑器网格中,滚动到右侧,直到你找到创建_时间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. 将创建的列分别重命名为创建_日期创建_时间Rename the created columns to created_date and created_time respectively.
  4. 选择新列,创建_时间,*并在*查询视图功能区中导航到添加列选项卡,然后在从日期和时间组下选择时间>小时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. 将此新小时列的类型更改为整数,方法是导航到主页选项卡并选择数据类型下拉菜单,或者右键单击列并选择转换>整数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. 继续向右滚动,直到你看到共享列。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.

    加载数据后,将列重命名为共享,方法是双击列名称并右键单击列,或在查询视图功能区中的重命名任何列组下选择转换选项卡。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. 最后,将新共享列更改为整数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 (#"Split Column by Delimiter",{{"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 (#"Changed Type",{{"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 (#"Renamed Columns", "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 (#"Inserted Hour",{{"Hour", type text}})Table.TransformColumnTypes (#"Inserted Hour",{{"Hour", type text}})
展开嵌套表中的值**Expand **values in a nested table** 展开共享Expand shares Table.ExpandRecordColumn (#"Changed Type1", "shares", {"count"}, {"shares.count"})Table.ExpandRecordColumn (#"Changed Type1", "shares", {"count"}, {"shares.count"})
重命名列**Rename **the column** 已重命名列 1Renamed Columns1 Table.RenameColumns (#" Expand shares",{{"shares.count", "shares"}})Table.RenameColumns (#" Expand shares",{{"shares.count", "shares"}})
更改类型Change Type 已更改类型 2Changed Type2 Table.TransformColumnTypes (#"Renamed Columns1",{{"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. 然后将创建的_日期拖放到图表中。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?