DAX Calculate and Values Functions

CALCULATE Function

In Power BI, the CALCULATE is a fundamental and very useful function. This function is used to evaluate an expression by applying filters, conditions and any calculation based on the context.

Syntax:

CALCULATE(Expression, [Filter1], ...)

Example:

CalculateDEMO = CALCULATE(SUM('Global-Superstore'[Sales]),'Global-Superstore'[Country] = "United States")

Example:

CalculateFiltersDemo = CALCULATE(SUM('Global-Superstore'[Sales]),
'Global-Superstore'[Country] = "United States" ||  
'Global-Superstore'[Country] = "Germany")

CALCULATE function

VALUES Function:

In DAX for Power BI, the VALUES function is used to retrieve distinct values from a column.

Syntax:

VALUES(TableORColumnName)

Example:

Number of cities = COUNTROWS(VALUES(City[CityName]))

Using the FILTER Function in DAX

The Power BI DAX, the FILTER function is used to get data from a table or an expression based on conditions applied. It is generally used in combination with other functions.

Syntax:

FILTER(Table, Filter OR Condition) 

Example:

FilterDAX1 = Calculate(Sum(Forecast[Forecast]),FILTER(Forecast, Forecast[Country]="USA" || Forecast[Country]="Italy"))

Variable for Debugging

In DAX variables can be used to store and reuse value stored in the variable within a formula. The variables in a DAX formula make it more readable, easier to maintain and helps in reusing values store in variables.

Below is the Syntax of how a variable is used:

VAR variable_name = expression

RETURN result_expression

The keyword ‘VAR’ indicates the beginning of the variable declaration.

The keyword ‘RETURN’ indicates the beginning of the expression that will return the result.

Below is an example of using variables:

DiscountOffered = VAR SaleQuantity = SUM('Order Data'[Cookies Shipped]) 
VAR CostOfCookies=SUM('Order Data'[Cost]) 
Return IF(SaleQuantity > 800, CostOfCookies * 10/100, SaleQuantity * 5 / 100)

The ‘DiscountOffered’ is a measure created with 2 variables to calculate the discount based on no of cookies shipped using an if condition.

The below is the output

Creating measures using DAX

DAX (Data Analysis Expressions) Measures in Power BI are standard aggregation functions or dynamic custom expressions defined with the DAX formula language. Using this formula language, you can calculate, combine, and filter data that can return one or more values. DAX Measures are dynamic and user-defined formulas where results change depending on context. This article discusses the DAX Measures in Power BI, how to create and edit, and how to use them in Power BI Visualization reports.

Similar Reads

What is DAX?

DAX, the Data Analysis Expressions is a powerful formula expression language that includes functions, operators, and constants to perform simple to advanced calculations and queries on data from different data sources to return values. DAX helps to perform complex calculations and analyze data trends in real-world business scenarios....

Creating Measures Using AggregateX Functions

There are many AggregateX functions for creating DAX measures in Power BI. These functions are used to aggregate values over a table or table expression by performing row-wise calculations. Below are the AggregateX functions available:...

Creating Calculated Columns Using DAX

Calculated columns are new columns created in an existing data table using a DAX Formula. This will be part of the table and can be used in Power BI visualization reports....

DAX Calculate and Values Functions

CALCULATE Function...

DAX Studio

DAX Studio is a tool to write, execute, and analyze DAX queries in Power BI. It provides a set of great features to help developers to create, test and optimize DAX formulas. DAX Studio has a Query Editor and option to execute and view results. DAX Studio is integrated with PowerBI and it connects directly with PowerBI mnodels and allows to work seemlessly with PowerBI datasets....

Conclusion

DAX Measures can be used to create calculations using wide range of functions for statistical, mathematical and date-time calculations. Using DAX measures in Power BI, interactive and dynamic reports can be created with Power BI visuals, which support advanced data analytics and business intelligence to gain deeper insights from their data....