Introduction to Tabular Data Models Julie Koesmarno | Consultant, MsSQLGirl Patrick LeBlanc | Senior Technical Specialist, Microsoft
04 | Querying with DAX Julie Koesmarno | Consultant, MsSQLGirl Patrick LeBlanc | Senior Technical Specialist, Microsoft
Module Overview Introduction To DAX Syntax Writing DAX Queries Outside SSMS Using DAX Queries In Practical World
Introduction To DAX Syntax
Querying With DAX Usage Syntax / Functions EVALUATE SUMMARIZE TopN Many others to look into, e.g. ADDCOLUMN GENERATEALL CALCULATETABLE Tools to use DAX Editor DAX Studio When this would be useful Actions SSRS Report
DAX in SSMS: Evaluate DAX in DAX Editor: Summarize
EVALUATE [DEFINE {MEASURE <tableName>[<name>] = <expression>} EVALUATE <table> [ORDER BY {<expression> [{ASC | DESC}]}[, …] [START AT {<value>|<parameter>} [, …]]] Retrieve data from table expression. T-SQL Equivalent SELECT * FROM <table> Note Formatting configured in Tabular Model is lost
EVALUATE is similar to EVALUATE 'InternetSales' SELECT * FROM [dbo].[FactInternetSales] EVALUATE 'InternetSales'
EVALUATE
SUMMARIZE SUMMARIZE(<table>, <groupBy_columnName>[, <groupBy_columnName>]…[, <name>, <expression>]…) Returns requested aggregated values over a set of groups T-SQL Equivalent SELECT <columns>, <aggregated columns> FROM <table> GROUP BY <columns> Type Statistical Function
SUMMARIZE is similar to … EVALUATE SUMMARIZE ( 'InternetSales', 'Product'[Product Name], "Sales", SUM ( 'InternetSales'[Sales Amount] ), "Revenue", 'InternetSales'[Revenue] )
SUMMARIZE SELECT p.[EnglishProductName], SUM(f.[SalesAmount]) AS Sales, SUM(f.[SalesAmount] - f.[ProductStandardCost] - f.[Freight] - f.[TaxAmt]) AS Revenue FROM [dbo].[FactInternetSales] f INNER JOIN [dbo].[DimProduct] p ON p.[ProductKey] = f.[ProductKey] GROUP BY p.[EnglishProductName];
SUMMARIZE
TopN TOPN(<n_value>, <table>, <orderBy_expression>, [<order>[, <orderBy_expression>, [<order>]]…]) Returns a table with the Cartesian product between each row in table1 and the table that results from evaluating table2 in the context of the current row from table1. T-SQL Equivalent SELECT <columns> FROM <table1> LEFT OUTER JOIN <table2> Type Filter Function
TopN EVALUATE FILTER ( GENERATE ( VALUES ( 'Date'[Calendar Year] ), ADDCOLUMNS ( TOPN ( 10, VALUES ( 'Product'[Product Name] ), 'InternetSales'[Sum of Sales], 0 ), "Sales", 'InternetSales'[Sum of Sales] ) ), 'InternetSales'[Sum of Sales] > 0 ) ORDER BY 'Date'[Calendar Year], "Sales" DESC
TopN – SQL Equivalent SELECT DISTINCT d.[CalendarYear], s.[EnglishProductName], s.[Sales] FROM [dbo].[DimDate] d OUTER APPLY (SELECT TOP 10 p.[EnglishProductName], SUM(sx.[SalesAmount]) Sales FROM [dbo].[FactInternetSales] sx INNER JOIN [dbo].[DimDate] dx ON dx.[DateKey] = sx.[OrderDateKey] INNER JOIN [dbo].[DimProduct] p ON p.[ProductKey] = sx.[ProductKey] WHERE d.[CalendarYear] = dx.[CalendarYear] GROUP BY dx.[CalendarYear], p.[EnglishProductName] ORDER BY SUM(sx.[SalesAmount]) DESC ) s WHERE s.[Sales] > 0 d.[CalendarYear] ASC, s.[Sales] DESC
Writing DAX Queries Outside SSMS
DAX in DAX Studio: TopN
Tools To Use Add-In To Excel Add-In To VS 2010, 2012 DAX Editor
Using DAX Queries In Practical World
Creating custom DAX query for Action
DAX in Reporting Tools Create a Report Project Change Command Type to DMX to write a DAX query Can also write MDX query as per normal SSAS
Alternative for SSRS If using In-Memory model, can use MDX on SSRS
Querying with DAX Session Takeaways Ability to write simple DAX Query Know what tools to use to write DAX Query Using DAX Queries to inspect data in DAX Studio Embedding DAX Queries into Action Using DAX Query in SSRS