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.