Commonly used Power BI DAX

DS - VRP
7 min readSep 17, 2024

--

DAX (Data Analysis Expressions) is a formula language used in Microsoft Power BI, Excel Power Pivot, and SQL Server Analysis Services (SSAS) to define custom calculations and perform complex data analysis on data models. It is designed to work with relational data and provides a range of functions for creating measures, calculated columns, and tables, enabling advanced analytical capabilities on top of your datasets.

Key Features of DAX:

  1. Calculations: DAX allows users to create custom calculations such as sums, averages, or more complex aggregations (like running totals or year-over-year comparisons).
  2. Row Context & Filter Context: DAX handles row-based calculations (row context) and can also apply filters dynamically (filter context) when performing aggregations.
  3. Time Intelligence: DAX has a set of functions specifically designed for working with dates and times, which allow for period-based calculations like year-to-date (YTD), month-over-month growth, and comparisons with previous periods.

Key Concepts in DAX:

  • Calculated Columns: These are new columns you create using DAX formulas. They are added to your data model to derive new data from existing columns.
  • Measures: Measures are dynamic aggregations, typically used in reporting visuals in Power BI. Unlike calculated columns, measures are computed only when needed.
  • Tables: DAX can create new tables using expressions, providing flexibility to manage data sets.

The most commonly used Power BI DAX (Data Analysis Expressions) formulas along with real-time use cases and step-by-step explanations.

1. CALCULATE
The `CALCULATE` function is one of the most powerful and commonly used functions in DAX. It evaluates an expression in a modified filter context.

Use Case:
You want to calculate the total sales only for the year 2023, despite having multiple years of sales data.

Formula:
DAX
Total Sales 2023 = CALCULATE(SUM(Sales[SalesAmount]), Sales[Year] = 2023)
Explanation:
- SUM(Sales[SalesAmount]): This part sums up the `SalesAmount` column from the `Sales` table.
- Sales[Year] = 2023: This is a filter that restricts the calculation to rows where the `Year` is 2023.
- CALCULATE modifies the filter context so that only data for the year 2023 is included in the sum.

2. SUM
The `SUM` function is used to calculate the total of a column.

Use Case:
You want to calculate the total sales across all records in the dataset.

Formula:
DAX
Total Sales = SUM(Sales[SalesAmount])

Explanation:
- SUM(Sales[SalesAmount]): This sums all values in the `SalesAmount` column in the `Sales` table.

3. SUMX

Use Case:

`SUMX` iterates through a table, evaluates an expression for each row, and then sums the results.

Formula:

DAX

Total Sales = SUMX(Sales, Sales[Quantity] * Sales[Price])

Explanation:

`SUMX` goes row by row and calculates the product of `Quantity` and `Price` for each row, and then sums the results. This is helpful when you need a calculated measure before performing aggregation.

Real-Time Use Case:

Suppose you have separate columns for `Quantity` and `Price` and want to calculate total revenue, where revenue = `Quantity` * `Price`. This function helps you compute total revenue for all transactions.

4. AVERAGE
The `AVERAGE` function calculates the average value of a numeric column.

Use Case:
You want to find out the average order amount across all orders.

Formula:
DAX
Average Order Amount = AVERAGE(Sales[OrderAmount])

Explanation:
- AVERAGE(Sales[OrderAmount]): This takes the mean value of the `OrderAmount` column from the `Sales` table.

5. AVERAGEX : Computes the average of an expression for each row in a table.

Formula: Average Sales = AVERAGE(Sales[SalesAmount])

Explanation: This formula calculates the mean of all sales values in the `SalesAmount` column.

Real-Time Use Case:For example, a manager might need to know the average sales per transaction to analyze customer purchasing behavior or to set sales benchmarks.

6. COUNT / COUNTA / COUNTX

Use Case:- COUNT: Counts the number of rows in a column containing numerical values.

- COUNTA: Counts the number of rows in a column, including text, numbers, or non-blank values.

