Sub topic 2
Creating Your First Measure
This chapter walks through creating a measure in Power BI Desktop, from the formula bar to a card visual. It assumes you can navigate Report view, the Fields pane, and visuals. It also assumes your model has at least one loaded table, such as a Sales table with a numeric SalesAmount column. You should know what tables, columns, and rows are.
What you are creating
A measure is a named DAX formula that Power BI evaluates when a visual needs a value. It does not store a result for each row. It is recalculated for every cell, bar, or card, using whatever filters apply at that point.
A calculated column is different. It is computed once for each row when the data refreshes, and the results are stored in the model. Use a column when you need a per-row value to slice or filter by. Use a measure when you need a total, count, or ratio that changes with the report.
| Feature | Calculated column | Measure |
|---|---|---|
| Evaluated | At data refresh, once per row | At query time, once per visual cell |
| Stored in the model | Yes, uses memory | No, only the formula is stored |
| Can be used on an axis or slicer | Yes | No |
| Typical use | Categories, row-level labels | Totals, counts, ratios |
This chapter builds one measure and walks through how to create it. Later chapters cover SUM, COUNTROWS, and DIVIDE in detail.
Where to create a measure
You can create a measure in Report view, Table view, or Model view. The steps are the same in each. The measure belongs to one table, which acts as its home. The table does not change the result of the formula, but it decides where you find the measure in the Fields pane.
- Open your report in Power BI Desktop.
- In the Fields pane (called the Data pane in newer versions), select the table that should hold the measure. For this tutorial, select
Sales. - On the ribbon, choose Home and then New measure. You can also right-click the table name and choose New measure.
- The formula bar opens above the report canvas, with a placeholder such as
Measure =. - Replace the placeholder with your formula and press Enter to commit it.
The measure appears in the Fields pane with a calculator icon. A column has a different icon, so you can tell them apart at a glance.
Anatomy of the formula
Your first measure totals the sales amount:
Total Sales = SUM(Sales[SalesAmount])Each part has a job.
Total Salesis the measure name. It is what people see in the Fields pane and in visuals.=separates the name from the expression.SUMis a function that adds up the values in one column.Sales[SalesAmount]is a column reference. The table name comes first, and the column name goes in square brackets.
This measure adds up every value in the SalesAmount column that is visible in the current filter context. Filter context is the set of filters that apply to a calculation at the moment it runs. Slicers, visual axes, and report filters all contribute to it. The next chapter covers it in more detail. For now, remember that the same formula can return different numbers in different places.
Syntax rules to remember
- A column reference always includes the table name, such as
Sales[SalesAmount]. A reference to a measure uses only square brackets, such as[Total Sales]. - If a table name contains spaces or special characters, wrap it in single quotes:
'Sales Orders'[SalesAmount]. - Function names are not case sensitive, but writing them in uppercase is the common convention.
- Names are not case sensitive either, but spelling and spaces must match the model exactly.
Test the measure in a visual
A measure shows its value only when a visual asks for it. Follow these steps to see it work:
- In Report view, add a Card visual from the Visualizations pane.
- Drag
Total Salesfrom the Fields pane onto the card's field well. - The card shows the total of
SalesAmountacross the whole table. - Add a Table or Clustered column chart visual. Put a column such as
Sales[Region]or a date field on the rows or axis, and addTotal Salesas the value.
The same measure now shows a separate value for each region or date. You did not write a different formula for each one. Power BI ran the formula once for each group, each time with a different filter applied.
Seeing it with sample data
Suppose Sales has these rows.
| OrderID | Region | SalesAmount |
|---|---|---|
| 1001 | East | 400 |
| 1002 | East | 1200 |
| 1003 | West | 700 |
| 1004 | West | 300 |
A card with Total Sales shows 2600. A table with Region on the rows shows 1600 for East and 1000 for West, and a total row of 2600. If you add a slicer and select only West, the card changes to 1000, because the slicer filters the Sales table before the formula runs.
Writing a second measure
Add a second measure the same way. Select the Sales table, choose New measure, and enter:
Order Lines = COUNTROWS(Sales)This measure counts the rows of the Sales table in the current filter context, so it counts order lines if each row is one line. In the sample data it returns 4 for the whole table, and 2 for each region.
Now that two measures exist, a third can use both:
Average Sales per Order Line = DIVIDE([Total Sales], [Order Lines])DIVIDE divides the first measure by the second and returns BLANK instead of an error when the denominator is zero or blank. For the sample data, the whole table gives 2600 divided by 4, which is 650. East gives 1600 divided by 2, which is 800.
These three measures form the working set for the rest of the tutorial. This chapter focuses on the creation process, so you do not need to understand every function yet.
Editing and managing measures
To change a measure, select it in the Fields pane. Its formula appears in the formula bar. Edit the text and press Enter. Every visual that uses the measure updates.
To rename a measure, double-click its name in the Fields pane, or select it and press F2. Power BI updates references in other measures and visuals when you rename through the interface.
To delete a measure, right-click it and choose Delete from model. Visuals that used it will show an error until you replace the field.
Useful formula bar behaviors:
- Press Shift+Enter to start a new line, so a long formula is easier to read.
- Press Alt+Enter in some versions for the same result, if Shift+Enter does not work.
- Start typing a function or table name, and IntelliSense lists matching items. Press Tab to accept one.
- Type
//at the start of a line to add a comment.
Total Sales =
// Sum of the SalesAmount column in the current filter context
SUM(Sales[SalesAmount])Common errors when creating a measure
| Symptom | Likely cause | Fix |
|---|---|---|
| "Cannot find name" or "column cannot be found" | A typo, or a missing table prefix | Check spelling and write `Table[Column]` |
| The measure returns the same number in every row of a table | The visual's grouping column comes from a table with no relationship to `Sales` | Check relationships in Model view |
| "A single value for column cannot be determined" | A column was used where a single value was expected, such as in a measure that returns a column reference | Wrap the column in an aggregation such as `SUM` |
| Result shows a sum on a text column | You dragged a column into the visual instead of a measure | Use the measure from the Fields pane |
| `SUM` rejects the column | The column has a text data type | Change the data type in Table view, or fix the source |
Check the data type before you write SUM. SUM works on numeric columns. If SalesAmount was imported as text, change its type to Decimal Number or Fixed Decimal Number in Power Query or in the column tools.
Checking your result
After you create a measure, check it against a known value:
- Put the measure in a card with no filters.
- Compare it to a simple calculation outside Power BI, such as a sum in Excel on the same source data.
- Add a slicer, select one value, and confirm the card changes as expected.
This habit catches wrong column choices and relationship problems early, before the measure is used in many places.
Exercise
You select the Sales table, click New measure, and enter Total Sales = SUM(Sales[SalesAmount]). You then drag Total Sales into a table visual with Sales[Region] on the rows. The table shows 1600 for East and 1000 for West. Why does one formula give two different values, and why is it not stored per row like a calculated column?
Show answer
Power BI evaluates a measure at query time, once for each cell of the visual. For the East row, the visual applies the filter Region = East, so SUM adds only East rows (400 + 1200 = 1600). For West it applies Region = West (700 + 300 = 1000). A calculated column is computed at refresh and stores one value per row, which would not change with the visual's filters. A measure stores only the formula.
Exercise
Write the formula for a measure named Order Lines that counts the rows of the Sales table. What does it return for the West row in the sample data, and which table should you select before you create it?
Show answer
Order Lines = COUNTROWS(Sales). It returns 2 for West, because two Sales rows (orders 1003 and 1004) pass the Region = West filter. Select the Sales table first, so the measure is placed in it and is easy to find in the Fields pane.