使用快速度量执行常见计算Use quick measures for common calculations

可使用快速度量快速、轻松地执行常见的高效计算。You can use quick measures to quickly and easily perform common, powerful calculations. 快速度量在后台运行一组数据分析表达式 (DAX) 命令,然后显示结果以供用户在报表中使用。A quick measure runs a set of Data Analysis Expressions (DAX) commands behind the scenes, then presents the results for you to use in your report. 无需编写 DAX,系统会根据对话框中提供的输入自动完成此操作。You don't have to write the DAX, it's done for you based on input you provide in a dialog box. 计算分为许多类别,可通过多种方式来根据自己的需求修改所有计算。There are many available categories of calculations and ways to modify each calculation to fit your needs. 也许最重要的是,可以查看快速度量执行的 DAX,从而开始学习或拓展你自己的 DAX 知识。Perhaps best of all, you can see the DAX that's executed by the quick measure and jump-start or expand your own DAX knowledge.

创建快速度量Create a quick measure

若要在 Power BI Desktop 中创建快速度量,右键单击或选择“字段”窗格中任意项旁边的省略号 ...,然后从随即显示的菜单中选择“新建快速度量” 。To create a quick measure in Power BI Desktop, right-click or select the ellipsis ... next to any item in the Fields pane, and select New quick measure from the menu that appears.

选择新建快速度量

你还可以在现有视觉对象的“值”框中右键单击或选择任何值旁边的下拉箭头,然后选择菜单中的“新建快速度量” 。You can also right-click or select the drop-down arrow next to any value in the Values well for an existing visual, and select New quick measure from the menu.

选择“新建快速度量”后将显示“快速度量”窗口,随即可以选择所需计算,以及要对其运行计算的字段 。When you select New quick measure, the Quick measures window appears, letting you select the calculation you want and the fields to run the calculation against.

选择“选择计算”字段,查看一长串的可用快速度量。Select the Select a calculation field to see a long list of available quick measures.

可用的快速度量计算

五种快速度量的计算类型及其计算如下所示:The five quick measure calculation types, with their calculations, are:

  • 每个类别的聚合Aggregate per category
    • 每个类别的平均值Average per category
    • 每个类别的差异Variance per category
    • 每个类别的最大值Max per category
    • 每个类别的最小值Min per category
    • 每个类别的加权平均Weighted average per category
  • 筛选器Filters
    • 已筛选的值Filtered value
    • 与已筛选值的差异Difference from filtered value
    • 与已筛选值的百分比差异Percentage difference from filtered value
    • 新客户的销售额Sales from new customers
  • 时间智能Time intelligence
    • 本年迄今总计Year-to-date total
    • 本季度至今总计Quarter-to-date total
    • 本月至今总计Month-to-date total
    • 年增率变化Year-over-year change
    • 季度增率变化Quarter-over-quarter change
    • 月增率变化Month-over-month change
    • 移动平均Rolling average
  • 总数Totals
    • 汇总Running total
    • 类别总数(应用筛选器)Total for category (filters applied)
    • 类别总数(未应用筛选器)Total for category (filters not applied)
  • 数学运算Mathematical operations
    • 相加Addition
    • 减法Subtraction
    • 乘法Multiplication
    • 除法Division
    • 百分比差异Percentage difference
    • 相关系数Correlation coefficient
  • 文本Text
    • 星级评分Star rating
    • 值连接列表Concatenated list of values

若要针对想查看的新快速度量、底层 DAX 公式或其他快速度量相关内容提交建议,请参阅本文末尾部分。To submit your ideas about new quick measures you'd like to see, underlying DAX formulas, or other quick measures ideas for consideration, see the end of this article.

备注

