--- title: Introduction to DAX - Part 2 (Revision) tags: dax-foundation-course --- :::info DAX materials for Analyzing Data in Power BI course. [datapot.vn](datapot.vn), 2022. ::: ## Obj: Understand `CALCULATE` function ### Task 1: Using Boolean Expression in `CALCULATE` function **Requirement**: Calculate Revenue of products with red or blue color ```python= Revenue Red/Blue (boolean filter) = // Using boolean filter to calculate CALCULATE( [Revenue], 'Product'[Color] IN {"Red", "Blue"} ) ``` **Requirement**: Calculate Revenue of products having high price (its list price >= $1000) ```python= Revenue Expensive Products = CALCULATE( [Revenue], 'Product'[ListPrice] > 1000 ) ``` **Requirement**: Calculate Revenue in the last day of each period ```python= Revenue at last day = CALCULATE( [Revenue], Sales[OrderDate] = MAX('Date'[Date])) ``` ![](https://hackmd.io/_uploads/r15Q7XQzq.png) ### Task 2: Using Table Expression in `CALCULATE` function **Requirement**: Calculate number of order line having high profit margin (>= 30%) ```python= High profit margin = CALCULATE( COUNTROWS(Sales), FILTER(Sales, [Profit Margin] > 0.3)) ``` **Requirement**: Calculate revenue of high margin product (product with list price is higher than twice of its standard cost) ```python= High Margin Product Sales = CALCULATE( [Revenue], FILTER( 'Product', 'Product'[ListPrice] > 'Product'[Standard Cost] * 2 ) ) ``` **Requirement**: Calculate revenue of months that have profit margin is higher than 5% ```python= Sales for Profitable Months = CALCULATE( [Revenue], FILTER( VALUES('Date'[Month]), [Profit Margin] > 0.05) ) ``` ![](https://hackmd.io/_uploads/B1-jBm7Gq.png) ### Task 3: Compare two types of expression in `CALCULATE` function Create the following measure: ```python= Revenue Red/Blue (table filter) = // Using table filter to calculate CALCULATE( [Revenue], FILTER( 'Product', 'Product'[Color] in {"Red", "Blue"})) ``` Then create a matrix to compare the two measures as follows: ![](https://hackmd.io/_uploads/S1FueQXGc.png) Read this: [BOOLEAN or FILTER expression?](https://docs.microsoft.com/en-us/dax/best-practices/dax-avoid-avoid-filter-as-filter-argument)