Professional Documents
Culture Documents
DAX Functions
DAX Functions
DAX:
DAX is a formula language which is used in Power BI, Power Pivot to define custom
calculations and queries on data models. DAX is designed to work with data in tables
and columns, making it suitable for creating complex expression and transformation.
DAX works with columns and tables, allows you to create calculated columns,
measures, and tables. It provides filters, sort, and manipulate tables.
1. Expanded Tables:
Expanded tables are a conceptual tool that help explain how DAX calculations
operate over relationship in the data model.
Expanded tables are not physical tables stored in memory but a way to visualize
how DAX handles relationships and context programs.
When we create relationships between tables in Power BI, DAX can
automatically expand tables to include columns from related tables.
In a one-to-many relationship, the tables present on the 'many' sides are
applicable for expanded tables.
Scenario:
Suppose you want to calculate the total sales amount by product category.
Without expanded tables, you would need to explicitly join the Sales, Products,
and Categories tables. With expanded tables, DAX handles this automatically.
2. Calculated Columns:
Calculated columns are stored in memory and their values are derived using DAX
functions. Thay are useful for complex calculations performed row-wise.
Scenario:
Adding a calculated column to show the year of a sale.
3. Measures:
Measures are used for defining calculations that do not take up memory as they are
calculated on the fly. Measures use filter context and do not require the calculate
functions.
Scenario:
Creating a measure to calculate a total sales.
4. Calculate Functions:
The Calculate function is one of the most powerful and versatile functions in Data
Analysis Expressions (DAX) in Power BI. It is used to modify the context in which a data
calculation is performed.
It allows you to add additional filters to the data before performing the calculation.
In filter of calculation function, If the column name are same then it will overwrite. It will
retain the same values.
Scenario:
Calculating total sales for a specific product category.
Syntax:
CALCULATE (
Expression,
Filter1,
Filter2, …)
5. KEEPFILTERS Function:
The KEEPFILTERS function ensures that filters applied to a calculation are preserved.
This function is particularly useful when you want to enforce certain filters within a
calculation, even if other filters are applied elsewhere in the report.
If a filter condition involves two columns and you do not use the KEEPFILTERS function,
the filters will overwrite each other. However, if you use KEEPFILTERS, both filters will
be preserved, providing the correct result.
Scenario:
Ensuring all filters are respected when calculating sales.
Syntax:
CALCULATE (
Expression,
KEEPFILTERS(Filter)
)
6. REMOVEFILTERS Function:
REMOVEFILTERS is used to remove all filter from tables and columns.
It works as a calculate modifier.
Scenario:
Calculating total sales without any category filters.
Syntax:
CALCULATE (
Expression,
REMOVEFILTERS(TableName[ColumnName]))
7. SUM and SUMX:
SUM function adds up all the values in a single column. It can accept only one column
as a argument.
Syntax:
SUM (TableName[ColumnName])
SUMX (
TableName,
Expression
)
8. DISTINCT Function:
DISTINCT returns single column table of unique values when column is passed as an
argument.
It returns unique combination of values when table expression is passed as an
argument.
Scenario:
Getting Unique product names.
Syntax:
DISTINCT (TableName[ColumnName])
9. VALUES Function:
VALUES return a single column table of unique values or the entire table with
duplication and blank rows.
Syntax:
VALUES (TableName[ColumnName])
Syntax:
ALLEXCEPT (
TableName,
TableName[ColumnName]
)
ALLSELECTED function removes inner filters and keep outer filter which are coming
through slicer.
It is used in calculate or table filed.
ALLSELECTED is used when you want to respect the slicers and filters applied on the
report page.
Syntax:
ALL (TableName[ColumnName])
ALLSELECTED (TableName[ColumnName])
Syntax:
SELECTCOLUMNS (
TableName,
"NewColumnName1", TableName[ExistingColumnName1],
"NewColumnName2", TableName[ExistingColumnName2],
...
)
Syntax:
ADDCOLUMNS (
TableName,
"NewColumnName1", Expression1,
"NewColumnName2", Expression2, ….)
14. DATATABLE Function:
The DATATABLE function is used to create a table with a defined set of data values and
a specific schema directly within a DAX expression.
You cannot use expression in data table.
Syntax:
DATATABLE (
ColumnName1, DataType1, ColumnName2, DataType2, ...,
{ { Value1, Value2, ... }, { Value1, Value2, ... }, ... }
)
Syntax:
Syntax:
UNION (
Table1,
Table2
)
INTERSECT (
Table1,
Table2
)
17. LOOKUPVALUE Function:
LOOKUPVALUE returns the value of a column in a table that meets specified criteria.
Syntax:
LOOKUPVALUE (
Result_ColumnName,
Search_ColumnName1, Search_Value1,
Search_ColumnName2, Search_Value2, …)
Syntax:
RANKX (
TableName,
Expression,
[Value],
[Order],
[Ties]
)
Syntax:
SWITCH (
TRUE(),
Condition1, Result1,
Condition2, Result2,
...
DefaultResult
)