長條圖Histograms

在 Power BI 中建立長條圖的方式有幾種。There are several ways to build histograms in Power BI. 我們先從最簡單的方式開始,再繼續往下進行。We'll start with the simplest and go from there.

簡單的長條圖Simple Histograms

一開始請決定哪個查詢具有您想要建立長條圖的欄位。To get started, determine which query has the field you want to build a histogram on. 使用查詢的 [參考] 選項來建立新的查詢,並命名為 FieldName HistogramUse the Reference option for the query to create a new query and name it FieldName Histogram. 使用 [轉換] 功能區中的 [群組依據] 選項,然後選取 [資料列計數] 彙總。Use the Group by option in the Transform ribbon and select the count rows aggregate. 確定所產生之彙總資料行的資料類型為數值。Ensure the data type is a number for the resulting aggregate column. 然後您可以在報表頁面上以視覺化方式呈現這項資料。Then you can visualize this data on the reports page. 此方法建立過程快速且容易,但如果您有許多資料點且不允許跨多種視覺效果進行筆刷繪製,則不適用。This approach is fast and easy to build, but doesn't work well if you have many data points and does not allow brushing across visuals.

定義值區以建立長條圖Defining buckets to build a histogram

決定哪個查詢具有您想要建立長條圖的欄位。Determine which query has the field you want to build a histogram on. 使用查詢的 [參考] 選項來建立新的查詢,並命名為 FieldNameUse the Reference option for the query to create a new query and name it FieldName. 現在使用規則來定義值區。Now define the buckets with a rule. 使用 [加入資料行] 功能區上的 [加入自訂資料行] 選項,並建立自訂規則。Use the Add Custom Column option on the Add Column ribbon and build a custom rule.

確定所產生之彙總資料行的資料類型為數值。Ensure the data type is a number for the resulting aggregate column. 您現在可以使用本文稍早之<簡單的長條圖> 中所述的群組依據方法來取得長條圖。Now you can use the group by technique described in Simple Histograms (earlier in this article) to achieve the histogram. 這個選項會處理更多資料點,但仍無法解決筆刷繪製的問題。This option handles more data points but still does not help with brushing.

定義支援筆刷繪製的長條圖Defining a histogram that supports brushing

筆刷繪製是指將視覺效果連結在一起,以便使用者選取某個視覺效果中的資料點時,報表頁面上的其他視覺效果會醒目提示或篩選與所選資料點相關的資料點。Brushing is when visuals are linked together so that when a user selects a data point in one visual other visuals on the report page highlight or filter data points related to the selected data point. 由於我們要在查詢期間操作資料,因此需要建立資料表之間的關聯性,並確保知道哪個詳細資料項目與長條圖中的值區相關,反之亦然。Since we're manipulating data at query time, we will need to create a relationship between tables and ensure we know which detail item relates to the bucket in the histogram and vice-versa.

在具有您想要建立長條圖之欄位的查詢上,使用 [參考] 選項開始進行這個程序。Start the process by using the Reference option on the query that has the field you want to build a histogram on. 將新的查詢命名為 BucketsName the new query Buckets. 在本例中,我們將原始查詢稱為 DetailsFor this example let's call the original query Details. 接著移除所有資料行,只留下要做為長條圖值區使用的資料行。Next remove all columns except the column you'll use as the bucket for the histogram. 現在,在查詢中使用 [移除重複項目] 功能,如此留在資料行中的值便是唯一的值;您可以選取資料行,然後在快顯功能表中找到這項功能。Now use the Remove Duplicates feature in query, it's on the right click menu when you select the column, so the remaining values are the unique values in the column. 如果您有十進位數字,您可以先使用定義值區的秘訣來建立長條圖,以取得一組可管理的值區。If you have decimal numbers you can first use the tip for defining buckets to build a histogram to get a manageable set of buckets. 現在,檢查查詢預覽中顯示的資料。Now, check the data shown in the query preview. 如果您看到空白值或 Null,則必須先修正這些問題,再建立關聯性。If you see blank values or null you'll need to fix those up before creating a relationship. 請參閱<資料包含 Null 或空白值時建立關聯性>。See "Creating a relationship if my data has null or blank values". 使用這個方法時,由於需要排序,因此可能會造成問題。Using this approach can be problematic due to the need to sort. 若要讓值區正確排序,請參閱<排列順序:以我想要的順序來顯示類別>。To get the buckets to sort correctly, see "Sorting order: make categories appear in the order I want".

注意

在建立任何視覺效果前先考慮排序次序會很有幫助。It's useful to think about the sort order before building any visuals.

本程序中的下一個步驟是在值區資料行上,定義 BucketsDetails 查詢之間的關聯性。Next step in the process is to define a relationship between the Buckets and Details queries on the buckets column. Power BI Desktop 中,選取功能區中的 [管理關聯性]。In Power BI Desktop, select Manage Relationships in the ribbon. 建立關聯性,其中 Buckets 位於左側資料表,而 Details 位於右側資料表,然後選取用於長條圖的欄位。Create a relationship where Buckets is in the left table and Details in on the right table, and select the field you're using for the histogram.

最後一個步驟是建立長條圖。Last step is to create the histogram. Buckets 資料表拖曳 Bucket 欄位。Drag the Bucket field from the Buckets table. 從產生的直條圖中移除預設欄位。Remove the default field from the resulting column chart. 現在從 Details 資料表將長條圖欄位拖曳到同一個視覺效果上。Now from the Details table drag the histogram field into the same visual. 將欄位區中的預設彙總方式變更為 [計數]。In the field well, change the default aggregate to Count. 結果會產生長條圖。The result is the histogram. 如果您從 Details 資料表建立類似矩形式樹狀結構圖的另一個視覺效果,請選取矩形式樹狀結構圖中的某個資料點以查看長條圖的醒目提示,並顯示與整個資料集趨勢相關的所選資料點長條圖。If you create another visual like a treemap from the Details table, select a data point in treemap to see the histogram highlight and show the histogram for the selected data point relative to the trend for the entire data set.