使用 URL 中的查詢字串參數篩選報表Filter a report using query string parameters in the URL

當您在 Power BI 服務中開啟報表時,每頁報表各有其唯一的 URL。When you open a report in Power BI service, each page of the report has its own unique URL. 若要篩選該報表頁面,您可以使用報表畫布上的 [篩選] 窗格。To filter that report page, you could use the Filters pane on the report canvas. 或者您也可以將查詢字串參數新增到 URL,以篩選報表。Or you could add query string parameters to the URL to filter the report. 您可能有想要向同事展示的報表,並想要預先為他們篩選。Perhaps you have a report you'd like to show colleagues and you want to pre-filter it for them. 其中一個執行方式是從報表的預設 URL 著手、將篩選參數新增到 URL,然後用電子郵件將整個 URL 寄送給他們。One way to do this is to start with the default URL for the report, add the filter parameters to the URL, and then email them the entire URL.

用於篩選的查詢字串參數Query string parameter syntax for filtering

語法相當簡單;只要從報表 URL 著手、新增問號,然後新增您的篩選語法即可。The syntax is fairly straightforward; start with the report URL, add a question mark, and then add your filter syntax.

URL?filter=資料表/欄位 eq ''URL?filter=Table/Field eq 'value'

  • 資料表欄位名稱區分大小寫,則無。Table and Field names are case sensitive, value is not.
  • 從報表檢視中隱藏的欄位仍可篩選。Fields that are hidden from report view can still be filtered.
  • 的前後必須加上單引號。Value has to be enclosed with single quotes.
  • 欄位類型必須是數字或字串Field type has to be a number or string
  • 資料表和欄位名稱不能有任何空格。Table and field names cannot have any spaces.

如果仍感到困惑,請繼續閱讀,我們會詳加解說。If it's still confusing, continue reading and we'll break it down.

篩選欄位Filter on a field

假設報表的 URL 如下所示。Let’s assume that the URL to our report is the following.

而我們可以從地圖視覺效果 (上方) 看到,我們有門市位於北卡羅萊納州。And we see in our map visualization (above) that we have stores in North Carolina.

注意

本範例以零售分析範例為依據。This example is based on the Retail Analysis sample.

若要篩選報表,使其只顯示 "NC" (北卡羅萊納州) 門市的資料,請將下列內容加到 URL 後;To filter the report to show data only for stores in "NC" (North Carolina), append the URL with the following;

?filter=Store/Territory eq 'NC'?filter=Store/Territory eq 'NC'

注意

NC 是儲存在 [Store] 資料表 [Territory] 欄位中的值。NC is a value stored in the Territory field of the Store table.

我們的報表已篩選出北卡羅萊納州;報表頁面上的所有視覺效果都只會顯示北卡羅萊納州的資料。Our report is filtered for North Carolina; all the visualizations on the report page show data for only North Carolina.

篩選多個欄位Filter on multiple fields

您也可以將額外參數新增至 URL,以篩選多個欄位。You can also filter on multiple fields by adding additional parameters to your URL. 讓我們回到原始的篩選參數。Let's go back to our original filter parameter.

?filter=Store/Territory eq 'NC'

若要篩選其他欄位,請新增 and,然後以上述相同格式新增另一個欄位。To filter on additional fields, add an and and another field in the same format as above. 範例如下。Here is an example.

?filter=Store/Territory eq 'NC' and Store/Chain eq 'Fashions Direct'

使用 DAX 篩選多個值Using DAX to filter on multiple values

篩選多個欄位的另一種方法是建立計算結果欄,將兩個欄位串連成單一值。Another way to filter on multiple fields is by creating a calculated column that concatenates two fields to a single value. 接著您就可以篩選該值。Then you can filter on that value.

例如,我們有兩個欄位:Territory 和 Chain。For example, we have two fields: Territory and Chain. 在 Power BI Desktop 中建立新的計算結果欄 (欄位),名稱為 TerritoryChain。In Power BI Desktop, create a new Calculated column (Field) called TerritoryChain. 請記住,欄位名稱不能有任何空格。Remember that the Field name cannot have any spaces. 以下是該資料行的 DAX 公式。Here is the DAX formula for that column.

TerritoryChain = [Territory] & " - " & [Chain]TerritoryChain = [Territory] & " - " & [Chain]

將報表發佈到 Power BI 服務,然後使用 URL 查詢字串篩選成只顯示 NC 的 Lindseys 門市資料。Publish the report to Power BI service and then use the URL query string to filter to display data for only Lindseys stores in NC.

https://app.powerbi.com/groups/me/reports/8d6e300b-696f-498e-b611-41ae03366851/ReportSection3?filter=Store/TerritoryChain eq 'NC–Lindseys'https://app.powerbi.com/groups/me/reports/8d6e300b-696f-498e-b611-41ae03366851/ReportSection3?filter=Store/TerritoryChain eq 'NC–Lindseys'

從篩選的報表釘選磚Pin a tile from a filtered report

在您使用查詢字串參數篩選報表後,可以將視覺效果從該報表釘選到儀表板。Once you've filtered the report using query string parameters, you can pin visualizations from that report to your dashboard. 儀表板上的磚會顯示經過篩選的資料,而選取該儀表板磚會開啟用來建立該磚的報表。The tile on the dashboard will display the filtered data and selecting that dashboard tile will open the report that was used to create it. 不過,您使用 URL 進行的篩選不會儲存在報表,而選取儀表板磚時,報表會以未篩選的狀態開啟。However, the filtering you did using the URL is not saved with the report and when the dashboard tile is selected, the report opens in its unfiltered state. 這表示儀表板磚中顯示的資料與報表視覺效果中顯示的資料不相符。This means that the data displayed in the dashboard tile will not match the data displayed in the report visualization.

在某些情況下,這在您想要查看不同結果時會很實用;在儀表板上已篩選,在報表則未篩選。There may be some cases where this will be helpful when you'd like to see different results; filtered on the dashboard and unfiltered in the report.

限制與疑難排解Limitations and troubleshooting

使用查詢字串參數時,有幾件點事項要注意。There are a couple of things to be aware of when using the query string parameters.

  • 查詢字串篩選不會處理發行至網路或 Power BI Embedded。Query string filtering does not work with Publish to web or Power BI Embedded.
  • 欄位類型必須是數字或字串。Field type has to be number or string.
  • 資料表和欄位名稱不能有任何空格。Table and field names cannot have any spaces.

後續步驟Next steps

將視覺效果釘選至儀表板Pin a visualization to a dashboard
請試用 - 完全免費!Try it out -- it's free!

有其他問題嗎?More questions? 嘗試在 Power BI 社群提問Try asking the Power BI Community