cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Frequent Visitor

Calculate the average

Hi,

I want to create a DAX formula that calculates the average price of product category.

I have the following SALES table, I want DAX to sum the prices for each category and divide it by the number of category present in my dataset.

Mathematically, it will be like this:

Sum of Clothing = 47

Sum of Electronics = 1300

Sum of Home = 250

Average by Category = 532.33

If want to filter from another table for one category the sum is the same as the average because it will divide by one.

Thank you

2 ACCEPTED SOLUTIONS
Super User

Try something like:

``````AVERAGEX (
VALUES ( SALES[Category] ),
CALCULATE ( SUM ( SALES[Price] ) )
)``````

VALUES gives a distinct list of categories.

AVERAGEX then iterates over this list of categories and calculates the sum of prices for each.

You need the CALCULATE statement there to turn the row context that you're on into a filter context on the table. If you leave it out it will just return the total price for the whole table.

Once it has a sum for each category averagex divides by the number of categories.

Ben Dobbs

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Helper I

Hi,

1. Create a measure with below Dax

Distinct Category = DISTINCTCOUNT('Table'[Category])
2. Create one more measure for calculating Average
Average Price by category = sum('Table'[Price])/[Distinct Category]

I have attached solution in sample file and uploaded on Sharepoint

Please confirm if the solution works..!!

2 REPLIES 2
Helper I

Hi,

1. Create a measure with below Dax

Distinct Category = DISTINCTCOUNT('Table'[Category])
2. Create one more measure for calculating Average
Average Price by category = sum('Table'[Price])/[Distinct Category]

I have attached solution in sample file and uploaded on Sharepoint

Please confirm if the solution works..!!

Super User

Try something like:

``````AVERAGEX (
VALUES ( SALES[Category] ),
CALCULATE ( SUM ( SALES[Price] ) )
)``````

VALUES gives a distinct list of categories.

AVERAGEX then iterates over this list of categories and calculates the sum of prices for each.

You need the CALCULATE statement there to turn the row context that you're on into a filter context on the table. If you leave it out it will just return the total price for the whole table.

Once it has a sum for each category averagex divides by the number of categories.

Ben Dobbs

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Announcements

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors