Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
RafalK
Advocate IV
Advocate IV

Two-level aggregation using DAX measures

Hi guys,

I need to calculate an average quantity per product and then per total, based on the product average.

I can make it work in DAX just as long as I only aggregate the data once. I just can't figure out how to aggregate to one level first and then aggregate that again.

 

I have this table as an input:

ProductDayQuantity
P1Day 110
P1Day 212
P1Day 320
P2Day 15
P2Day 27
P2Day 312
P3Day 123
P3Day 230
P3Day 335

 

Now I need to first calculate "Avg Quantity":

ProductAvg Quantity
P114,00
P28,00
P329,33

 

And then, based on that the "Avg of Averages"

Avg of Average
17,11

 

My first aproach was to simply create new tables with aggregates and this does work, however I loose the option to filter the data by Day, using a slicer.

 

For those with SQL backgroupd. I need to do this:

 

select
AVG(AvgQuantity) "AvgofAverages"
from (
   select
     Product,

     AVG(Quantity) "AvgQuantity" 
   where Day = <selection> 
   group by Product
) X

 

Regards

Rafał Kun
1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @RafalK

 

You can use nested AVERAGEX functions to get an average of averages.

 

I'm assuming that if you happen to have multiple rows per Product per Day, you want the Quantity to be summed at a Product/Day level.

(If you can guarantee you will never have multiple rows per Product per Day, you could simplify the below a bit).

 

Either of these should work:

 

 

Average of Averages v1 = 
AVERAGEX (
    VALUES ( YourTable[Product] ),
    AVERAGEX (
        VALUES ( YourTable[Day] ),
        CALCULATE ( SUM ( YourTable[Quantity] ) )
    )
)

Average of Averages v2 =
AVERAGEX (
VALUES ( YourTable[Product] ),
CALCULATE (
AVERAGEX (
VALUES ( YourTable[Day] ),
CALCULATE ( SUM ( YourTable[Quantity] ) )
)
)
)

 

The second measure avoids redundant iteration over values of the Day column for each Product, with the additional CALCULATE. This could perform better if each Product sells on different sets of Days.

 

Regards,

Owen 🙂

 

 

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

6 REPLIES 6
afzalphatan
Resolver I
Resolver I

Jus check... If P1 is not sold in DAY 1 .. then there are 2 ways to enter in ur data table...

 

1. To mention value of Quantity as Zero in Day1 

2. There is no transaciton in data table of P1 and Day1 ( As no quantity was sold... there wouldn't be any transaction )

 

Whats ur pattern.. check if the solution works

OwenAuger
Super User
Super User

Hi @RafalK

 

You can use nested AVERAGEX functions to get an average of averages.

 

I'm assuming that if you happen to have multiple rows per Product per Day, you want the Quantity to be summed at a Product/Day level.

(If you can guarantee you will never have multiple rows per Product per Day, you could simplify the below a bit).

 

Either of these should work:

 

 

Average of Averages v1 = 
AVERAGEX (
    VALUES ( YourTable[Product] ),
    AVERAGEX (
        VALUES ( YourTable[Day] ),
        CALCULATE ( SUM ( YourTable[Quantity] ) )
    )
)

Average of Averages v2 =
AVERAGEX (
VALUES ( YourTable[Product] ),
CALCULATE (
AVERAGEX (
VALUES ( YourTable[Day] ),
CALCULATE ( SUM ( YourTable[Quantity] ) )
)
)
)

 

The second measure avoids redundant iteration over values of the Day column for each Product, with the additional CALCULATE. This could perform better if each Product sells on different sets of Days.

 

Regards,

Owen 🙂

 

 

 

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Hi Owen,

 

I have a similar case were I have 12 months of data and need to have the average of the first 6 months and the second 6 months to compare changes period vs period. Any ideas?

 

thanks!

This is a good example of aggregating across multiple tables

Thanks! 

This is exactly what I was looking for 🙂

Rafał Kun
Dog
Responsive Resident
Responsive Resident

Hi,

 

I think...... the visuals aggregation functions will take care of this. 

 

create a standard averge mearsure on the quantity. 

 

Capture.PNG

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.