Successfully reported this slideshow.
Upcoming SlideShare
×

# Slides for PUG 2018 - DAX CALCULATE

168 views

Published on

Explaining CALCULATE and the FILTER context.

Published in: Software
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Slides for PUG 2018 - DAX CALCULATE

1. 1. CALCULATE – The Most Powerful DAX Function Ike Ellis, MVP
2. 2. Order Line Total = 'Sales OrderDetails'[qty] * 'Sales OrderDetails'[unitprice]
3. 3. Total Sales = SUM('Sales OrderDetails'[Order Line Total])
4. 4. select sum(Sales.OrderDetails.unitprice * Sales.OrderDetails.qty) from sales.OrderDetails
5. 5. Name the measure. You’ll use that in the visualization Built in DAX formula Table name Column nameEquals sign separates expression name from expression formula Total Sales = SUM('Sales OrderDetails'[Ordera Line Total])
6. 6. Days To Ship = DATEDIFF('Sales Orders'[orderdate], 'Sales Orders'[shippeddate],DAY) Average Days to Ship = AVERAGE('Sales Orders'[Days To Ship])
7. 7. Dates = CALENDAR("1/1/2000", "12/31/2016")
8. 8. Order Line Total = 'Sales OrderDetails'[qty] * 'Sales OrderDetails'[unitprice]
9. 9. Total Sales by year and with a page filter.
10. 10. Measures Calculated Columns Different functions Uses Row Contex Doesn’t take up space Mostly text Less space Executes at the point data is read into the model and saved (value is static) Filter Context Executed at the time it is used
11. 11. Beverages Total Sales = CALCULATE ( SUM('Sales OrderDetails'[Order Line Total]) , 'Production Categories'[categoryname] = "Beverages" ) AGGREGATION FILTER Look at the tab CALCULATE
12. 12. CALCULATE(<expression>,<filter1>,<filter2> …)
13. 13. Total Sales - Beverages in USA = CALCULATE( sum('Sales OrderDetails'[Order Line Total]) , 'Production Categories'[categoryname]= "Beverages" , 'Sales Customers'[country] = "USA" )
14. 14. Country Count = COUNTROWS( VALUES('Sales Orders'[shipcountry])) Country Count wo VALUES = COUNTROWS('Sales Orders’)
15. 15. Country Count Total = COUNTROWS(All('Sales Orders'[shipcountry]))
16. 16. Total Sales For Customers with Minimum Order Count v2 = VAR MinimumOrderCount = 10 VAR CustomersWithMinimumOrders = FILTER('Sales Customers', [Number of Orders] > MinimumOrderCount) VAR CustomersWithMinimumOrdersResult = CALCULATE ( sum('Sales OrderDetails'[Order Line Total]) , CustomersWithMinimumOrders ) RETURN CustomersWithMinimumOrdersResult
17. 17. Number of Orders = COUNT('Sales Orders'[orderid]) Number of US Orders = CALCULATE ( COUNT ( 'Sales OrderDetails'[orderid] ) , FILTER ( 'Sales Customers' , 'Sales Customers'[country] = "USA" ) )
18. 18. Minimum Order Customers = VAR MinimumOrderCount = 10 var CustomersWithMinimumOrders = FILTER('Sales Customers', [Number of Orders] > MinimumOrderCount) RETURN CustomersWithMinimumOrders
19. 19. Total Sales For Customers with Minimum Order Count = VAR MinimumOrderCount = 5 VAR CustomersWithMinimumOrders = CALCULATE ( sum('Sales OrderDetails'[Order Line Total]) , FILTER('Sales Customers', [Number of Orders] > MinimumOrderCount) ) RETURN CustomersWithMinimumOrders
20. 20. Data Type Variable Name Variable Value RETURN myVar + 25 Expressions must use RETURN to return a value
21. 21. YTD Total Sales = TOTALYTD ( SUM('Sales OrderDetails'[Order Line Total]) , Dates[Date].[Date] )
22. 22. Total Sales Previous Month = CALCULATE ( sum('Sales OrderDetails'[Order Line Total]) , PREVIOUSMONTH(Dates[Date]) )
23. 23. Total Sales SUMX = SUMX( 'Sales OrderDetails' , 'Sales OrderDetails'[qty] * 'Sales OrderDetails'[unitprice ) Total Sales = SUM('Sales OrderDetails'[Order Line Total])
24. 24. Days To Ship = DATEDIFF ( 'Sales Orders'[orderdate] , 'Sales Orders'[shippeddate] , DAY )
25. 25. Total Sales By Ship Year = CALCULATE ( SUM('Sales OrderDetails'[Order Line Total]) , USERELATIONSHIP('Sales Orders'[shippeddate], Dates[Date]) ) Only one active relationship at a time
26. 26. • Parses • Formats • Shows execution plan • Connects to SSAS Tabular or Power BI Desktop
27. 27. http://www.craftingbytes.com http://blog.ikeellis.com http://www.ikeellis.com YouTube http://www.youtube.com/user/IkeEllisData San Diego Tech Immersion Group http://www.sdtig.com Twitter: @ike_ellis 619.922.9801 ike@craftingbytes.com