Languages Advisor | DAX

AVERAGEX Function in DAX

Learn how to use the AVERAGEX function in DAX to compute the average of an expression evaluated for each row in a table.


Empty image or helper icon

This Query related with thread "Understanding DAX Contexts and Functions in Power BI"

Prompt

In the given SUMX example, a weighted average price was calculated. Now, continue the exploration by writing a DAX expression using the AVERAGEX function to calculate the average profits for each product sold. Assume your table ('Sales Table') has additional column 'Profit'.

Answer

In DAX, the AVERAGEX function is used to compute the average of an expression evaluated for each row in a table. In your case, we can use the AVERAGEX function to calculate the average profit for each product sold. The syntax would be as follows:

AverageProfit = AVERAGEX('Sales Table', 'Sales Table'[Profit])

In this DAX expression, 'Sales Table' is the name of the table where the average calculation is performed, and 'Sales Table'[Profit] is the expression for which to compute the average.

This code will yield the average profit of the sales by taking the profit for each row in the 'Sales Table' and then computing the average.

To calculate average profit per product, you can use a similar formula, but you first need to iterate over a distinct list of products rather than the whole 'Sales Table'. That way, the averaged will be done per product instead:

AverageProfitPerProduct = AVERAGEX(
  VALUES('Sales Table'[Product]), 
  CALCULATE(SUM('Sales Table'[Profit]))
)

Here we are using a combination of the AVERAGEX, VALUES, and SUM functions.

  • We obtain a distinct list of products with VALUES('Sales Table'[Product]).
  • Then for each product, we are using CALCULATE(SUM('Sales Table'[Profit])) to summarize the profit.
  • Finally, we average these totals.

Remember that in DAX, the iteration functions like AVERAGEX process each row context in isolation, and you often need to use CALCULATE to modify the filter context. If you want to learn more about these concepts and functions, consider exploring courses offered on the Enterprise DNA platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

The AVERAGEX function in DAX is useful for calculating the average of a specific expression for each row in a table. By specifying the table name and the expression to evaluate, you can easily determine the average value. In the provided DAX code examples, the AVERAGEX function is used to calculate the average profit for each product sold. The first example calculates the overall average profit for the entire 'Sales Table', while the second example calculates the average profit per product. It is important to note that in DAX, iteration functions like AVERAGEX operate on individual row contexts, and the CALCULATE function is often necessary to modify the filter context. Further understanding of these concepts and functions can be gained by exploring courses offered on the Enterprise DNA platform.