DAX Formulas and Statements in Power BI
DAX Formulas and Statements in Power BI
DAX Cheat Sheet • DATEADD(<dates>, <number_of_intervals>, <interval>) Moves a date by a specific • CALENDAR (<start_date>, <end_date>) Returns a table with a single column named
interval. "Date" that contains a contiguous set of dates.
Math & statistical functions • DATESBETWEEN (<dates>, <date_1>, <date_2>) Returns the dates between specified • DATE (<year>, <month>, <day>) Returns the specified date in datetime format.
• SUM(<column>) Adds all the numbers in a column. dates.
• DATEDIFF (<date_1>, <date_2>, <interval>) Returns the number of units between two
• SUMX (<table>, <expression>) Returns the sum of an expression evaluated for each row • TOTALYTD(<expression>,<dates>[,<filter>][,<year_end_date>]) Evaluates the dates as defined in <interval>.
year-to-date value of the expression in the current context.
in a table. • DATEVALUE(<date_text>) Converts a date in text to a date in datetime format.
• AVERAGE(<column>) Returns the average (arithmetic mean) of all the numbers in a • SAMEPERIODLASTYEAR (<dates>) Returns a table that contains a column of dates
shifted one year back in time. • DAY(<date>) Returns a number from 1 to 31 representing the day of the month.
column.
• STARTOFMONTH(<dates>) // ENDOFMONTH(<dates>) Returns the start // end of • WEEKNUM (<date>) Returns weeknumber in the year.
• AVERAGEX(<column>, <expression>) Calculates the average (arithmetic mean) of a set
the month. • MONTH(<date>) Returns a number from 1 to 12 representing a month.
of expressions evaluated over a table.
• MEDIAN (<column>) Returns the median of a column. • STARTOFQUARTER (<dates>) // ENDOFQUARTER (<dates>) Returns the start // • QUARTER (<date>) Returns a number from 1 to 4 representing a quarter.
end of the quarter.
• MEDIANX (<table>, <expression>) Calculates the median of a set of expressions
evaluated over a table.
• STARTOFYEAR (<dates>) // ENDOFYEAR (<dates>) Returns the start // end of the Information functions
quarter.
• COUNT(<column>) Returns the number of cells in a column that contain non-blank values. • COLUMNSTATISTICS () Returns statistics regarding every column in every table. This
• LASTDATE(<dates>) Returns the last date in the current context for the specified column
function has no arguments.
• COUNTX(<table>, <expression>) Counts the number of rows from an expression that of dates.
evaluates to a non-blank value. • NAMEOF(<value>) Returns the column or measure name of a value.
• DATESINPERIOD(<dates>, <start_date>, <number_of_intervals>, <interval>)
• DIVIDE (<numerator>, <denominator> [,<alternateresult>]) Performs division and Returns a table that contains a column of dates that begins with a specified start date and • ISBLANK (<value>) // ISERROR (<value>) Returns whether the value is blank // an
returns alternate result or BLANK() on division by 0. continues for the specified number and type of date intervals. error.
• MIN(<column>) Returns a minimum value of a column. • ISLOGICAL (<value>) Checks whether a value is logical or not.
• MAX(<column>) Returns a maximum value of a column. Relationship functions • ISNUMBER(<value>) Checks whether a value is a number or not.
• COUNTROWS ([<table>]) Counts the number of rows in a table. • CROSSFILTER(<left_column>, <right_column>, <crossfiltertype>) Specifies the • ISFILTERED (<table> | <column>) Returns true when there are direct filters on a column.
cross-filtering direction to be used in a calculation.
• DISTINCTCOUNT(<column>) Counts the number of distinct values in a column. • ISCROSSFILTERED (<table> | <column>) Returns true when there are crossfilters on a
• RELATED (<column>) Returns a related value from another table. column.
• RANKX(<table>, <expression> [, <value>[, <order>[, <ties>]]]) Returns the ranking
of a number in a list of numbers for each row in the table argument. • USERPRINCIPALNAME() Returns the user principal name or email address. This function
has no arguments.
Table manipulation functions
Filter functions
• SUMMARIZE (<table>, <groupBy_columnName>[, <groupBy_columnName>]... DAX statements
• FILTER(<table>, <filter>) Returns a table that is a subset of another table or expression. [, <name>, <expression>]...) Returns a summary table for the requested totals over a set
of groups. • USERPRINCIPALNAME() Returns the user principal name or email address. This function
• CALCULATE(<expression> [, <filter1> [, <filter2> [,...]]]) Evaluates an expression in
has no arguments.
a filter context.
• DISTINCT(<table>) Returns a table by removing duplicate rows from another table or
expression. • COLUMN (<table> [<column>] = <expression>) Stores the result of an expression as a
• HASONEVALUE (<columnName>) Returns TRUE when the context for columnName has
column in a table.
been filtered down to one distinct value only. Otherwise it is FALSE.
• ADDCOLUMNS (<table>, <name>, <expression> [, <name>, <expression>]...)
Adds calculated columns to the given table or table expression. • ORDER BY (<table>[<column>]) Defines the sort order of a column. Every column can be
• VALUES(<TableNameOrColumnName>) Returns a one-column table that contains the
distinct values from the specified column sorted in ascending (ASC) or descending (DESC) way.
• SELECTCOLUMNS(<Table>, [<Name>], <Expression>, <Name>], …) Selects
• ALLNORANKROW(<table> | <column> [, <column> [, <column> [,…]]]) Returns a calculated columns from the given table or table expression.
table that is a subset of another table or expression.
• GROUPBY(<table> [, <groupBy_columnName>[, [<column_name>]
DAX Operators
• ALL([<table> | <column> [, <column> [, <column> [,...]]]]) Returns all the rows in a [<expression>1]]...) Create a summary of the input table grouped by specific columns.
table, or all the values in a column, ignoring any filters that might have been applied. Comparison operators Meaning
• ALLEXCEPT(<table>, <column> [, <column> [,..]]) Returns all the rows in a table = Equal to
except for those rows that are affected by the specified column filters. Text functions == Strict equal to
• ALLSELECTED([<tableName> | <columnName>[, <columnName>[, • EXACT(<text_1>, <text_2>) Checks if two strings are identical (EXACT() is case sensitive). > Greater than
<columnName>[,…]]]] ) Returns all the values in a column, or all the rows in a table while • FIND(<text_tofind>, <in_text>) Returns the starting position a text within another text
ignoring all the filters that might have been applied inside the query and keeping the filters < Smaller than
(FIND() is case sensitive).
that might have come from the outside. >= Greater than or equal to
• FORMAT ( <value>, <format>) Converts a value to a text in the specified number format.
=< Smaller than or equal to
Logical functions • LEFT(<text>, <num_chars>) Returns the number of characters from the start of a string.
<> Not equal to
• IF(<logical_test>, <value_if_true>[, <value_if_false>]) Checks a condition and • RIGHT(<text>, <num_chars>) Returns the number of characters from the end of a string.
returns a certain value depending on whether it is true or false. • LEN(<text>) Returns the number of characters in a string of text. Text operator Meaning Example
• AND (<logical 1>, <logical 2>) Checks whether both arguments are TRUE and returns • LOWER(<text>) Converts all letters in a string to lowercase. Concatenates text Concatenates text values |
TRUE if both arguments are TRUE. Otherwise, it returns FALSE. &
values [City]&”,”&[State]
• UPPER (<text>) Converts all letters in a string to uppercase.
• OR(<logical 1>, <logical_2>) Checks whether one of the arguments is TRUE to return
TRUE. The function returns FALSE if both arguments are FALSE. • TRIM(<text>) Remove all spaces from a text string. Logical operator Meaning Example
• NOT(<logical>) Changes TRUE to FALSE and vice versa. • CONCATENATE (<text_1>, <text_2>) Joins two strings together into one string. && AND condition ([City] = “Bru”) && ([Return] = “Yes”))
• SWITCH(<expression>, <value>, <result>[, <value>, <result>]..[, <else>]) • SUBSTITUTE (<text>, <old_text>, <new_text>, <instance_num>) Replaces existing || OR condition ([City] = “Bru”) || ([Return] = “Yes”))
Evaluates an expression against a list of values and returns one of possible results. text with new text in a string.
OR condition for
• IFERROR(<value>, <value_if_error>) Returns value_if_error if the first expression is an • REPLACE(<old_text>, <start_posotion>, <num_chars>, <new_text>) Replaces IN {} Product[Color] IN {“Red”, “Blue”, “Gold”}
each row
error and the value of the expression itself otherwise. part of a string with a new string.