Sub topic 4
Totaling a Column with SUM
This chapter assumes you can work in Power BI Desktop's Report view and Fields pane, and that your model has a Sales table with a numeric SalesAmount column. You should also know how to create a measure from the formula bar, which the earlier chapter on creating your first measure covers.
What SUM does
SUM is a DAX aggregation function. It takes one column reference and adds up every value in that column that is visible in the current filter context. The filter context is the set of filters that slicers, visuals, and report filters apply to a calculation.
The syntax is:
SUM(Table[Column])The argument must be a column, not an expression. SUM(Sales[SalesAmount] * 2) is invalid. If you need to sum a calculated value row by row, use SUMX, which is outside this chapter.
SUM also needs a numeric column. Text columns raise an error. Blank cells are ignored.
Writing the measure
Select the Sales table in the Fields pane, choose New measure on the Home tab, and enter this in the formula bar:
Total Sales = SUM(Sales[SalesAmount])Press Enter. The measure appears in the Sales table with a calculator icon. The part before = is the measure name. The part after it is the expression Power BI evaluates.
Notice that the formula refers to the column with its table name: Sales[SalesAmount]. Always write the table name when you reference a column. It makes the formula easier to read and tells measures apart from columns at a glance.
One formula, many results
The measure does not store a number. Power BI evaluates it again each time a visual needs it, using the filters that apply to that visual. This is why one Total Sales measure can give different answers across a report.
Suppose your Sales table contains these rows:
| Region | Product | SalesAmount |
|---|---|---|
| West | Bike | 1200 |
| West | Helmet | 80 |
| East | Bike | 1500 |
| East | Helmet | 120 |
The same measure produces different results depending on where you place it:
| Where the measure is used | Filter context | Result |
|---|---|---|
| Card visual, no slicers | None | 2900 |
| Card visual, Region slicer set to West | Region = West | 1280 |
| Table visual, one row per Product | One product per row | Bike 2700, Helmet 200 |
| Table visual, Product = Bike and Region = East | Both filters | 1500 |
You write the formula once. The filter context decides which rows SUM sees.
Using the measure in a visual
To check your work:
- Add a Card visual and drag
Total Salesinto its Fields well. It should show the sum of the whole column. - Add a Table or Clustered column chart visual. Put a column such as
Producton the axis andTotal Salesin the values. - Add a Slicer on
Region. Click values in the slicer and watch both visuals change.
If a number looks wrong, compare it with the rows you expect. A common cause is a leftover filter on the visual or page.
Common errors
- Using a text column. If
SalesAmountwas imported as text,SUMfails or the column does not appear as a number. Fix the data type in Power Query or the column tools. Do not work around it in the measure. - Dropping the table name.
SUM([SalesAmount])may work in some cases, but it is ambiguous and harder to maintain. UseSales[SalesAmount]. - Passing an expression.
SUMaccepts only a column. Row-by-row calculations need an iterator function such asSUMX. - Expecting a total that ignores filters. A measure always respects the filter context. If you want a grand total regardless of slicers, that needs a different technique, covered later in DAX.
SUM and the next measure
Total Sales is the numerator for the ratio you will build in a later chapter. The next chapter adds COUNTROWS, which gives you the denominator. Once both exist, a third measure can refer to them by name instead of repeating their logic.
Exercise
A Card visual uses the measure Total Sales = SUM(Sales[SalesAmount]). With the sample rows in this chapter, you select Region = East in a slicer. What does the card show, and why does it differ from the unfiltered value?
Show answer
It shows 1620 (1500 + 120). The slicer adds a filter context of Region = East, so SUM sees only the East rows. The measure formula did not change. Only the rows visible to it did.
Exercise
Why does SUM(Sales[SalesAmount] * 2) fail, and which function would you look at instead?
Show answer
SUM accepts only a column reference, not an expression. To evaluate an expression for each row and then add the results, use the iterator function SUMX.