在 Power BI Desktop 中按列排序Sort by column in Power BI Desktop

Power BI DesktopPower BI 服务中,你可以更改视觉对象的外观,方法是按不同的数据字段对其进行排序。In Power BI Desktop and the Power BI service, you can change how a visual looks by sorting it by different data fields. 通过更改视觉对象的排序方式,可以突出显示想要表达的信息,并确保视觉对象反映想要传达的任何趋势(或重点)。By changing how you sort a visual, you can highlight the information you want to convey, and ensure the visual reflects whatever trend (or emphasis) you want to convey.

无论使用的是数值数据(如销售图表)还是文本数据(如州名),都可按所需的方式对可视化进行排序,并将其设置为所需的外观。Whether you're using numeric data (such as sales figures) or text data (such as state names), you can sort your visualizations however you want, and make them look like you want them to. Power BI 为你提供排序和快速菜单方面的灵活性。Power BI provides lots of flexibility for sorting, and quick menus for you to use. 在任何视觉对象上,依次选择省略号菜单 (...)、“排序依据”以及排序所依据的字段,如下图中所示。On any visual, select the ellipses menu (...) and then Sort By, then select the field by which you want to sort, as shown in the following image.

更多深入分析的示例More depth, and an example

让我们举个具有更多深入分析的例子,并了解该示例在 Power BI Desktop 中的工作方式。Let's take an example that has more depth, and see how it works in Power BI Desktop.

下述可视化内容列出了在天气方面排名靠前的 15 个州(最多晴天,从 1 到 50 排名,其中 1 表示晴天最多)。The following visualization list the top 15 states in terms of weather (most sunny days, ranked from 1 to 50, with 1 having the most sunny days). 下面是可视化内容在未进行任何排序之前的显示效果。Here's the visualization as it looks before we do any sorting.

视觉对象当前按生活成本进行排序 - 可将降序条的颜色与图例相匹配来进行分辨,但还有种更好的方式来确定当前排序列:使用位于视觉对象右上角处省略号菜单 (...) 的排序依据对话框。The visual is currently sorted by Cost of living - we can tell that by matching the color of the descending bars to the legend, but there's a better way to determine the current sort column: the Sort by dialog, available from the ellipses menu (...) in the upper right corner of the visual. 选择省略号时,将显示以下内容:When we select the ellipses, we see the following:

在选择省略号时出现的菜单中,需要注意以下几项:There are a couple items to notice in the menu that appears when you select the ellipses:

  • 生活成本旁边的黄色栏,以及生活成本按粗体显示这一事实The yellow bar next to Cost of living, and the fact that Cost of living is in bold
  • 排序依据字词旁的小图标,它显示了 Z/A(从 Z 到 A 降序)和向下箭头。The little icon beside the words Sort By, which shows Z/A (Z above A) and a down arrow.

我们将在以下两部分中分别探索其中每一项。We'll look at each of those independently in the next two sections.

选择要用于排序的列Selecting which column to use for sorting

你注意到排序依据菜单中的生活成本旁边有个黄色栏,它表示可视内容正在使用生活成本列来对自身进行排序。You noticed the yellow bar beside Cost of living in the Sort By menu, which indicated that the visual was using the Cost of living column to sort the visual. 按其他列进行排序非常简单 - 只需选择省略号以显示排序依据菜单,然后选择其他列。Sorting by another column is easy - just select the ellipses to show the Sort by menu, then select another column. 就是这么简单。It's that easy.

在下图中,我们选择了社区幸福感作为要依此进行排序的列。In the following image, we selected Community well-being as the column by which we want to sort. 此列恰好是视觉对象中的一行,而不是其中的某栏。That column happens to be one of the lines on the visual, rather than one of the bars. 下面就是选择社区幸福感之后的外观效果。Here's what it looks like after we select Community well-being.

请注意视觉对象的更改方式。Notice how the visual has changed. 现在,对于此视觉对象中所含的这些州,值从“社区幸福感”最高值(本例中是 RI,即罗得岛州)到最低值(本例中是 AZ,即亚利桑那州)进行排序。The values now are ordered from highest "Community well-being" value (in this case RI for Rhode Island) for those states included in this visual, down to AZ (for Arizona) which has the lowest value. 请记住,整张图表仍然仅包括具有最多晴天的 15 个州 - 我们只是按视觉对象中的其他列进行排序。Remember that the overall chart still only includes the 15 states with the most sunny days - we've just ordered them based on another column included in the visual.

但如果想要按升序(而非降序)进行排序,该怎么办呢?But what if we want to sort ascending, instead of descending? 下一部分展示了此操作是多么的简单。The next section shows just how easy that is.

选择排序顺序 - 最小到最大,最大到最小Selecting the sort order - smallest to largest, largest to smallest

如果更深入地查看上图中的排序依据菜单,可以注意到排序依据旁边的图标显示了 Z/A(从 Z 到 A 降序)。When we take a closer look at the Sort By menu from the previous image, we see that the icon next to Sort By shows Z/A (Z above A). 来看一下:Take a look:

