Data Analysis Expressions (DAX) Reference SQL Server 2012 Books Online Summary: The Data Analysis Expressions (DAX) language is a library of functions and operators that can be combined to build formulas and expressions. Category: Reference Applies to: SQL Server 2012 Source: SQL Server Books Online (link to source content) E-book publication date: June 2012 Copyright © 2012 by Microsoft Corporation All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Contents Data Analysis Expressions (DAX) Reference .......................................................................................................... 8 DAX Syntax Specification for PowerPivot .............................................................................................................. 8 DAX Operator Reference for PowerPivot ............................................................................................................ 15 Parameter-Naming Conventions ........................................................................................................................... 20 Understanding Functions for Parent-Child Hierarchies in DAX .................................................................. 22 DAX Function Reference ............................................................................................................................................ 27 DAX Queries ............................................................................................................................................................... 27 DAX Query Syntax Reference........................................................................................................................... 28 Parameters for DAX Queries ............................................................................................................................ 29 Date and Time Functions ...................................................................................................................................... 30 Time Intelligence Functions .............................................................................................................................. 31 CLOSINGBALANCEMONTH Function........................................................................................................ 32 CLOSINGBALANCEQUARTER Function .................................................................................................... 34 CLOSINGBALANCEYEAR Function .............................................................................................................. 35 DATEADD Function .......................................................................................................................................... 37 DATESBETWEEN Function ............................................................................................................................. 38 DATESINPERIOD Function ............................................................................................................................. 40 DATESMTD Function ....................................................................................................................................... 41 DATESQTD Function ........................................................................................................................................ 42 DATESYTD Function ......................................................................................................................................... 44 ENDOFMONTH Function ............................................................................................................................... 45 ENDOFQUARTER Function ............................................................................................................................ 46 ENDOFYEAR Function ..................................................................................................................................... 47 FIRSTDATE Function ........................................................................................................................................ 49 FIRSTNONBLANK Function ........................................................................................................................... 50 LASTDATE Function ......................................................................................................................................... 51 LASTNONBLANK Function ............................................................................................................................ 53 NEXTDAY Function........................................................................................................................................... 54 NEXTMONTH Function ................................................................................................................................... 55 NEXTQUARTER Function................................................................................................................................ 56 NEXTYEAR Function ......................................................................................................................................... 58 OPENINGBALANCEMONTH Function ....................................................................................................... 59 OPENINGBALANCEQUARTER Function.................................................................................................... 61 OPENINGBALANCEYEAR Function ............................................................................................................. 62 PARALLELPERIOD Function........................................................................................................................... 64 PREVIOUSDAY Function ................................................................................................................................. 66 PREVIOUSMONTH Function ......................................................................................................................... 67 PREVIOUSQUARTER Function ...................................................................................................................... 69 PREVIOUSYEAR Function ............................................................................................................................... 70 SAMEPERIODLASTYEAR Function .............................................................................................................. 71 STARTOFMONTH Function ........................................................................................................................... 73 STARTOFQUARTER Function ........................................................................................................................ 74 STARTOFYEAR Function ................................................................................................................................. 75 TOTALMTD Function ....................................................................................................................................... 76 TOTALQTD Function ........................................................................................................................................ 78 TOTALYTD Function......................................................................................................................................... 79 DATE Function ....................................................................................................................................................... 81 DATEVALUE Function .......................................................................................................................................... 85 DAY Function ......................................................................................................................................................... 86 EDATE Function ..................................................................................................................................................... 87 EOMONTH Function............................................................................................................................................ 89 HOUR Function ..................................................................................................................................................... 90 MINUTE Function ................................................................................................................................................. 91 MONTH Function ................................................................................................................................................. 92 NOW Function ....................................................................................................................................................... 94 SECOND Function ................................................................................................................................................ 94 TIME Function ........................................................................................................................................................ 96 TIMEVALUE Function........................................................................................................................................... 98 TODAY Function ................................................................................................................................................... 99 WEEKDAY Function .............................................................................................................................................. 99 WEEKNUM Function .......................................................................................................................................... 101 YEAR Function ..................................................................................................................................................... 103 YEARFRAC Function ........................................................................................................................................... 104 Filter Functions ........................................................................................................................................................ 106 ALL Function ......................................................................................................................................................... 107 ALLEXCEPT Function.......................................................................................................................................... 112 ALLNOBLANKROW Function.......................................................................................................................... 114 ALLSELECTED Function ..................................................................................................................................... 118 CALCULATE Function ........................................................................................................................................ 126 CALCULATETABLE Function ............................................................................................................................ 128 DISTINCT Function ............................................................................................................................................. 129 EARLIER Function................................................................................................................................................ 131 EARLIEST Function ............................................................................................................................................. 134 FILTER Function ................................................................................................................................................... 135 FILTERS Function................................................................................................................................................. 137 HASONEFILTER Function ................................................................................................................................. 138 HASONEVALUE Function ................................................................................................................................. 139 ISCROSSFILTERED Function ............................................................................................................................ 141 ISFILTERED Function .......................................................................................................................................... 145 KEEPFILTERS Function ....................................................................................................................................... 148 RELATED Function .............................................................................................................................................. 151 RELATEDTABLE Function ................................................................................................................................. 154 USERELATIONSHIP Function .......................................................................................................................... 155 VALUES Function ................................................................................................................................................ 157 Information Functions .......................................................................................................................................... 159 CONTAINS Function .......................................................................................................................................... 160 CUSTOMDATA Function .................................................................................................................................. 161 ISBLANK Function............................................................................................................................................... 161 ISERROR Function .............................................................................................................................................. 162 ISLOGICAL Function .......................................................................................................................................... 163 ISNONTEXT Function ........................................................................................................................................ 164 ISNUMBER Function .......................................................................................................................................... 165 ISTEXT Function................................................................................................................................................... 166 LOOKUPVALUE Function ................................................................................................................................. 167 PATH Function ..................................................................................................................................................... 168 PATHCONTAINS Function ............................................................................................................................... 170 PATHITEM Function ........................................................................................................................................... 171 PATHITEMREVERSE Function ......................................................................................................................... 172 PATHLENGTH Function .................................................................................................................................... 174 USERNAME Function......................................................................................................................................... 174 Logical Functions .................................................................................................................................................... 175 AND Function ...................................................................................................................................................... 176 FALSE Function .................................................................................................................................................... 179 IF Function............................................................................................................................................................. 180 IFERROR Function............................................................................................................................................... 181 NOT Function ....................................................................................................................................................... 182 OR Function .......................................................................................................................................................... 183 SWITCH Function................................................................................................................................................ 186 TRUE Function ..................................................................................................................................................... 187 Math and Trig Functions ..................................................................................................................................... 188 ABS Function ........................................................................................................................................................ 189 CEILING Function ................................................................................................................................................ 190 CURRENCY Function ......................................................................................................................................... 192 EXP Function ........................................................................................................................................................ 193 FACT Function...................................................................................................................................................... 194 FLOOR Function .................................................................................................................................................. 195 INT Function ......................................................................................................................................................... 196 ISO.CEILING Function........................................................................................................................................ 197 LN Function .......................................................................................................................................................... 199 LOG Function ....................................................................................................................................................... 199 LOG10 Function .................................................................................................................................................. 200 MOD Function ..................................................................................................................................................... 201 MROUND Function ............................................................................................................................................ 202 PI Function ............................................................................................................................................................ 203 POWER Function ................................................................................................................................................. 204 QUOTIENT Function .......................................................................................................................................... 205 RAND Function .................................................................................................................................................... 206 RANDBETWEEN Function ................................................................................................................................ 207 ROUND Function ................................................................................................................................................ 207 ROUNDDOWN Function .................................................................................................................................. 209 ROUNDUP Function .......................................................................................................................................... 210 SIGN Function ...................................................................................................................................................... 212 SQRT Function ..................................................................................................................................................... 213 SUM Function ...................................................................................................................................................... 213 SUMX Function .................................................................................................................................................... 214 TRUNC Function ................................................................................................................................................. 215 Statistical Functions ............................................................................................................................................... 216 ADDCOLUMNS Function ................................................................................................................................. 218 AVERAGE Function ............................................................................................................................................. 219 AVERAGEA Function .......................................................................................................................................... 221 AVERAGEX Function .......................................................................................................................................... 222 COUNT Function ................................................................................................................................................. 223 COUNTA Function .............................................................................................................................................. 224 COUNTAX Function ........................................................................................................................................... 225 COUNTBLANK Function ................................................................................................................................... 226 COUNTROWS Function .................................................................................................................................... 227 COUNTX Function .............................................................................................................................................. 229 CROSSJOIN Function ....................................................................................................................................... 230 DISTINCTCOUNT Function.............................................................................................................................. 232 GENERATE Function ......................................................................................................................................... 233 GENERATEALL Function ................................................................................................................................... 236 MAX Function ...................................................................................................................................................... 239 MAXA Function ................................................................................................................................................... 240 MAXX Function.................................................................................................................................................... 241 MIN Function ....................................................................................................................................................... 242 MINA Function .................................................................................................................................................... 243 MINX Function..................................................................................................................................................... 245 RANK.EQ Function ............................................................................................................................................ 246 RANKX Function.................................................................................................................................................. 248 ROW Function...................................................................................................................................................... 251 STDEV.S Function ............................................................................................................................................... 252 STDEV.P Function ............................................................................................................................................... 253 STDEVX.S Function ............................................................................................................................................. 254 STDEVX.P Function............................................................................................................................................. 255 SUMMARIZE Function ...................................................................................................................................... 256 TOPN Function .................................................................................................................................................... 263 VAR.S Function .................................................................................................................................................... 265 VAR.P Function .................................................................................................................................................... 266 VARX.S Function ................................................................................................................................................. 267 VARX.P Function ................................................................................................................................................. 268 Text Functions.......................................................................................................................................................... 270 BLANK Function .................................................................................................................................................. 271 CONCATENATE Function ................................................................................................................................. 272 EXACT Function ................................................................................................................................................... 274 FIND Function ...................................................................................................................................................... 275 FIXED Function .................................................................................................................................................... 276 FORMAT Function .............................................................................................................................................. 278 Pre-Defined Numeric Formats for the FORMAT Function .............................................................. 279 Custom Numeric Formats for the FORMAT Function ....................................................................... 281 Pre-defined Date and Time formats for the FORMAT Function ................................................... 287 Custom Date and Time formats for the FORMAT Function ........................................................... 287 LEFT Function ....................................................................................................................................................... 291 LEN Function ........................................................................................................................................................ 292 LOWER Function ................................................................................................................................................. 293 MID Function ....................................................................................................................................................... 294 REPLACE Function .............................................................................................................................................. 295 REPT Function ...................................................................................................................................................... 296 RIGHT Function ................................................................................................................................................... 298 SEARCH Function ............................................................................................................................................... 299 SUBSTITUTE Function ....................................................................................................................................... 301 TRIM Function...................................................................................................................................................... 302 UPPER Function ................................................................................................................................................... 303 VALUE Function................................................................................................................................................... 304 Formula Compatibility in DirectQuery Mode .................................................................................................. 305 Data Analysis Expressions (DAX) Reference The Data Analysis Expressions (DAX) language is a library of functions and operators that can be combined to build formulas and expressions. This section provides topics that describe function syntax and other attributes of the DAX language. For an overview of how you can use DAX formulas, see Getting Started with Data Analysis Expressions (DAX). In this Section DAX Syntax Specification Operator Reference Function Reference See Also Key Concepts in DAX Data Types Supported in PowerPivot Workbooks DAX Syntax Specification for PowerPivot Data Analysis Expressions (DAX) is a library of functions, operators, and constants that can be combined to build formulas and expressions in PowerPivot for Excel. This section provides details about the syntax and requirements of the DAX language. For examples of the kinds of formulas that you can build, and how you can use expressions to filter tables and change context, see Getting Started with Data Analysis Expressions (DAX). This topic contains the following sections: Syntax Requirements Naming Requirements Functions Operators and Constants Data Types Syntax Requirements DAX formulas are very similar to the formulas you type in Excel tables, but there are some key differences. • In Microsoft Excel you can reference individual cells or arrays; in PowerPivot you can reference only complete tables or columns of data. However, If you need to work 8 with only part of a column, or with unique values from a column, you can achieve similar behavior by using DAX functions that filter the column or return unique values. • DAX formulas do not support exactly the same data types as Microsoft Excel. In general, DAX provides more data types than Excel does, and DAX performs implicit type conversions on some data when importing. For more information, see Data Types in DAX. A DAX formula always starts with an equal sign (=). After the equals sign, you can provide any expression that evaluates to a scalar, or an expression that can be converted to a scalar. These include the following: • A scalar constant, or expression that uses a scalar operator (+,-,*,/,>=,...,&&, ...) • References to columns or tables. The DAX language always uses tables and columns as inputs to functions, never an array or arbitrary set of values. • Operators, constants, and values provided as part of an expression. • The result of a function and its required arguments. Some DAX functions return a table instead of a scalar, and must be wrapped in a function that evaluates the table and returns a scalar; unless the table is a single column, single row table, then it is treated as a scalar value. Most PowerPivot functions require one or more arguments, which can include tables, columns, expressions, and values. However, some functions, such as PI, do not require any arguments, but always require parentheses to indicate the null argument. For example, you must always type PI(), not PI. You can also nest functions within other functions. • Expressions. An expression can contain any or all of the following: operators, constants, or references to columns. For example, the following are all valid formulas. Formula Result =3 3 ="Sales" Sales ='Sales'[Amount] If you use this formula within the Sales table, you will get the value of the column Amount in the Sales table for the current row. =(0.03 *[Amount]) Three percent of the value in the Amount column of the current table. =0.03 * [Amount] Although this formula can be used to calculate a percentage, the result is not 9
Description: