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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.