Create measures using DAX in Power BI
Tip: All calculations can be copied from the D:\PL300\Demo\Resources\Snippets-Demo-05.txt file.
Create a calculated table
- Create a calculated table by using the following expression:
Date = CALENDARAUTO()
- Switch to Data view, and review the table, which comprises a single date column.
Create calculated columns
- Add a calculated column to the Date table:
Year = "CY" & YEAR('Date'[Date])
- Add an additional calculated column to the Date table:
Month = FORMAT('Date'[Date], "YYYY-MM")
-
In Model view, create a relationship by dragging the Date table Date column to the Sales table OrderDate column.
-
Hide the Sales table OrderDate column.
-
In the Date table, create the Calendar hierarchy, with Year and Month levels.
-
In Report view, mark the Date table as a date table using the Date column.
-
In the matrix visual, remove the Products hierarchy, and then replace it with the Calendar hierarchy.
-
Add a calculated column to the Sales table:
Cost = 'Sales'[Quantity] * RELATED('Product'[Cost])
- Format the Cost column to two decimal places.
Create a Quick Measure
-
Add a quick measure to the Sales table, subtracting the Cost column from Profit column.
-
Rename the measure as Profit.
-
Explain that the measure does not store data in the model.
Create regular measures
- Add a measure to the Sales table:
Profit Margin = DIVIDE([Profit], SUM('Sales'[Sales]))
-
Format the Profit Margin column as a percentage.
-
Add another measure to the Sales table:
Sales YTD = TOTALYTD(SUM('Sales'[Sales]), 'Date'[Date])
- Format the Sales YTD column to two decimal places.
Validate the calculations with the matrix visual
-
Add the Cost, Profit, Profit Margin, and Sales YTD fields to the matrix visual.
-
Save the Power BI Desktop file.
-
Leave the Power BI Desktop file open for a later demo.