DAX Expressions Notes
DAX Expressions Notes
Data Analysis Expressions (DAX) is a formula expression language used in Analysis Services, Power BI, and Power Pivot
in Excel. DAX formulas include functions, operators, and values to perform advanced calculations and queries on data in
related tables and columns in tabular data models.
Calculations
DAX formulas are used in measures, calculated columns, calculated tables, and row-level security.
Measures
Measures are dynamic calculation formulas where the results change depending on context. Measures are used in reporting
that support combining and filtering model data by using multiple attributes such as a Power BI report or Excel PivotTable
or PivotChart. Measures are created by using the DAX formula bar in the model designer.
A formula in a measure can use standard aggregation functions automatically created by using the Autosum feature, such
as COUNT or SUM, or you can define your formula by using the DAX formula bar. Named measures can be passed as an
argument to other measures. For example, using this very simple measure formula: Total Sales = SUM([Sales Amount])
Calculated columns
A calculated column is a column that you add to an existing table (in the model designer) and then create a DAX formula
that defines the column's values. When a calculated column contains a valid DAX formula, values are calculated for each
row as soon as the formula is entered. Values are then stored in the in-memory data model. For example, in a Date table,
when the formula is entered into the formula bar: = [Calendar Year] & " Q" & [Calendar Quarter]
Row-level security
With row-level security, a DAX formula must evaluate to a Boolean TRUE/FALSE condition, defining which rows can be
returned by the results of a query by members of a particular role. For example, for members of the Sales role, the
Customers table with the following DAX formula: = Customers[Country] = "USA"
1. Aggregation functions: Calculate scalar values like count, sum, average, min, or max for all rows in a column or
table.
2. Date and time functions: Similar to Excel's date and time functions but based on datetime data types used by
SQL Server.
3. Filter functions: Help return specific data types, look up values in related tables, and manipulate data context for
dynamic calculations.
4. Financial functions: Perform financial calculations like net present value and rate of return.
5. Information functions: Determine if values in a table or column match the expected type, such as ISERROR.
7. Math and Trig functions: Similar to Excel's math and trig functions, but with differences in numeric data types.
9. Parent and Child functions: Manage data presented in a parent/child hierarchy in data models.
11. Statistical functions: Calculate values related to statistical distributions and probability.
13. Text functions: Return part of a string, search for text, or concatenate string values. Also control formats for dates,
times, and numbers.
14. Time intelligence functions: Create calculations using built-in knowledge about calendars and dates for
meaningful comparisons across time periods.
Syntax: COUNT(<column>)
Syntax: SUM(<column>)
Syntax: AVERAGE(<column>)
Syntax: MIN(<column>)
Syntax: MAX(<column>)
Date and time functions: Similar to Excel's date and time functions but based on datetime data types used by SQL Server.
Syntax: TODAY()
Syntax: NOW()
3. DATE: Creates a date value from the specified year, month, and day.
Syntax: YEAR(<date>)
Syntax: MONTH(<date>)
Syntax: DAY(<date>)
7. DATEADD: Adds or subtracts a specified number of units (days, months, years) to a date.
10. CALENDARAUTO: Automatically generates a date table based on the data in the model.
Syntax: CALENDARAUTO()
Filter functions: Help return specific data types, look up values in related tables, and manipulate data context for dynamic
calculations.
1. FILTER: Returns a table that contains only the rows that satisfy the specified conditions.
Syntax: RELATED(<column>)
3. RELATEDTABLE: Returns a table related to the current table based on a specified relationship.
Syntax: RELATEDTABLE(<table>)
4. ALL: Removes all filters from a table or column, or from all columns except specified columns.
5. ALLEXCEPT: Removes all filters from a table except for those specified columns.
6. KEEPFILTERS: Preserves existing filters in the current context while evaluating a calculation.
Syntax: KEEPFILTERS(<expression>)
7. VALUES: Returns a single-column table of unique values from a column, considering only the rows that are visible
in the current context.
Syntax: VALUES(<column>)
8. SELECTCOLUMNS: Returns a table with selected columns from the specified table.
1. NPV: Calculates the net present value of an investment based on a series of cash flows.
2. IRR: Calculates the internal rate of return for a series of cash flows.
Syntax: IRR(<values>)
3. XNPV: Calculates the net present value of cash flows that are not necessarily periodic.
4. XIRR: Calculates the internal rate of return for cash flows that are not necessarily periodic.
5. FV: Calculates the future value of an investment based on periodic, constant payments and a constant interest rate.
6. PV: Calculates the present value of an investment based on periodic, constant payments and a constant interest
rate.
Information functions: Determine if values in a table or column match the expected type, such as ISERROR.
Syntax: ISERROR(<value>)
Syntax: ISBLANK(<value>)
Syntax: ISNUMBER(<value>)
Syntax: ISTEXT(<value>)
Syntax: ISLOGICAL(<value>)
7. HASONEVALUE: Checks whether a column has only one distinct value in the current context.
Syntax: HASONEVALUE(<column>)
8. SELECTEDVALUE: Returns the value if there is only one value in the specified column in the current context;
otherwise, returns blank.
Syntax: SELECTEDVALUE(<column>)
Syntax: USERNAME()
10. USERPRINCIPALNAME: Returns the user principal name (UPN) of the current user.
Syntax: USERPRINCIPALNAME()
Truth tables are used to represent the outputs of logical operations for all possible combinations of inputs. Here are the truth
tables for the basic logical operations AND, OR, and NOT:
In the AND operation, the output is TRUE only if both inputs are TRUE; otherwise, the output is FALSE.
In the OR operation, the output is TRUE if at least one input is TRUE; the output is FALSE only if both inputs are FALSE.
Input Output
FALSE TRUE
TRUE FALSE
In the NOT operation, the output is the opposite of the input. If the input is TRUE, the output is FALSE, and vice versa.
1. IF: Returns one value if a condition is TRUE and another value if it's FALSE.
2. AND: Returns TRUE if all arguments are TRUE, and FALSE otherwise.
4. NOT: Returns the opposite of a logical value - TRUE if the argument is FALSE, and FALSE if the argument is TRUE.
Syntax: NOT(<logical>)
Syntax: TRUE()
Syntax: FALSE()
7. SWITCH: Evaluates an expression against a list of values and returns the result corresponding to the first matching
value.
Math and Trig functions: Similar to Excel's math and trig functions, but with differences in numeric data types.
Syntax: ABS(<number>)
Syntax: EXP(<number>)
Syntax: LN(<number>)
Syntax: SQRT(<number>)
Syntax: SIN(<number>)
Syntax: COS(<number>)
Syntax: TAN(<number>)
Syntax: PI()
Syntax: BLANK()
2. SWITCH: Evaluates an expression against a list of values and returns the result corresponding to the first matching
value.
3. CONCATENATEX: Concatenates the result of an expression evaluated for each row in a table.
Syntax: RELATED(<column>)
5. SELECTEDVALUE: Returns the value if there is only one value in the specified column in the current context;
otherwise, returns blank.
Syntax: SELECTEDVALUE(<column>)
6. UNICHAR: Returns the Unicode character that corresponds to the specified numeric value.
Syntax: UNICHAR(<number>)
7. UNICODE: Returns the Unicode value of the first character of the text.
Syntax: UNICODE(<text>)
8. ROW: Returns a single row table that represents a row from a table.
10. DATATABLESELECTCOLUMNS: Creates an in-memory table by selecting columns from an existing table.
1. PATH: Returns a delimited text string that represents the path from the root node to a specified node in a hierarchy.
2. PATHCONTAINS: Checks whether a specified node is in the path of another node in a hierarchy.
3. PATHITEM: Returns the name of a node at a specified position in the path of another node in a hierarchy.
4. PATHLENGTH: Returns the number of levels in the path from the root node to a specified node in a hierarchy.
Syntax: PATHLENGTH(<path>)
Syntax: RELATEDHIERARCHY(<column>)
Syntax: ISEMPTY(<table_or_column>)
Syntax: ISCROSSFILTERED(<column>)
Syntax: HASONEFILTER(<column>)
1. RELATED: Retrieves a related value from another table based on a one-to-many or many-to-one relationship.
Syntax: RELATED(<column>)
2. RELATEDTABLE: Returns a table related to the current table based on a specified relationship.
Syntax: RELATEDTABLE(<table>)
Syntax: CROSSFILTER(<table1>[,<column1>],[<table2>,<column2>],<direction>)
6. HASONEVALUE: Checks whether a column has only one distinct value in the current filter context.
7. LOOKUPVALUE: Returns the value in a column that corresponds to the result of a calculation.
8. PATH: Returns a delimited text string that represents the path from the root node to a specified node in a hierarchy.
1. AVERAGEX: Calculates the average of an expression evaluated for each row in a table.
2. COUNTAX: Counts the number of rows in a table where the specified expression evaluates to a non-blank value.
3. MAXX: Returns the maximum value of an expression evaluated for each row in a table.
4. MINX: Returns the minimum value of an expression evaluated for each row in a table.
5. STDEV.P: Calculates the standard deviation based on the entire population given as arguments.
6. STDEV.S: Estimates the standard deviation based on a sample of the entire population.
7. VAR.P: Calculates the variance based on the entire population given as arguments.
9. MEDIANX: Calculates the median of an expression evaluated for each row in a table.
10. PERCENTILE.EXC: Returns the k-th percentile of values in a range, exclusive of 0 and 1.
1. FILTER: Returns a table that contains only the rows that satisfy the specified conditions.
2. SELECTCOLUMNS: Returns a new table with selected columns from the specified table.
3. ADDCOLUMNS: Returns a table with new columns added, calculated from existing columns.
5. GROUPBY: Groups the rows of a table based on the values of one or more columns and then performs a calculation
on each group.
6. DISTINCT: Returns a table with unique rows based on the specified columns.
Syntax: DISTINCT(<table>)
8. EXCEPT: Returns all the rows from one table that are not present in another table.
9. INTERSECT: Returns all the rows that are common to two tables.
10. DATATABLE: Creates an in-memory table with the specified columns and values.
4. MID: Returns a specific number of characters from a text string, starting at a specified position.
Syntax: LEN(<text>)
Syntax: LOWER(<text>)
Syntax: UPPER(<text>)
Syntax: TRIM(<text>)
9. FIND: Returns the starting position of one text string within another text string.
10. SUBSTITUTE: Replaces occurrences of a specified text string within another text string with a new text string.
12. FORMATDATETIME: Formats a datetime value based on the specified format string.
1. TOTALYTD: Calculates the year-to-date total for a given expression, up to the specified date.
2. TOTALMTD: Calculates the month-to-date total for a given expression, up to the specified date.
3. TOTALQTD: Calculates the quarter-to-date total for a given expression, up to the specified date.
4. DATESYTD: Returns a table of dates from the start of the year up to the specified date.
Syntax: DATESYTD(<dates>)
5. DATESMTD: Returns a table of dates from the start of the month up to the specified date.
Syntax: DATESMTD(<dates>)
6. DATESQTD: Returns a table of dates from the start of the quarter up to the specified date.
Syntax: DATESQTD(<dates>)
8. SAMEPERIODLASTYEAR: Returns a table of dates for the same time period in the previous year.
Syntax: SAMEPERIODLASTYEAR(<dates>)
Syntax: PREVIOUSYEAR(<dates>)