- COUNTX: Counts rows in a table based on an expression.

Formula:Total Orders = COUNT(Sales[OrderID])

NonBlankValues = COUNTA(Sales[CustomerName])

Explanation:

- COUNT counts the number of rows with numerical values in the OrderID column.

- COUNTA counts the rows where `CustomerName` is not blank.

Real-Time Use Case: Track the total number of orders or count non-empty customer names in your dataset. This is essential for business intelligence where you’re counting transactions, active customers, etc.

7. FILTER
The `FILTER` function returns a table with rows that satisfy a specific condition. This is particularly useful when you need to filter down datasets within other functions.

Use Case:
You want to calculate the total sales where the order quantity is greater than 100.

Formula:
High Quantity Sales = CALCULATE(SUM(Sales[SalesAmount]), FILTER(Sales, Sales[OrderQuantity] > 100))
Explanation:
- FILTER(Sales, Sales[OrderQuantity] > 100): This filters the `Sales` table to include only rows where `OrderQuantity` is greater than 100.
- CALCULATE(SUM(Sales[SalesAmount]): This calculates the total sales amount for the filtered data.

8.RELATED
The `RELATED` function retrieves related values from another table. This works well with relationships between tables.

Use Case:
You want to pull in the `Product Category` from the `Product` table into a sales report that uses the `Sales` table.

Formula:
DAX
Product Category in Sales = RELATED(Product[Category])

Explanation:
-RELATED(Product[Category]): This fetches the related `Category` from the `Product` table based on the relationship with the `Sales` table.

9. IF
The `IF` function returns one value if a condition is TRUE and another value if it’s FALSE.

Use Case:
You want to classify sales as “High” if the amount is greater than $500, otherwise, classify them as “Low”.

Formula:
DAX
Sales Classification = IF(Sales[SalesAmount] > 500, “High”, “Low”)
Explanation:
- IF(Sales[SalesAmount] > 500, “High”, “Low”): If `SalesAmount` is greater than 500, it returns “High”, otherwise it returns “Low”.

10. COUNTROWS
The `COUNTROWS` function returns the number of rows in a table.

Use Case:You want to count how many orders were made in the year 2023.

Formula:
DAX
Order Count 2023 = COUNTROWS(FILTER(Sales, Sales[Year] = 2023))
Explanation:
- FILTER(Sales, Sales[Year] = 2023): Filters the sales table to include only rows where the year is 2023.
- COUNTROWS(…): Counts the number of rows in the filtered table.

11.DISTINCTCOUNT
The `DISTINCTCOUNT` function counts the number of unique values in a column.

*Use Case: You want to count how many unique customers made purchases.

Formula:

DAX
Unique Customers = DISTINCTCOUNT(Sales[CustomerID])
Explanation:
- DISTINCTCOUNT(Sales[CustomerID]): This counts the distinct (unique) values in the `CustomerID` column of the `Sales` table.

12.DIVIDE
The `DIVIDE` function performs division and handles division by zero gracefully.

Use Case: You want to calculate the profit margin by dividing profit by revenue, but you want to avoid errors when revenue is zero.

Formula:
DAX
Profit Margin = DIVIDE(Sales[Profit], Sales[Revenue], 0)
Explanation:
- DIVIDE(Sales[Profit], Sales[Revenue], 0): This divides profit by revenue, and if revenue is zero, it returns 0 instead of an error.

13. DATESYTD
The `DATESYTD` function is used to calculate year-to-date values.

Use Case: You want to calculate the total sales from the start of the year to the current date.

Formula:
DAX
Sales YTD = CALCULATE(SUM(Sales[SalesAmount]), DATESYTD(Sales[OrderDate]))

Explanation:
- DATESYTD(Sales[OrderDate]): This function returns the dates from the start of the year to the current date.
- CALCULATE(SUM(Sales[SalesAmount]), …): This calculates the total sales within the year-to-date range.

14.ALL
The `ALL` function removes any filters from a column or table, often used for calculating percentages or totals.

*Use Case: You want to calculate the percentage of total sales for each product.

Formula:
DAX
Sales Percentage = DIVIDE(SUM(Sales[SalesAmount]), CALCULATE(SUM(Sales[SalesAmount]), ALL(Sales)))
Explanation:
- SUM(Sales[SalesAmount]): This calculates the total sales amount for the current context.
-CALCULATE(SUM(Sales[SalesAmount]), ALL(Sales)): This calculates the total sales without any filters.
-DIVIDE(…, …): This divides the individual sales amount by the total sales to get the percentage.

15.RANKX
The `RANKX` function is used to assign a ranking to each value in a column, based on an expression.

Use Case:
You want to rank products based on their total sales.

Formula:
DAX
Product Rank = RANKX(ALL(Sales[ProductName]), SUM(Sales[SalesAmount]))

Explanation:
- RANKX(ALL(Sales[ProductName]), SUM(Sales[SalesAmount])): This ranks the products based on the total sales, without considering any filters on `ProductName`.

16. MAX / MIN

Use Case:

- `MAX`: Returns the maximum value in a column.

- `MIN`: Returns the minimum value in a column.

Formula:

DAX

Max Order Value = MAX(Sales[OrderValue])

Min Order Value = MIN(Sales[OrderValue])

Explanation:

This formula extracts the highest and lowest values from the `OrderValue` column, which can indicate the range of transactions.

Real-Time Use Case:

Find out the largest and smallest sales order values in a sales dataset to identify high-value transactions or small, potentially risky orders.

17. ALL / REMOVEFILTERS

Use Case:

`ALL` and `REMOVEFILTERS` remove filters from a calculation.

Formula:

DAX

Total Sales All Regions = CALCULATE(SUM(Sales[SalesAmount]), ALL(Sales[Region]))

Explanation:

This formula ignores any filters applied to the `Region` column and calculates total sales across all regions.

Real-Time Use Case:

This is useful when comparing filtered data (e.g., sales for a specific region) against unfiltered data (total sales). It can help analyze contribution margins or compare segments.

18. LOOKUPVALUE

Use Case:

Returns the value of a column from another table based on a condition.

Formula:

DAX

Lookup Price = LOOKUPVALUE(Products[Price], Products[ProductID], Sales[ProductID])

Explanation:

This formula retrieves the `Price` from the `Products` table where `ProductID` in the `Products` table matches the `ProductID` in the `Sales` table.

Real-Time Use Case:

For cases where no relationships exist between two tables, `LOOKUPVALUE` is useful to fetch values based on specific criteria, such as pulling pricing details for a sales report.

19. IF / SWITCH

Description:

- `IF` performs logical checks and returns different values based on the result.

- `SWITCH` evaluates an expression against multiple values and returns different results.

Formula (IF):

DAX

HighValueOrder = IF(Sales[OrderAmount] > 1000, “High”, “Low”)

Formula (SWITCH):

DAX

RegionGroup = SWITCH(Sales[Region], “East”, “Group 1”, “West”, “Group 2”, “Other”)

Explanation:

- `IF` checks whether the order value exceeds 1000, and labels the order as `High` or `Low`.

- `SWITCH` categorizes the regions into groups based on the `Region` column.

Real-Time Use Case:

You can use `IF` to categorize data (e.g., flagging high-value orders) and `SWITCH` for multi-condition checks like assigning labels or grouping regions.

20. Time Intelligence Function: For time Intelligence Functions in Power Bi and Excel please click here.

Conclusion:
These DAX formulas are fundamental to creating powerful and interactive reports in Power BI. By understanding these, you can manipulate data, apply filters, calculate complex metrics, and generate meaningful insights for better decision-making.

Please comments for improvements, for more contant connect over LinkedIn

--

--

DS - VRP
DS - VRP

Written by DS - VRP

An aspiring data scientist on a journey of continuous learning and discovery—turning curiosity into insights and challenges into opportunities to innovate

No responses yet