Sub topic 4
Totaling a Column with SUM
This chapter covers SUM, the function behind most first measures. You will write the measure, see how filters change its result, and learn the errors people hit most often.
I assume you can already:
- Open Power BI Desktop and work in Report view, the Fields pane, and basic visuals.
- Create a measure from the formula bar, as shown in the earlier chapter on creating your first measure.
- Describe a measure as a calculation evaluated on demand rather than stored per row.
The examples use a Sales table with numeric columns SalesAmount and Quantity. They also use a related Product table with a Category column. If your model has different names, substitute your own.
What SUM does
SUM adds up every number in one column. It takes exactly one argument, a column reference, and returns a single number.
Total Sales = SUM(Sales[SalesAmount])Three details explain most of the behavior you will see:
- It adds only the rows that survive the current filters. The measure does not store one fixed number. Power BI evaluates it again for each cell in a visual, and each cell has its own set of visible rows.
- It skips blanks. A blank
SalesAmountadds nothing. If every visible row is blank, or no rows are visible, the result isBLANK, not 0. - It accepts only a column. The argument must be a reference of the form
Table[Column]. It cannot be an arithmetic expression or a table.
The column must have a numeric data type, such as whole number, decimal, or fixed decimal. Dates are accepted as well, but summing them is rarely meaningful.
Writing the measure
Create the measure on the Sales table:
- Select the
Salestable in the Fields pane. - Choose New measure on the Home or Table tools ribbon.
- Type the formula in the formula bar and press Enter.
Total Sales = SUM(Sales[SalesAmount])The text before the equals sign is the measure's name. The text after it is the expression. You can add a second measure the same way:
Total Quantity = SUM(Sales[Quantity])Drag Total Sales into a card visual. The card shows the sum of SalesAmount over the whole table, because nothing is filtering it yet.
A worked example
Suppose the Sales table holds these five rows. Row 4 has no amount recorded.
| Row | Category | SalesAmount |
|---|---|---|
| 1 | Bikes | 1200 |
| 2 | Bikes | 800 |
| 3 | Helmets | 150 |
| 4 | Helmets | (blank) |
| 5 | Helmets | 50 |
Here is what Total Sales returns in several contexts:
| Context | Visible rows | Total Sales |
|---|---|---|
| Card with no filters | 1, 2, 3, 4, 5 | 2200 |
| Table visual, row for Bikes | 1, 2 | 2000 |
| Table visual, row for Helmets | 3, 4, 5 | 200 |
| Card with a slicer set to Helmets | 3, 4, 5 | 200 |
The Helmets total is 200, not 150, because row 5 is included. Row 4 is blank, so it contributes nothing. It does not cause an error or turn the total into blank.
How filters change the result
The formula SUM(Sales[SalesAmount]) never mentions Bikes or Helmets. The filtering comes from the report. Each place a value is drawn creates a filter context: a set of filters that limits which rows SUM can see. Filter context comes from several sources:
- Fields placed on the rows, columns, or axis of a visual
- Slicers
- Filters in the Filters pane, at visual, page, or report level
- Cross-highlighting when you click a bar or slice in another visual
Filters also travel across relationships. If Product[Category] is related to Sales through a product key, a filter on Category narrows the Sales rows automatically. This works in the usual direction: from the one side (Product) to the many side (Sales). The measure formula needs no extra code for that.
You can check this yourself with a table visual. Put Product[Category] and Total Sales in it. Each row shows a different value from the same formula. Then add a slicer for year and change its selection. Every value changes again.
Checking a measure in DAX query view
DAX query view in Power BI Desktop lets you evaluate a measure without building a visual. Open it from the left-hand view switcher and run:
EVALUATE
SUMMARIZECOLUMNS(
'Product'[Category],
"Total Sales", [Total Sales],
"Total Quantity", [Total Quantity]
)The result is one row per category with both totals. This is a quick way to confirm that a measure behaves as you expect before it goes into a report.
Totals and subtotals
SUM is additive. The total of the category rows equals the grand total, because the categories do not overlap. The grand-total row of a table visual is evaluated with no category filter, so SUM sees all rows.
This is not true of every measure. Averages and distinct counts do not add up this way. For SUM over a clean table it holds, so a mismatch between a total and its parts is a sign of a data problem, covered in the next section.
Common errors and surprises
The column name has no table prefix
Total Sales = SUM(SalesAmount)This fails. DAX expects a column reference in the form Table[Column]. Write SUM(Sales[SalesAmount]). Typing Sales[ in the formula bar lists the columns and fills in the reference for you.
The column is text
If SalesAmount was imported as text, for example because some values contain a currency symbol or the word "N/A", SUM cannot work with it. The formula bar reports an error about the data type. Fix the type at the source. In Power Query, remove the stray characters, replace the invalid values, and change the column type to a number. Do not wrap the column in a conversion inside the measure to hide the problem.
An expression is passed instead of a column
Revenue = SUM(Sales[Quantity] * Sales[UnitPrice])This is invalid because SUM takes a column and not an expression. You have two options: create a calculated column for the product and sum that column, or use SUMX, which evaluates an expression row by row. SUMX is outside the scope of this chapter. The point here is to know why the error appears.
The measure and a column share a name
A measure cannot have the same name as a column in the same table. If you name a measure SalesAmount on the Sales table, you will get an error. A descriptive name such as Total Sales avoids the clash and makes the intent clearer in a visual.
The result is blank where you expected 0
If no Sales rows are visible, or all visible amounts are blank, SUM returns BLANK. A table visual usually hides rows whose measures are all blank. A card shows an empty value. If a report needs a visible zero, handle that case deliberately and understand what it hides. Changing blank to zero everywhere can also make visuals list many rows that have no data.
The total is too high
If SalesAmount totals look inflated, check the grain of the table. Suppose each row is one order line, but an order-level amount such as shipping is repeated on every line of the order. SUM then counts it once per line. SUM does exactly what it is told. The fix is in the data model: store order-level values in an order-level table, or divide them properly across lines.
Duplicate rows
Rows loaded twice, for example from overlapping source files, are summed twice. Compare COUNTROWS(Sales) with the count you expect from the source. The next chapter introduces COUNTROWS.
Explicit versus implicit sums
If you drag the SalesAmount column itself into a visual, Power BI sums it automatically. This is an implicit measure. The default aggregation, shown on the column's Summarize by property, is Sum for numeric columns. Implicit measures are convenient, but they have drawbacks:
- They exist only inside one visual, so you cannot reuse the logic.
- You cannot reference them from another measure.
- Anyone editing the visual can change Sum to Average or Count with a few clicks, silently changing the meaning.
- They have no consistent name or format across the report.
An explicit measure like Total Sales is defined once and used everywhere. It can be referenced by other measures, which is how the later ratio chapters build on it. Prefer the explicit measure.
Two settings keep implicit sums from creeping back in:
- For numeric columns that should never be summed, such as a year, a product key, or a unit price, set Summarize by to None in the Column tools ribbon.
- Hide the raw column in Report view once you have an explicit measure for it. Select the column, then use the Properties pane or the right-click menu to hide it.
Measures are marked with a calculator icon in the Fields pane. Implicit aggregations on columns show a sigma (Σ) icon. If a numeric field shows a sigma, the column is still available for implicit summing.
Naming and format
Set the format once on the measure, and every visual that uses it inherits the setting. Select the measure, then use the Measure tools ribbon to choose a format such as currency or whole number and the number of decimal places. A measure that returns money with no format shows raw digits in a card, which is easy to misread. Short, plain names that say what the number is, such as Total Sales and Total Quantity, are easier to find and read than names like Measure1.
Summary
SUM(Table[Column])adds the values of one numeric column across the rows visible in the current filter context.- Blanks are skipped, and an all-blank result is
BLANK, not 0. - The same formula gives different results in each visual cell, because slicers, visual fields, and filters narrow the rows.
- Most errors come from a missing table name, a text column, or an expression passed where a column is required.
- An explicit measure beats an implicit sum because you can name it, format it, reuse it, and reference it from other measures.
Exercise
A table visual shows Category and Total Sales, with Total Sales defined as SUM(Sales[SalesAmount]). The Gloves row shows 300. You add a slicer for Year and pick 2025, and the Gloves row now shows 120. Did the formula change, and what explains the new value?
Show answer
The formula did not change. The slicer added a filter on Year, so SUM now sees only the Sales rows for Gloves in 2025. Those rows add up to 120. The 300 was the total across all years.
Exercise
A colleague writes Revenue = SUM(Sales[Quantity] * Sales[UnitPrice]) and gets an error. Why does it fail, and what are two ways to fix it?
Show answer
SUM accepts only a column reference, not an expression. One fix is to add a calculated column on Sales that multiplies Quantity by UnitPrice, then use SUM on that column. The other is to use SUMX, which evaluates the expression row by row and adds the results.