显示 Z/A 时,表示视觉对象正在依据所选列按最大值到最小值的顺序进行排序。When Z/A is displayed, it means the visual is being sorted by the selected column in order of greatest value to smallest value. 想要进行更改?Want to change that? 毫无问题 - 只需点击或单击 Z/A 图标,它就会将排序顺序更改为 A/Z 并按从最大值到最大值对视觉对象进行排序(基于所选列)。No problem - just tap or click that Z/A icon, and it changes the sort order to A/Z and sorts the visual (based on the selected column) from smallest to greatest value.

还是相同的视觉对象,这次可点击排序依据菜单上的 Z/A 图标来更改其排序顺序。Here's our same visual, this time after tapping the Z/A icon on the Sort By menu to change its ordering. 请注意,现在首先列出的州是 AZ(亚利桑那州),而最后列出的是 RI(罗得岛州) - 与之前的完全相反。Notice that AZ (Arizona) is now the first state listed, and RI (Rhode Island) is the last - the opposite sorting from before.

可按视觉对象中的任意列进行排序 - 可轻松地将“天气”选为要按其进行排序的列,然后选择排序依据中的 Z/A,以首先显示具有最多晴天的州(最高值 - 天气等同于此数据模型中的晴天天数);同时仍可保留视觉对象中的其他列(无论它们按何种方式应用到此州)。You can sort by any column included in the visual - we could easily select Weather as the column by which we want to sort, and select Z/A from the Sort By menu, to show the states with the most sun first (highest value - Weather equates to days of sunshine in this data model), and still retain the other columns in the visual however they happen to apply to that state. 下面来看看具有这些设置的视觉对象。Here's a look at the visual with those settings.

使用“排序依据列”按钮进行排序Sort using the Sort by Column button

对数据进行排序的另一个方法是通过使用“建模”功能区中的“排序依据列”按钮。There's another way to sort your data, and that's by using the Sort by Column button in the Modeling ribbon.

此排序方法要求你从“字段”窗格中选择一个列,然后选择“排序依据列”按钮来选择(通过哪一列)对视觉对象进行排序的方式。This approach to sorting requires that you select a column from the Fields pane, and then select the Sort by Column button to choose how (by which column) you want to sort your visual. 必须选择要从“字段”窗格中进行排序的列(字段)才能启用“排序依据列”按钮,否则此按钮处于非活动状态。You have to select the column (field) you want to sort from the Fields pane in order to enable the Sort by Column button - otherwise the button is inactive.

让我们看一下常见的示例:你拥有一周中每一天的数据,并且想要基于时间顺序进行排序。Let's look at a common example: you have data from each day of the week, and you want to sort it based on chronological order. 以下步骤演示了操作过程。The following steps show you how.

  1. 首先,请注意,如果选择了视觉对象但没有选择“字段”窗格中的列,“排序依据列”按钮将处于非活动状态(灰色)。First, notice that when the visual is selected but no column is selected in the Fields pane, the Sort by Column button is inactive (grayed out).

  2. 在选择所需的排序依据列时,“字段”窗格中的“排序依据列”按钮将变为活动状态。When we select the column by which we want to sort, in the Fields pane, the Sort by Column button becomes active.

  3. 在选择视觉对象后,现在可以选择“每周的某一日”,而不是默认值(星期几),并且视觉对象现在会按照所需顺序进行排序:依据每周的某一日。Now, with the visual selected, we can select Day of Week, instead of the default (Name of Day), and the visual now sorts in the order we want: by the day of the week.

就是这么简单。And that's it. 请记住,必须选择“字段”窗格中的列,以使“排序依据列”按钮变为活动状态。Remember that you must select a column in the Fields pane for the Sort by Column button to become active.

返回默认列进行排序Getting back to default column for sorting

可按所需的任意列进行排序,但有时你可能也会想要将视觉对象返回到其默认的排序列。You can sort by any column you'd like, but there may be times when you want the visual to return to its default sorting column. 没问题。No problem. 对于已选中排序列的视觉对象(正如所示内容,在排序依据菜单中,所选排序列的旁边具有一个黄色栏),只需打开排序依据菜单并再次选择此列,可视化内容即可返回到其默认的排序列。For a visual that has a sort column selected (a selected sort column has a yellow bar beside it in the Sort By menu, as we learned), simply open the Sort By menu and select that column again, and the visualization returns to its default sort column.

例如,下面是先前的图表:For example, here's our previous chart:

返回菜单并再次选择“天气”后,视觉对象默认为按“州代码”字母顺序进行排序,如下图所示。When we go back to the menu and select Weather again, the visual defaults to being ordered alphabetically by State Code, as shown in the following image.

有如此多可用于排序视觉对象的选项,创建所需图表或图像轻而易举。With so many options for sorting your visuals, creating just the chart or image you want is easy.