Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

DAX (Data Analysis Expression):

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.

Understanding Key DAX Functions in Power BI

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.

SUMX works on multiple columns, It iterates over a table, performing row-by-row


calculations.

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

10. ALLEXCEPT Function:


ALLEXCEPT removes filters form all the columns except those mentioned. It does not
remove filters from the columns which are mentioned in the formula.
It contains two arguments table name and column name.
Most of the time it is used as a calculate modifier.

Syntax:

ALLEXCEPT (
TableName,
TableName[ColumnName]
)

11. ALL and ALLSELECTED Function:


ALL function removes all the filters. It does not respect any outer filter or inner filter.
All is used when you want to calculate a grand total, average or aggregate measures.

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

12. SELECTCOLUMNS Function:


SELECTCOLUMNS is used to create a new table by selecting specific columns form an
existing table.
This is powerful for creating customized tables and can be used un various scenarios.

Syntax:

SELECTCOLUMNS (
TableName,
"NewColumnName1", TableName[ExistingColumnName1],
"NewColumnName2", TableName[ExistingColumnName2],
...
)

13. ADDCOLUMNS Function:


ADDCOLUMNS function adds calculated columns to an existing table or table
expression.
It does not remove any existing columns; it simply appends new columns based on the
provided DAX expressions.

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, ... }, ... }
)

15. TABLE Constructor:


The Table Constructor is used to create a table of one or more columns without
specifying column names but allows expression.

Syntax:

{ (Value1, Value2, ...), (Value1, Value2, ...) }

16. UNION and INTERSECTION Functions:


UNION returns all the rows from each of the table (virtual table or filter table).
In UNINON arguments must have same number of columns.
Duplicate rows are retained.

INTERSECTION returns common rows from both the tables.


Duplicate row is retained.

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

18. RANKX Function:


RANKX returns the rank of a number in a list of numbers for each row in the table.

Syntax:

RANKX (
TableName,
Expression,
[Value],
[Order],
[Ties]
)

19. SWITCH Function:


SWITCH evaluate an expression against a list of values and returns one of multiple
possible result expressions.

Syntax:

SWITCH (
TRUE(),
Condition1, Result1,
Condition2, Result2,
...
DefaultResult
)

You might also like