在 Power BI Desktop 中建立及使用模擬參數來視覺化變數Create and use a What if parameter to visualize variables in Power BI Desktop

從 2017 年 8 月發行的 Power BI Desktop 開始,您可以為報表建立模擬變數,將變數當作交叉分析篩選器與其互動,進而視覺化及量化報表中的不同索引鍵值。Starting with the August 2017 release of Power BI Desktop, you can create What if variables for your reports, interact with the variable as a slicer, and thereby visualize and quantify different key values in your reports.

您可以在 Power BI Desktop 的 [模型] 索引標籤上找到模擬參數。The What if parameter is found on the Modeling tab in Power BI Desktop. 當您這樣做時,會出現對話方塊,您可以在其中設定參數。When you do so, a dialog appears where you can configure the parameter.

建立模擬參數Creating a What if parameter

若要建立模擬參數,請從 Power BI Desktop 的 [模型] 索引標籤選取 [模擬] 按鈕。To create a What if parameter, select the What if button from the Modeling tab in Power BI Desktop. 在下圖中,我們建立了稱為「折扣百分比」的參數,並將其資料類型設定為 [小數]。In the following image, we've created a parameter called Discount percentage and set its data type to Decimal number. [最小值] 為零,[最大值] 為 0.50 (50%)。The Minimum value is zero, the Maximum is 0.50 (fifty percent). 我們也將 [增量] 設定為 0.05 (5%)。We've also set the Increment to 0.05, or five percent. 這是與報表互動時會調整的參數量。That's how much the parameter will adjust when interacted with in a report.

注意

針對小數,請務必在前面加上零,例如該方塊中的 0.50 (而不是 .50)。For decimal numbers, make sure you precede it with a zero, as in 0.50 versus just .50 in that box. 否則不會驗證數字,而且無法選取 [確定] 按鈕。Otherwise the number won't validate and the OK button will not be selectable.

為了方便起見,[在本頁新增交叉分析篩選器] 核取方塊會將含有模擬參數的交叉分析篩選器自動放到目前的報表頁面上。For your convenience, the Add slicer to this page checkbox automatically puts a slicer with your What if parameter onto the current report page.

建立模擬參數除了建立參數本身之外,也會建立一個量值,可供您用來視覺化模擬參數的目前值。In addition to creating the parameter, creating a What if parameter also creates a measure, which you can use to visualize the current value of the What if parameter.

請注意,一旦您建立模擬參數,參數和量值會成為模型的一部分。It's important and useful to note that once you create a What if parameter, both the parameter and the measure become part of your model. 因此,這些參數和量值可在報表中使用,也可以在其他報表頁面上使用。So they're available throughout the report, and can be used on other report pages. 因為這些參數和量值是模型的一部分,所以您可以從報表頁面刪除交叉分析篩選器;若要將它放回,只要從 [欄位] 清單擷取模擬參數並將它拖曳到畫布上 (然後將視覺效果變更為交叉分析篩選器),即可輕鬆將模擬參數放回您的報表中。And since they're part of the model, you can delete the slicer from the report page, and if you want it back, just grab the What if parameter from the Fields list and drag it onto the canvas (then change the visual to a slicer) to easily get the What if parameter back into your report.

使用模擬參數Using a What if parameter

讓我們建立使用模擬參數的簡單範例。Let's create a simple example of using a What if parameter. 我們在上一節中已建立模擬參數,現在我們將建立新量值來使用此參數,該量值的值是利用滑桿來調整。We created the What if parameter in the previous section, now we'll put it to use by creating a new measure whose value adjusts with the slider. 為了達成此目的,我們將建立一個新量值。To accomplish this, we create a new measure.

此新量值將會是套用折扣率的總銷售金額。The new measure is simply going to be the total sales amount, with the discount rate applied. 當然,您可以建立複雜且相關的量值,讓您報表的取用者視覺化模擬參數的變數。You can create complex and interesting measures, of course, that let the consumers of your reports visualize the variable of your What if parameter. 例如,您可以建立報表,讓銷售人員查看達到特定銷售目標或百分比時的報酬,或查看銷售量增加對更優惠折扣的影響。For example, you could create a report that let sales people see their compensation if they meet certain sales goals or percentages, or see the affect of increased sales to deeper discounts.

在公式列中鍵入量值公式,並將其命名為 Sales after Discount 之後,其結果會如下所示:Once we type the measure formula into the formula bar, and name it Sales after Discount, we see its result:

接著使用軸上的 OrderDate 建立自訂視覺效果,其值為 SalesAmount 和剛建立的量值 Sales after DiscountThen we create a column visual with OrderDate on the on the axis, and both SalesAmount and the just-created measure Sales after Discount as the values.

然後,當我們移動滑桿時,我們會看到 [Sales after Discount] 欄反映折扣後的銷售金額。Then, as we move the slider, we see that the Sales after Discount column reflects the discounted sales amount.

就是這麼簡單。And that's all there is to it. 您可以在各式各樣的情況下使用模擬參數,讓報表的取用者可以與您在報表中建立的不同案例互動。You can use What if parameters in all sorts of situations, to enable the consumers of reports to interact with different scenarios that you create in your reports.