Power Pivot & DAX course · Lesson 5 of 16

Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT

Create DAX measures in Power Pivot step by step: the Measure dialog, the Calculation Area, SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT, MAX and DIVIDE.

Beginner 18 min read

Time to write real DAX. In this lesson you create a small set of measures that almost every model needs: totals, counts, averages and distinct counts. Along the way you learn the Measure dialog, the Calculation Area, number formatting and the habit of building measures on top of other measures.

We use the three tables from The Data Model and relationships: star schema and filter direction: Sales, Products and Customers, related on ProductID and CustomerID.

ABCDEF
1OrderIDDateCustomerIDProductIDQtyAmount
210012026-01-05C1P121900
310022026-01-12C2P210120
410032026-01-20C3P33567
510042026-02-02C1P45175
610052026-02-09C4P541680
710062026-02-17C2P11950
810072026-03-03C3P220240
910082026-03-10C4P32378
1010092026-03-18C1P51420
1110102026-03-25C2P46210

Sales, the fact table used for every measure in this lesson.

Creating a measure with the Measure dialog

  1. Click any cell in a PivotTable based on the Data Model (or any cell at all).
  2. Choose Power Pivot › Calculations › Measures › New Measure…. Alternatively, in the PivotTable Fields pane right-click the Sales table name and choose Add Measure….
  3. Table name: choose Sales. This is only the home table where the measure is listed; it does not limit what the formula can use.
  4. Measure name: type Revenue.
  5. Description (optional): Sum of order amounts in euros. It appears as a tooltip in the field list.
  6. Formula: after the = that is already there, type SUM(Sales[Amount]). As you type, IntelliSense lists tables, columns and functions; press Tab to accept a suggestion.
  7. Click Check formula. No errors in formula means the syntax is valid.
  8. Under Formatting Options choose Category: Number, Decimal places: 0 and tick Use 1000 separator (,).
  9. Click OK. Revenue appears in the Sales table in the field list with an fx icon. Tick it to add it to Values.

To change a measure later, use Power Pivot › Measures › Manage Measures, select it and click Edit. The same dialog lets you delete measures.

Tip. The formatting you choose in the dialog travels with the measure. Every PivotTable, chart and cube formula that uses Revenue shows it with thousands separators, so you never format the same field twice.

Typing measures in the Calculation Area

In the Power Pivot window, Data View shows a grid of empty cells under each table: the Calculation Area (toggle it with Home › View › Calculation Area). Click any cell there and type the measure as Name := expression in the formula bar. This is quick when you create many measures, and you can see their grand-total values immediately. Format a measure there with the Home › Formatting buttons.

DAX
Revenue := SUM ( Sales[Amount] )
Calculation Area syntax. In the Measure dialog you type only = SUM ( Sales[Amount] ).

The core aggregation functions

DAX
Revenue := SUM ( Sales[Amount] )
Units := SUM ( Sales[Qty] )
Orders := COUNTROWS ( Sales )
Avg Order := AVERAGE ( Sales[Amount] )
Customers := DISTINCTCOUNT ( Sales[CustomerID] )
Largest Order := MAX ( Sales[Amount] )
Avg Unit Price := DIVIDE ( [Revenue], [Units] )
Seven measures, each stored in the Sales table.
  • SUM adds a numeric column. Unlike Excel's SUM, it takes exactly one column, not a list of ranges.
  • COUNTROWS counts the rows of a table, here the number of order lines. It is the clearest way to count transactions. COUNT counts non-blank numbers in a column and COUNTA non-blank values of any type; COUNTROWS does not depend on any particular column being filled in.
  • AVERAGE is the arithmetic mean of a column, blanks ignored.
  • DISTINCTCOUNT counts the different values in a column. A normal PivotTable cannot do this without tricks; in the model it is one function.
  • MAX and MIN return the largest and smallest value.
  • DIVIDE divides two measures and returns a blank instead of an error when the denominator is zero (Variables, DIVIDE and error handling in DAX goes deeper).

The result in a PivotTable

Insert a PivotTable from the Data Model (Insert › PivotTable › From Data Model), put Products[Category] in Rows and tick the measures:

ABCDEF
1CategoryRevenueUnitsOrdersAvg OrderCustomers
2Accessories745414186.253
3Computers4950841,237.503
4Displays94552472.502
5Grand Total66405410664.004

Products[Category] in Rows; five measures in Values. Avg Order formatted with 2 decimals.

Check the Computers row by hand: orders 1001, 1005, 1006 and 1009 are laptops and tablets, totalling 4,950 over 4 orders, so the average order is 4,950 ÷ 4 = 1,237.50. Three different customers bought them (C1 twice, C4 and C2).

Now look at the Grand Total row. Revenue, Units and Orders add up, as you would expect. Avg Order does not equal the average of the three rows: it is 6,640 ÷ 10, recomputed on all ten rows. And Customers shows 4, not 8, because customers who bought in several categories are counted once. Each measure is recalculated for the total; nothing is added up from the rows above it.

Same measures, different layout

Because measures are not tied to any layout, you can swap Category for Region and they still work. Replace the rows with Customers[Region] and add Avg Unit Price and Largest Order:

ABCDE
1RegionRevenueUnitsAvg Unit PriceLargest Order
2East8072335.09567
3North24958311.881900
4South12801775.29950
5West20586343.001680
6Grand Total664054122.961900

Customers[Region] in Rows. Avg Unit Price is Revenue ÷ Units, formatted with 2 decimals.

Avg Unit Price reuses two existing measures. If you later change how Revenue is calculated (say, to exclude returns), Avg Unit Price follows automatically. Building small measures and combining them is the single best habit in DAX.

Blank results and empty rows

If a measure returns BLANK for a row item, the PivotTable hides that row (unless you tick Show items with no data in Field Settings). This is usually what you want. Be aware that COUNTROWS of an empty table returns blank, not 0. To show a zero, write Orders := COUNTROWS ( Sales ) + 0, but think twice: forcing zeros makes every product appear for every customer, which can turn a 10-row report into a 10,000-row one.

Watch out. DAX functions such as SUM and AVERAGE take a column, not an expression. SUM(Sales[Qty] * Sales[Price]) is an error. For row-by-row arithmetic, use the iterator versions SUMX and AVERAGEX, covered in Iterators in DAX: SUMX, AVERAGEX, MAXX and RANKX.

Practice

  1. Create Units and Orders and check that their totals are 54 and 10 for the sample data.
  2. Create Avg Qty per Order := DIVIDE ( [Units], [Orders] ) and format it with one decimal.
  3. Create Products Sold := DISTINCTCOUNT ( Sales[ProductID] ). Put Customers[Customer] in Rows: Anna Weber bought three different products.
  4. Open Manage Measures and add a description to each measure.

Check yourself

1. Which formula counts the order lines in Sales?

2. Why is Customers at the Grand Total smaller than the sum of the category rows?

3. In the Measure dialog, what does the Table name setting do?

4. What is wrong with SUM ( Sales[Qty] * Sales[Price] )?

5. How is a measure written in the Calculation Area?