DAX Reference

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

DAX Quick 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()

Here is the list of common Formulas, functions we are using:

View a Table
T-SQL

Select *
from [Internet Sales]
DAX

Evaluate( 'Internet Sales' )

View a Single Column Distinct Values


T-SQL

Select distinct OrderDateKey


from Internet Sales

DAX

evaluate( values('Internet Sales'[OrderDateKey]) )

Count the Rows


T-SQL

Select count(*)
from [Internet Sales]

DAX

evaluate(
ROW ( "Count" ,COUNTROWS('Internet Sales') )
)

Distinct Count
T-SQL

Select count(distinct [ProductKey]) as ‘No Distinct Products’


From [Internet Sales]

DAX

evaluate(

ROW ( "No Distinct Products" ,DISTINCTCOUNT('Internet


Sales'[ProductKey]) )

)
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

evaluate( filter('Internet Sales', [ProductKey] = 373))

To add multiple filters we just need to nest them.

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

Select TOP 10 WITH TIES *


from [Internet Sales]
order by OrderDateKey

DAX

evaluate(
TOPN(10, 'Internet Sales', [OrderDateKey])
)

evaluate(
TOPN(10, 'Internet Sales', [OrderDateKey])
)order by [OrderDateKey])DESC

T-SQL

Select ProductKey, SUM([Sales Amount])


from [Internet Sales]
Group by ProductKey

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”).

And for multiple aggregations over columns, with ordering…


T-SQL

Select ProductKey, PromotionKey, SUM([Sales Amount]) , AVG([Gross Profit]


)
from [Internet Sales]
Group by [ProductKey], [PromotionKey]
Order by [ProductKey], [PromotionKey]

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]

Min and Max


The MIN() and MAX() functions is DAX work in much the same way as T-SQL.

T-SQL

Select Min(OrderDateKey) MinOrderDate


, Max(OrderDateKey) MaxOrderDate
From [Internet Sales]

DAX

evaluate(
row(
"MinOrderDate", min('Internet Sales'[OrderDateKey]) ,
"MaxOrderDate", max('Internet Sales'[OrderDateKey]) )
)

Dates and Time


DAX has a vast array of datetime functions and the concept of time being a contiguous dimension of any
event is implemented natively as part of SSAS. Time dimension details will not be covered in this
document, but I will give some examples of basic functionality.

To get the current date with Getdate()

http://blogs.msdn.com/b/analysisservices/archive/2010/04/12/time-intelligence-functions-in-dax.aspx

T-SQL

Select getdate() as ‘Timestamp’

DAX

evaluate(
ROW ( " Timestamp’" ,NOW() )
)

To Convert() a value to a datetime

T-SQL

select convert(datetime, '01/01/1900') as ‘Beginning of time’

DAX
evaluate(
ROW ( "Beginning of time" ,DATEVALUE( "01/01/1900") )
)

For the Year(), Month(), Day() functions

T-SQL

Select YEAR(0) as 'First Year'


, MONTH(0) as 'First Month'
, DAY(0) as 'First Day'

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)) )
)

To perform simple date arithmetic

T-SQL

select dateadd(month,-1,getdate()) as 'One month previous'

DAX

evaluate(
ROW ( "One month previous" ,eDate(now() ,-1) )
)

Text Processing

Find & Search


The DAX find() and search() functions are a rather curious implementation. There are subtle differences,
with the case-sensitivity characteristic being the most obvious. Search() will ignore case and find() will
retain case-sensitivity. Both find() and search() functions can be compared to the patindex() T-SQL
function but they extend the functionality to include start indices and default return location parameters.

T-SQL

Select PATINDEX( 'a', 'ABC' ) AS [Pattern index]


DAX

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

Select replace( "the pen is blue", "blue", "red") [Pen colour]


, stuff( "the pen is blue",5,3,"sky") [Sky colour]

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

Select cast("4" as int) as 'Number 4'

DAX

Evaluate ( ROW ( "Number 4", VALUE("4") ) )

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy