IAC 2024 - IA Fast Track to Search Focused AI Solutions
DAX (Data Analysis eXpressions) from Zero to Hero
1. DAX (DATA ANALYSIS EXPRESSIONS)
FROM ZERO TO HERO
Frederik Vandeputte
Partner @Kohera
President @SQLUG.BE
frederik.vandeputte@kohera.be
2. AGENDA
• What’s DAX
• Calculated columns
• Calculated measures
• Some common DAX functions
• Working with DAX and Dates
• Define security in the model
• Query the BI Semantic Model
3. DAX - DATA ANALYSIS EXPRESSION
• Introduced in PowerPivot v1
• DAX = Data Analysis Expressions
• DAX goals
• Make data analysis really easy
• Uses relationships defined in PowerPivot/BISM model (no need for VLOOKUP)
• Excel like Syntax
• Support for +/- 80 Excel function
• 35 Built in Time Intelligence functions
• + Additional function
• SQL Server 2012 – Analysis Services Tabular Models
7. CALCULATED COLUMNS
• Just another column in a table
• Similar to named calculations in SSAS OLAP Datasource views
• Calculated after data has been loaded (cube processing)
• Can be placed in colums, rows, filters and slicers
• Evaluated in row context
• Examples
• [Amount] =[Qty] *[Price]
• [Full Name] = [First Name] & “ “ & [Last Name]
8. CALCULATED MEASURES
• Created in the measure grid
• Calculated “on the fly”
• Can only be placed in values area of PivotTables
• Evaluated in filter context
• Rows/Columns/filters/Slicers
• Examples
• [Sales] = SUM (Sales[Amount])
• [Average Sale] = AVERAGEX ( Sales, Sales[Amount])
10. MEASURES MUST BE EXPLICITELY CREATED
• PowerPivot will create implicit measures when you drag a column
to the values area in a pivot table
• Not possible in BISM (No_ measure defined)
• Like in SSAS OLAP – measures must be created explicitely
• Eeasiest way: AutoSum in SQL Data Tools
11. SOME COMMON DAX FUNCTIONS
• DISTINCTCOUNT()
• RELATED()
• CALCULATE()
12. RELATED(COLUMN) AND RELATEDTABLE(TABLE)
• More powerfull than VLOOKUP
• VLOOKUP only returns 1st match
• Relies on relationships in PowerPivot
• Follows many to one relationship
13. RELATED(COLUMN)
FactSales • Storekey
• Travels many to one direction
• Can travel multiple relationships •Geograpghy
DimStore
• Add calculated columns to FactSales
Key
• =RELATED(DimStore[StoreName])
• =RELATED(DimGeography[ContinentName])
DimGeography
14. CALCULATE(EXPRESSION, SETFILTER1, SETFILTER2, …)
• CALCULATE more complex than previous functions, but
very powerful
• CALCULATE is the Queen Of DAX
• CALCUATE
• Using the SetFilter arguments, modify the Filter Context
• If there is a Row Context, move that Row Context onto the Filter Context
• Evaluate the Expression in the newly modified Filter Context
15. DAX AND TIME INTELLIGENCE
• +/- 35 DAX time intelligence functions
• DAX helps you when calculating with dates
• But you must follow the rules
16. THE GOLDEN RULES WHEN WORKING WITH DATES
• Always create a separate Date table.
• Make sure your date table has a continues date range
• Create relationships between fact tables and the Date
table.
• Never use the datetime column from the fact table in time
functions.
• The datetime column in the Date table should be at day
granularity (without fractions of a day).
• Mark the Date table as a Date Table and set the Date
column.
17. IF YOU DON’T HAVE A SEPERATE TIME TABLE ERRORS
18. IF YOU HAVE A SEPERATE TIME TABLE CORRECT VALUES
19. DATE CALCULATIONS
• TOTALYTD function
• Calculated Year To Date Values
• SalesAmtYTD:=TOTALYTD(SUM(AutoSales[SalesAmount]);Dates[Date])
WRONG RESULTS
20. CREATE A DATE DIMENSION
• Mark Time Dimension Table as Date
• Use data column from Time dimension in Calculations
• Sort Columns By key column
• Create Hierarchies (in Diagram view)
CORRECT RESULTS
21. CREATE THE PERFECT DATE DIMENSION
• Follow the golden rules
• Sort Columns By key column
• Create Hierarchies
22. TIME INTELLIGENCE EXAMPLES FOR SALES
Time Period Formula
Current Period =SUM([SalesAmount])
MTD = CALCULATE(SUM(SalesAmount), DATESMTD(Dates[Date]))
QTD = CALCULATE(SUM(SalesAmount), DATESQTD(Dates[Date]))
YTD = CALCULATE(SUM(SalesAmount), DATESYTD(Dates[Date]))
Current Period Last Year = CALCULATE(SUM(SalesAmount), DATEADD(Dates[Date],-1,YEAR))
PriorYearMTD = CALCULATE(SUM(SalesAmount),
DATEADD(DATESMTD(Dates[Date]),-1,YEAR))
PriorYearQTD = CALCULATE(SUM(SalesAmount),
DATEADD(DATESQTD(Dates[Date]),-1,YEAR))
PriorYearYTD = CALCULATE(SUM(SalesAmount),
DATEADD(DATESYTD(Dates[Date]),-1,YEAR))
23. DAX EDITOR
• Code highlighting
• No intellisense
• Free download at http://daxeditor.codeplex.com/
24. WHAT CAN WE DO WITH DAX
• Create calculated columns
• Create (calculated) measures
• Define security in the model
• Query the BI Semantic Model
28. DAX QUERIES IN REPORTING SERVICES (SQL DATA TOOLS)
• Create SSAS DataSource
• Create Dataset
• In Query Designer
• Switch to Command Type DMX
• Switch to Design Mode
• Define Parameters
29. DAX QUERIES IN REPORT BUILDER
• Query designer does not support DMX!!
• Create OLEDB Datasource
• Create Dataset
• Write the query as an expression
and concatenate using
Parameters.ParamName.Value
30. SO, WHAT CAN WE DO WITH DAX
• Create calculated columns
• Create (calculated) measures
• Define security in the model
• Query the BI Semantic Model
• DAX is Simple, but it’s not always Easy
Carry forward the existing ecosystem of client applicationsLook a bit deeper into the conceptual architectureCall out PowerPivot as a clientMDX versus DAXData access: cache the data or pass it through Vertipaq new in-memorycolunmstore, compression, blazing fast perf. No aggregations or tuning. Same as PowerPivot for Excel and SharePoint.