使用 SQL Server Analysis Services (SSAS) 实时连接时,可以使用一些快速度量。When using SQL Server Analysis Services (SSAS) live connections, some quick measures are available. Power BI Desktop 仅显示连接到的 SSAS 版本所支持的快速度量。Power BI Desktop displays only the quick measures that are supported for the version of SSAS you're connecting to. 如果连接到 SSAS 实时数据源,但列表中没有显示特定的快速度量,这是因为连接到的 SSAS 版本不支持用于实现这些快速度量的 DAX 度量。If you're connected to a SSAS live data source and don't see certain quick measures in the list, it's because the SSAS version you're connected to doesn't support the DAX commands used to implement those quick measures.

选择要用于快速度量的计算和字段后,选择“确定”。After you select the calculations and fields you want for your quick measure, select OK. 新建的快速度量将显示在“字段”窗格中,而基础 DAX 公式显示在“公式”栏中。The new quick measure appears in the Fields pane, and the underlying DAX formula appears in the formula bar.

快速度量示例Quick measure example

接下来在实践中了解快速度量。Let's take a look at a quick measure in action.

以下矩阵视觉对象显示了一张各种产品的销售额表。The following matrix visual shows a sales table for various products. 这是包含了每个类别的销售总额的基本表。It's a basic table that includes the sales totals for each category.

显示销售额表的矩阵视觉对象

选择矩阵视觉对象后,在“值”框中选择 TotalSales 旁边的下拉箭头,然后选择“新建快速度量” 。With the matrix visual selected, select the drop-down arrow next to TotalSales in the Values well, and select New quick measure.

在“快速度量”窗口的“计算”中,选择“Average per category” 。In the Quick measures window, under Calculation, select Average per category.

将“Average Unit Price”从“字段”窗格拖到“基值”字段 。Drag Average Unit Price from the Fields pane into the Base value field. 将“类别”字段保留为 Category,然后选择“确定” 。Leave Category in the Category field, and select OK.

Power BI Desktop 的屏幕截图,其中显示了“字段”窗格中的“筛选”选项。

选择“确定”后会发生几个值得关注的事情。When you select OK, several interesting things happen.

在“视觉对象”、“公式栏”和“字段”列表中新建快速度量

  1. 矩阵视觉对象有一个新列,其中显示已计算的“Average Unit Price average per Category”。The matrix visual has a new column that shows the calculated Average Unit Price average per Category.

  2. 新建的快速度量的 DAX 公式显示在公式栏中。The DAX formula for the new quick measure appears in the formula bar. 有关 DAX 公式的详细信息,请参阅下一节See the next section for more about the DAX formula.

  3. 新建的快速度量在“字段”窗格中以选中和高亮显示状态显示。The new quick measure appears selected and highlighted in the Fields pane.

新建的快速度量值可用于报表中的任何视觉对象,而不只是您为其创建的视觉对象。The new quick measure is available to any visual in the report, not just the visual you created it for. 下图显示了使用“新建的快速度量”字段创建的快速柱形图视觉对象。The following image shows a quick column chart visual created by using the new quick measure field.

基于“快速度量”字段的新条形图视觉对象

使用快速度量了解 DAXLearn DAX by using quick measures

快速度量的一个强大优点在于显示了实现度量值的 DAX 公式。A great advantage of quick measures is that they show you the DAX formula that implements the measure. 选择“字段”窗格中的快速度量后将显示公式栏,其中显示了 Power BI 为实现此度量值而创建的 DAX 公式 。When you select a quick measure in the Fields pane, the Formula bar appears, showing the DAX formula that Power BI created to implement the measure.

公式栏中的快速度量公式

公式栏不仅显示度量值背后的公式,而且更重要的可能是,使你可以了解如何创建 DAX 公式基础快速度量。The formula bar not only shows you the formula behind the measure, but perhaps more importantly, lets you see how to create the DAX formulas underlying quick measures.

假设需要执行年增率计算,但你不确定该如何编写 DAX 公式,或者说,一点头绪都没有。Imagine you need to do a year-over-year calculation, but you're not sure how to structure the DAX formula, or you have no idea where to start. 无需坐在桌前冥思苦想,可以使用“年增率变化”计算创建快速度量,然后看它在视觉对象中如何显示以及 DAX 公式如何运作。Instead of banging your head on the desk, you can create a quick measure using the Year-over-year change calculation, and see how it appears in your visual and how the DAX formula works. 然后,可以直接更改 DAX 公式,也可以创建符合要求和预期的类似度量值。Then you can either make changes directly to the DAX formula, or create a similar measure that meets your needs and expectations. 这就好像只需单击几下,即有老师迅速回答你的“假设”问题一样。It's like having a teacher that immediately responds to what-if questions you ask with a few clicks.

如果不喜欢快速度量,你可以随时将其从模型中删除。You can always delete quick measures from your model if you don't like them. 只需右键单击或选择度量值旁边的“...”,然后选择“删除” 。That's as easy as right-clicking or selecting the ... next to the measure and selecting Delete. 还可以在菜单中选择“重命名”,重命名快速度量。You can also rename a quick measure whatever you like by selecting Rename from the menu.

删除或重命名快速度量

限制和注意事项Limitations and considerations

有几点要牢记的限制和注意事项。There are a few limitations and considerations to keep in mind.

  • 你可以在报表的任何视觉对象中使用添加到“字段”窗格的快速度量。You can use quick measures added to the Fields pane with any visual in the report.
  • 选择“字段”列表中的度量值,然后查看公式栏中的公式,可以随时查看与快速度量相关联的 DAX。You can always see the DAX associated with a quick measure by selecting the measure in the Fields list and looking at the formula in the formula bar.
  • 如果能够修改模型,快速度量才可适用。Quick measures are only available if you can modify the model. 如果使用某些实时连接,则不适用。That isn't the case when you're working with some Live connections. 如前文所述,支持 SSAS 表格实时连接。SSAS tabular live connections are supported, as previously described.
  • 在 DirectQuery 模式下工作时,无法创建时间智能快速度量。You can't create time intelligence quick measures when working in DirectQuery mode. 这些快速度量中使用的 DAX 函数在转换为发送到数据源的 T-SQL 语句时会影响性能。The DAX functions used in these quick measures have performance implications when translated into the T-SQL statements that are sent to your data source.

重要

快速度量的 DAX 语句只将逗号用作参数分隔符。DAX statements for quick measures use only commas for argument separators. 如果 Power BI Desktop 版本使用的是将逗号用作十进制分隔符的语言,快速度量将无法正常运行。If your version of Power BI Desktop is in a language that uses commas as decimal separators, quick measures will not work properly.

时间智能和快速度量Time intelligence and quick measures

可以将自己的自定义日期表与时间智能快速度量配合使用。You can use your own custom date tables with time intelligence quick measures. 如果使用的是外部表格模型,请确保在生成模型时,此表中的主日期列被标记为“日期”表,如指定“标记为日期表”以便与时间智能配合使用所述。If you're using an external tabular model, make sure that when the model was built, the primary date column in the table was marked as a date table, as described in Specify Mark as Date Table for use with time-intelligence. 如果要导入自己的日期表,请确保将其标记为“日期”表,如在 Power BI Desktop 中设置和使用日期表所述。If you're importing your own date table, make sure to mark it as a date table, as described in Set and use date tables in Power BI Desktop.

其他信息和示例Additional information and examples

对尚未提供的快速度量有建议吗?Have an idea for a quick measure that isn't already provided? 很好!Great! 请参阅 Power BI 建议页,并提交你的建议以及想要在 Power BI Desktop 中使用的快速度量的 DAX 公式。Check out the Power BI Ideas page, and submit your ideas and DAX formulas for quick measures you'd like to see in Power BI Desktop. 在未来版本中,我们会考虑将其添加到“快速度量”列表。We'll consider adding them to the quick measures list in a future release.