Sub topic 2
Totaling a Column with SUM
Assume you know the Power BI Desktop Report view, tables, columns, and rows. This chapter uses a Sales table with a numeric SalesAmount column. A measure is a DAX calculation evaluated when a visual needs a result. Unlike a calculated column, it does not store one value for every row.
Totaling a Column with SUM
Create a measure by selecting the Sales table in the Fields pane, choosing New measure, and entering this formula in the formula bar:
Total Sales = SUM(Sales[SalesAmount])SUM adds the values in the specified numeric column. The measure responds to the current filter context, which includes filters from slicers, page filters, and the fields used in a visual.
For example, a card showing [Total Sales] might show all sales. Adding Product[Category] to a table causes Power BI to evaluate the measure separately for each category.
Counting Rows with COUNTROWS
Use COUNTROWS when you need the number of rows in a table:
Order Lines = COUNTROWS(Sales)This counts rows in the current filter context. If each row represents one order line, the measure counts order lines. It does not necessarily count unique orders. For unique orders, you would need a distinct-count calculation using an order identifier.
Building Measures from Other Measures
Measures can reference other measures. This keeps common logic in one place and makes later changes easier.
Average Sales per Order Line =
DIVIDE([Total Sales], [Order Lines])The expression uses the already-defined measures rather than repeating their formulas.
Calculating Ratios with DIVIDE
DIVIDE is safer than the / operator when the denominator might be zero or blank. It returns blank by default in that case:
Average Sales per Order Line (0 if none) =
DIVIDE([Total Sales], [Order Lines], 0)The optional third argument returns 0 when there are no rows. Choose blank when “no result” should remain distinct from zero.
Exercise
What does [Total Sales] return when a visual is filtered to one product category?
Show answer
It returns the sum of Sales[SalesAmount] for the rows that remain visible in that category's filter context.