DAX Reference
DAX Reference
DAX Reference
Introduction to DAX
Data Analysis Expressions (DAX) is the formula language used to create custom calculations in Microsoft
PowerPivot for Microsoft Excel workbooks and SQL Server 2012 Analysis Services (SSAS) tabular model
projects. DAX formulas include functions, operators, and values to perform advanced calculations on
relational tables and columns, including lookups to related values and related tables.
While DAX applies to both PowerPivot workbooks and tabular model projects, this topic applies more
specifically to tabular model projects authored in SQL Server Data Tools (SSDT).
Select Evaluate
Distinct Values
Count Row(Countrows
Count(Distinct Row(DistinctCount
Where Filter
Top TOPN
Inner Join Generate
Group by Summarize
Rank RANKX
Min,Max Min,max
GetDate() NOW()
Dateadd eDate
PATINDEX Find()-case-sensitivity
Search()- No Case-sensitivity
Replace() Substitute()
Stuff() Replace()
View a Table
T-SQL
Select *
from [Internet Sales]
DAX
DAX
Select count(*)
from [Internet Sales]
DAX
evaluate(
ROW ( "Count" ,COUNTROWS('Internet Sales') )
)
Distinct Count
T-SQL
DAX
evaluate(
)
Building Blocks of DAX Queries
Where Clause
We can filter rows in a DAX query using the filter function which accepts a Boolean evaluation in the
same way the T-SQL where clause does.
T-SQL
Select * from
Internet Sales
where ProductKey = 373
DAX
T-SQL
Select *
from Internet Sales
where ProductKey = 373
and OrderDateKey > 20080000
DAX
evaluate(
filter(
filter('Internet Sales', [ProductKey] = 373)
, [OrderDateKey] > 20070000)
)
Order By Clause
The order by clause should be very familiar
T-SQL
Select *
from Internet Sales
order by ProductDateKey
DAX
evaluate(
'Internet Sales'
)
order by [ProductKey]
TOP Clause
It’s worth noting that the TOPN function in DAX requires an ordering expression (OrderDateKey), rather
than allowing the combination of TOPN and Order By. But don’t be fooled into thinking the result will be
returned in order because there is no guarantee. This does mean however that a result set can be
restricted by a definitive row count and then re-order again by the Order By clause. It’s also worth noting
that the DAX equivalent is always with ties.
T-SQL
DAX
evaluate(
TOPN(10, 'Internet Sales', [OrderDateKey])
)
evaluate(
TOPN(10, 'Internet Sales', [OrderDateKey])
)order by [OrderDateKey])DESC
T-SQL
DAX
evaluate(
summarize(
'Internet Sales',
[ProductKey],
"Total Sales", SUM('Internet Sales'[Sales Amount] )
)
)
Note: Be careful to use double quotes when specifying the column headings
for your aggregated column (“Total Sales”).
DAX
evaluate(
summarize(
'Internet Sales',
[ProductKey],
[PromotionKey],
"Total Sales", SUM('Internet Sales'[Sales Amount]),
"Average Gross Profit", SUM('Internet Sales'[Gross
Profit])
)
)
order by [ProductKey], [PromotionKey]
Ranking
The ability to rank results is an important feature is any BI application. DAX has two ranking functions;
RANKX() and RANK.EQ(). RANK.EQ is the equivalent to the excel ranking function RANK.EQ and allows
you to find the rank of a number in a list of numbers. I won't go into any further details regarding
RANK.EQ() because RANKX() is the function that you will frequently use for ranked reports. RANKX()
ranks a value against a range of values. In the example below the internet sales amount for a product is
ranked against the internet sales amount of all products.
T-SQL
SELECT [ProductKey]
,sum([SalesAmount]) AS 'Sum Total Sales'
, RANK() OVER ( ORDER BY SUM([SalesAmount]) DESC) AS Rank
FROM [FactInternetSales]
GROUP BY ProductKey
DAX
EVALUATE (
SUMMARIZE ('Internet Sales'
,[ProductKey]
,"Sum Internet Sales",
sum('Internet Sales'[Sales Amount])
,"Rank",RANKX(ALL('Internet
Sales'[ProductKey]),SUMX(RELATEDTABLE('Internet Sales'),[Sales Amount]))
)
)
order by [Rank]
T-SQL
DAX
evaluate(
row(
"MinOrderDate", min('Internet Sales'[OrderDateKey]) ,
"MaxOrderDate", max('Internet Sales'[OrderDateKey]) )
)
http://blogs.msdn.com/b/analysisservices/archive/2010/04/12/time-intelligence-functions-in-dax.aspx
T-SQL
DAX
evaluate(
ROW ( " Timestamp’" ,NOW() )
)
T-SQL
DAX
evaluate(
ROW ( "Beginning of time" ,DATEVALUE( "01/01/1900") )
)
T-SQL
DAX
The execution of date() here returns 31/12/1899, which appears to be a bug. As year() correctly returns
1900
evaluate(
ROW ( "First Year" , Year(Date(1900,1,1)),
"First Month", Year(Date(1900,1,1)),
"First Day", Day(Date(1900,1,1)) )
)
T-SQL
DAX
evaluate(
ROW ( "One month previous" ,eDate(now() ,-1) )
)
Text Processing
T-SQL
evaluate(
ROW ( "FIND", FIND( "A", "ABC" ), "Search", SEARCH( "a", "ABC" )
)
)
With start-location and default return parameters, the search starts at the second character and returns -1
if the search/find is unsuccessful.
evaluate(
ROW ( "FIND", FIND( "A", "ABC",2,-1 ), "Search", SEARCH( "a",
"ABC", 2,-1 ) )
)
Replace-Substitute, Stuff-Replace
In DAX we can compare the T-SQL replace() function to the DAX Substitute() function and the T-SQL
stuff() function to the DAX Replace() function. Which is a little confusing, but here are some examples.
T-SQL
DAX
evaluate(
ROW ( "Pen Colour", Substitute("The pen is blue","blue","red"),
"Sky Colour", Replace("The pen is blue", 5,3, "Sky") )
)
Value
The Value() function can be compared with the T-SQL cast or convert functions. It takes a string literal
representation of a number and converts it to a number type.
T-SQL
DAX