Sub topic 13
A first measure with DAX
This chapter assumes you have Power BI Desktop installed, a table named Sales loaded with a numeric SalesAmount column, and a basic idea of what Report view and the Data pane look like. You do not need earlier DAX experience.
What a measure is
DAX (Data Analysis Expressions) is the formula language of Power BI. A measure is a named DAX formula that Power BI calculates when a visual needs it. The result is not stored in the table. It is computed on the fly, using whatever filters apply to that visual.
This is different from dragging SalesAmount onto a visual and letting Power BI pick "Sum" for you. That is an implicit measure, and it depends on the column's summarization setting. An explicit measure is a formula you write and name yourself. It can be reused in any visual and does not change if someone alters the column's default summarization.
Creating Total Sales
- Open Report view and select the
Salestable in the Data pane. - On the Home ribbon, or the Table tools ribbon, choose New measure.
- Type the formula into the formula bar and press Enter.
Total Sales = SUM(Sales[SalesAmount])The part before = is the measure name. SUM adds up every value in the column it receives. Sales[SalesAmount] is the column reference, written as table name followed by the column name in square brackets.
The measure appears in the Data pane with a calculator icon. Drag it onto the canvas, or tick its checkbox, and Power BI draws a card or a column chart with the total.
Why the number changes
Put Total Sales in a table visual next to a Region field. Each row shows a different value, even though the formula never mentions regions. Power BI applies a filter for each row, and the measure recalculates inside that filter. Add a slicer and the same measure responds to it too.
The diagram below sketches the flow.
A second measure
A measure can count rows instead of adding values.
Order Count = COUNTROWS(Sales)COUNTROWS takes a table and returns how many rows are in it after filters are applied. This assumes each row in Sales is one order line. If one order has several lines, the count reflects lines, not orders.
Create this measure while in Table view or Model view and it still appears in the Data pane in every view. A measure belongs to a table, but it is not a column. You will not see it as a column of values in Table view.
Common errors
- Writing
SUM(SalesAmount)without the table name. DAX reports that it cannot find the column. Always qualify columns asTable[Column]. - Using
SUMon a text column.SUMneeds a numeric column. Check the data type in Table view first. - Creating a measure while the wrong table is selected. The measure still works, but it sits in an unexpected table. You can move it by selecting it and changing Home table in the Measure tools ribbon.
- Expecting a measure to show a value per row in Table view. Measures only produce values inside a visual.
Exercise
You wrote Total Sales = SUM(Sales[SalesAmount]) and placed it in a table visual with a Region field. Why does each row show a different number, and where does the measure appear in the Data pane?
Show answer
Power BI applies a filter for each Region row, and the measure recalculates within that filter, so each row sums only its own sales. The measure appears under the Sales table in the Data pane with a calculator icon, and it is visible in every view.