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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
ngaray
Helper I
Helper I

Using variables and HASONEVALUE() to calculate average in a matrix

Hello everyone,

 

I need to create a matrix that calculates the average of values instead of row total. I see this is a common issue in this forum. I was able to find other posts that got me close to what I need, but I am still getting the wrong result.

 

In my example, I have a table that shows daily hours worked by employees (the lowest granularity is days). In the matrix, I would like to aggregate the hours by week. However, instead of calculating a grand total, I would like to calculate the average.

 

This is a snapshot of the raw data

 Raw data example.PNG

 

This is how the matrix looks (with the incorrect "total"). The averages should be (7+15)/2 = 11 and (15+30)/2 = 22.5. Instead of 5.63 and 11.25

Incorrect total.png

 

The DAX measure that I am using is 

 

 

 

 

Average Weekly Hours =
VAR AllWeeksAverage =
    AVERAGEX ( 'Employee Hours', AVERAGE ( 'Employee Hours'[Reported Hours] ) )
RETURN
    IF (
        HASONEVALUE ( 'Employee Hours'[Week of Year] ),
        SUM ( 'Employee Hours'[Reported Hours] ),
        AllWeeksAverage
    )

 

 

 

 

 

I believe variable AllWeeksAverage is what is causing the error. Something important to point out is that there can be weeks were an employee will not report any hours. In my example, John Doe didn't report any hours in week 3.

 

I am attaching the pbix file here

 

I appreciate your assistance,

 

Nathalia

1 ACCEPTED SOLUTION
FrankAT
Community Champion
Community Champion

Hi @ngaray,

try the following measure:

 

09-07-_2020_00-22-19.jpg

 

Regards FrankAT

View solution in original post

2 REPLIES 2
FrankAT
Community Champion
Community Champion

Hi @ngaray,

try the following measure:

 

09-07-_2020_00-22-19.jpg

 

Regards FrankAT

Hi @FrankAT 

 

Your measure worked perfectly!

I only modified the aggregation in the true portion of the If condition from Average to Sum. I should have clarified that I want to see the total hours by week not the average.

I can see that my problem was that I didn't filter the table by employee.

average in a matrix.PNG

 

Thank you for your assistance, it is very much appreciated!

 

Nathalia 

 

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors