Power BI Desktop 中的度量值Measures in Power BI Desktop

使用 Power BI Desktop,只需点几下鼠标,即可创建数据见解。Power BI Desktop helps you create insights into your data with just a few clicks. 但有时候,这些数据并不包含解决某些重要问题所需的所有内容。But sometimes that data just doesn’t include everything you need to answer some of your most important questions. 度量值可以帮助你解决问题。Measures can help you get there.

度量值用于一些最常见的数据分析中;例如,求和、平均值、最小值或最大值、计数,或你自己使用 DAX 公式创建的更高级的计算。Measures are used in some of the most common data analysis; for example, sums, averages, minimum or maximum values, counts, or more advanced calculations you create yourself using a DAX formula. 度量值的计算结果也始终随着你与的报表的交互而改变,以便进行快速和动态的临时数据浏览。The calculated results of measures are always changing in response to your interaction with your reports, allowing for fast and dynamic ad-hoc data exploration. 让我们仔细了解下。Let’s take a closer look.

了解度量值Understanding measures

Power BI Desktop 中,可以在“报表视图”或“数据视图”中创建和使用度量值。In Power BI Desktop, measures are created and used in Report View or Data View. 你自己创建的度量值将显示在带有计算器图标的“字段”列表中。Measures you create yourself appear in the Fields list with a calculator icon. 你可以随心所欲地为你的度量值命名,并将它们添加到新的或现有的可视化效果中,正如其他字段一样。You can name measures whatever you want, and add them to a new or existing visualization just like any other field.


你可能还会对快速度量值感兴趣,它们是可以在对话框中选择的现成度量值。You might also be interested in quick measures, which are ready-made measures you can select from dialog boxes. 既是快速创建度量值的绝佳方法,也是学习 DAX 语法的绝佳方法,因为可以查看快速度量值自动创建的 DAX 公式。They're a good way to quickly create measures, and also a good way to learn DAX syntax, since their automatically created DAX formulas are available to review. 请参阅快速度量值这篇文章。Check out the article: quick measures.

数据分析表达式Data Analysis Expressions

度量值将计算表达式公式的结果。Measures calculate a result from an expression formula. 在创建自己的度量值时,将使用数据分析表达式 (DAX) 公式语言。When you create your own measures, you’ll use the Data Analysis Expressions (DAX) formula language. DAX 包括一个超过 200 个函数、运算符和构造的库,在创建度量值时提供巨大的灵活性,可以计算几乎任何数据分析需求的结果。DAX includes a library of over 200 functions, operators, and constructs, providing immense flexibility in creating measures to calculate results for just about any data analysis need.

DAX 公式与 Excel 公式非常相似。DAX formulas are a lot like Excel formulas. DAX 甚至具有许多相同的函数,例如 DATE、SUM 和 LEFT。DAX even has many of the same functions like DATE, SUM, and LEFT. 但是,DAX 的函数用于处理关系数据,类似于 Power BI Desktop 中的关系数据。But, DAX’s functions are meant to work with relational data like we have in Power BI Desktop.

我们来看一个示例Let’s look at an example

Jan 是 Contoso 的销售经理。Jan is a sales manager at Contoso. 她接到要求,要提供下一个会计年度的经销商销售预测。She’s been asked to provide reseller sales projections over the next fiscal year. 她决定根据去年的销售额做出预测,并加上从未来六个月计划的各种促销结果得出的六个百分点的年增长率。She decides to base her estimates on last year’s sales amounts, with a six percent annual increase resulting from various promotions that are scheduled over the next six months.

为了报表这些估计值,她将上一年的销售数据导入了 Power BI Desktop 中。To report the estimates, she imports last year’s sales data into Power BI Desktop. 在“Reseller Sale”表中,她找到了“SalesAmount”字段。She finds the SalesAmount field in the Reseller Sales table. 由于她导入的数据仅包含上一年的销售额,她将“SalesAmount”字段重新命名为“Last Years Sales”。Because the data she imported only contains sales amounts for last year, she renames the SalesAmount field to Last Years Sales. 然后,她将“Last Years Sales”拖动到报表画布上。She then drag Last Years Sales onto the report canvas. 该字段在图表可视化效果中显示为去年所有经销商销售额总和的单一值。It appears in a chart visualization as single value that is the sum of all reseller sales from last year.

她注意到,即使自己没有指定计算,系统已经自动提供了一种计算。She notices that even though she did not specify a calculation herself, one has been provided automatically. Power BI Desktop 通过对“Last Years Sales”中的值进行求和,创建其自己的度量值。Power BI Desktop created its own measure by summing up all of the values in Last Years Sales.

但是,Jan 需要度量值来计算明年的销售预测,即基于去年的销售额乘以 1.06,以代表预期为 6% 的业务增长。But, Jan needs a measure to calculate sales projections for the coming year, which will be based on last year’s sales multiplied by 1.06 to account for the expected 6 percent increase in business. 对于此计算,她将创建自己的度量值。For this calculation, she’ll create her own measure. 使用新建度量值功能,她可以创建新的度量值,然后输入下面的 DAX 公式:Using the New Measure feature, she creates a new measure, then enters the following DAX formula:

Projected Sales = SUM('Sales'[Last Years Sales])*1.06

接着将她的新“Projected Sales”度量值拖动到图表中。Jan then drags her new Projected Sales measure into the chart.

只需要最小的工作量,Jan 很快就可拥有用于计算预测销售额的度量值。Very quickly and with minimal effort, Jan now has a measure to calculate projected sales. 通过筛选特定的经销商或将其他字段添加到她的报表中,她可以进一步分析她的预测。She can further analyze her projections by filtering on specific resellers or by adding other fields to her report.

了解详细信息Learn more

在此处我们仅向你快速地介绍了度量值,仍有许多内容可帮助你学习如何创建自己的度量值。We’ve only provided you with a quick introduction to measures here, but there’s a lot more to help you learn how to create your own. 请务必参阅教程:在 Power BI Desktop 中创建你自己的度量值,在其中你可以下载示例文件并获取有关如何创建更多度量值的逐步课程。Be sure to see the Tutorial: Create your own measures in Power BI Desktop, where you can download a sample file and get step-by-step lessons on how to create more measures.

若要更深入了解 DAX,请确保查看 Power BI Desktop 中的 DAX 基本概念To dive a little deeper into DAX, be sure to check out DAX basics in Power BI Desktop. 数据分析表达式参考提供了有关每个函数、语法、运算符和命名约定的详细文章。The Data Analysis Expressions Reference provides detailed articles on each of the functions, syntax, operators, and naming conventions. DAX 出现在 Excel 的 Power Pivot 和 SQL Server Analysis Services 中已经有数年时间了,因此还有许多其他有用的资源可供使用。DAX has been around for several years in Power Pivot in Excel and SQL Server Analysis Services, so there are a lot of other great resources available, too. 请务必查看 DAX 资源中心 Wiki,其中有影响力的 BI 社区成员将会分享他们的 DAX 知识。Be sure to check out the DAX Resource Center Wiki, where influential members of the BI community share their knowledge of DAX.