Create Your Own Measures in Power BI Desktop
Create Your Own Measures in Power BI Desktop
Create Your Own Measures in Power BI Desktop
Power BI Desktop
• Article
• 09/10/2021
• 11 minutes to read
• 4 contributors
By using measures, you can create some of the most powerful data analysis solutions in
Power BI Desktop. Measures help you by performing calculations on your data as you
interact with your reports. This tutorial will guide you through understanding measures
and creating your own basic measures in Power BI Desktop.
Prerequisites
• This tutorial is intended for Power BI users already familiar with using Power
BI Desktop to create more advanced models. You should already be familiar
with using Get Data and Power Query Editor to import data, work with
multiple related tables, and add fields to the report canvas. If you’re new to
Power BI Desktop, be sure to check out Getting Started with Power BI
Desktop.
• This tutorial uses the Contoso Sales Sample for Power BI Desktop file, which
includes online sales data from the fictitious company, Contoso. Because
this data is imported from a database, you can't connect to the datasource
or view it in Power Query Editor. Download and extract the file on your
computer.
Automatic measures
When Power BI Desktop creates a measure, it's most often created for you
automatically. To see how Power BI Desktop creates a measure, follow these steps:
1. In Power BI Desktop, select File > Open, browse to the Contoso Sales
Sample for Power BI Desktop.pbix file, and then select Open.
2. In the Fields pane, expand the Sales table. Then, either select the check box
next to the SalesAmount field or drag SalesAmount onto the report
canvas.
A new column chart visualization appears, showing the sum total of all
values in the SalesAmount column of the Sales table.
Any field (column) in the Fields pane with a sigma icon is numeric, and its values can
be aggregated. Rather than display a table with many values (two million rows
for SalesAmount), Power BI Desktop automatically creates and calculates a measure to
aggregate the data if it detects a numeric datatype. Sum is the default aggregation for a
numeric datatype, but you can easily apply different aggregations like average or count.
Understanding aggregations is fundamental to understanding measures, because every
measure performs some type of aggregation.
Values calculated from measures change in response to your interactions with your
report. For example, if you drag the RegionCountryName field from
the Geography table onto your existing SalesAmount chart, it changes to show the
average sales amounts for each country.
When the result of a measure changes because of an interaction with your report,
you've affected your measure’s context. Every time you interact with your report
visualizations, you're changing the context in which a measure calculates and displays its
results.
DAX formulas use many of the same functions, operators, and syntax as Excel formulas.
However, DAX functions are designed to work with relational data and perform more
dynamic calculations as you interact with your reports. There are over 200 DAX functions
that do everything from simple aggregations like sum and average to more complex
statistical and filtering functions. There are many resources to help you learn more
about DAX. After you've finished this tutorial, see DAX basics in Power BI Desktop.
When you create your own measure, it's called a model measure, and it's added to
the Fields list for the table you select. Some advantages of model measures are that you
can name them whatever you want, making them more identifiable; you can use them
as arguments in other DAX expressions; and you can make them perform complex
calculations quickly.
Quick measures
Many common calculations are available as quick measures, which write the DAX
formulas for you based on your inputs in a window. These quick, powerful calculations
are also great for learning DAX or seeding your own customized measures.
• From a table in the Fields pane, right-click or select More options (...), and
then select New quick measure from the list.
• Under Calculations in the Home tab of the Power BI Desktop ribbon,
select New Quick Measure.
For more information about creating and using quick measures, see Use quick
measures.
Create a measure
Suppose you want to analyze your net sales by subtracting discounts and returns from
total sales amounts. For the context that exists in your visualization, you need a measure
that subtracts the sum of DiscountAmount and ReturnAmount from the sum of
SalesAmount. There's no field for Net Sales in the Fields list, but you have the building
blocks to create your own measure to calculate net sales.
1. In the Fields pane, right-click the Sales table, or hover over the table and
select More options (...).
2. From the menu that appears, select New measure.
This action saves your new measure in the Sales table, where it's easy to
find.
Tip
When you create a measure from the ribbon, you can create it in any of
your tables, but it's easier to find if you create it where you plan to use it. In
this case, select the Sales table first to make it active, and then select New
measure.
The formula bar appears along the top of the report canvas, where you can
rename your measure and enter a DAX formula.
3. By default, each new measure is named Measure. If you don’t rename it,
additional new measures are named Measure 2, Measure 3, and so on.
Because we want this measure to be more identifiable, highlight Measure in
the formula bar, and then change it to Net Sales.
4. Begin entering your formula. After the equals sign, start to type Sum. As you
type, a drop-down suggestion list appears, showing all the DAX functions,
beginning with the letters you type. Scroll down, if necessary, to
select SUM from the list, and then press Enter.
An opening parenthesis appears, along with a drop-down suggestion list of
the available columns you can pass to the SUM function.
The column name preceded by the table name is called the fully qualified
name of the column. Fully qualified column names make your formulas
easier to read.
6. Select Sales[SalesAmount] from the list, and then enter a closing
parenthesis.
Tip
a. After the closing parenthesis for the first expression, type a space, a minus
operator (-), and then another space.
b. Enter another SUM function, and start typing DiscountAmount until you
can choose the Sales[DiscountAmount] column as the argument. Add a
closing parenthesis.
The validated Net Sales measure is now ready to use in the Sales table in
the Fields pane.
9. If you run out of room for entering a formula or want it on separate lines,
select the down arrow on the right side of the formula bar to provide more
space.
The down arrow turns into an up arrow and a large box appears.
10. Separate parts of your formula by pressing Alt + Enter for separate lines, or
pressing Tab to add tab spacing.
1. Select the Net Sales measure from the Sales table, or drag it onto the
report canvas.
2. Select the RegionCountryName field from the Geography table, or drag it
onto the Net Sales chart.
3. To see the difference between net sales and total sales by country, select
the SalesAmount field or drag it onto the chart.
The chart now uses two measures: SalesAmount, which Power BI summed
automatically, and the Net Sales measure, which you manually created.
Each measure was calculated in the context of another
field, RegionCountryName.
Add a slicer to further filter net sales and sales amounts by calendar year:
1. Select a blank area next to the chart. In the Visualizations pane, select
the Table visualization.
Because Year is a numeric field, Power BI Desktop sums up its values. This
summation doesn’t work well as an aggregation; we'll address that in the
next step.
3. In the Values box in the Visualizations pane, select the down arrow next
to Year, and then select Don't summarize from the list. The table now lists
individual years.
4. Select the Slicer icon in the Visualizations pane to convert the table to a
slicer. If the visualization displays a slider instead of a list, select List from
the down arrow in the slider.
5. Select any value in the Year slicer to filter the Net Sales and Sales Amount
by RegionCountryName chart accordingly. The Net
Sales and SalesAmount measures recalculate and display results in the
context of the selected Year field.
Use your measure in another measure
Suppose you want to find out which products have the highest net sales amount per
unit sold. You'll need a measure that divides net sales by the quantity of units sold.
Create a new measure that divides the result of your Net Sales measure by the sum
of Sales[SalesQuantity].
1. In the Fields pane, create a new measure named Net Sales per Unit in
the Sales table.
2. In the formula bar, begin typing Net Sales. The suggestion list shows what
you can add. Select [Net Sales].
3. You can also reference measures by just typing an opening bracket ([). The
suggestion list shows only measures to add to your formula.
4. Enter a space, a divide operator (/), another space, a SUM function, and then
type Quantity. The suggestion list shows all the columns with Quantity in
the name. Select Sales[SalesQuantity], type the closing parenthesis, and
press ENTER or select Commit (checkmark icon) to validate your formula.
5. Select the Net Sales per Unit measure from the Sales table, or drag it onto
a blank area in the report canvas.
The chart shows the net sales amount per unit over all products sold. This
chart isn't very informative; we'll address it in the next step.