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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
jmcph
Helper III
Helper III

Averaging Per Category

Hi, 

 

I am doing some averaging computations, and my computations seems to be quite off. I just want to ask for correction on why my measure doesnt seem to work. My data are as follows : 

 

On my Masterlist table:
Borrower                         Attribute                            Date                               Value 

ABC                                  Loan                                   1/1/2019                        1000

ABC                                  Collection                           1/5/2019                        300
ABC                                  Loan                                   2/5/2019                        3000

My measures were as follows : 

AverageLoan =
Var
    SumLoan = Calculate (
Sum( Masterlist[Value] ), Masterlist[Attribute] = "Loan Amount")

Return
    AVERAGEX (
All( 'Calendar'[Date]) , SumLoan )

What i am after is to compute for the Average on a per Borrower basis. My slicer includes both Months and Years.  
 
When i entered the abovementioned measure it shows me a result of 4,000 (for the year 2019) which i expect to return a value of 2,000. Please enlighten me on what did i do wrong. 

Thank you very much for any help that you may provide! Cheers! 

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Hi, @jmcph , the cause of problem lies in leveraging a variable within iteration.

In fact, variable in DAX is NOT always variable, refer to:

https://radacad.com/caution-when-using-variables-in-dax-and-power-bi

 

 

In DAX, variables are calculated within the scope in which they are written, and then the result of them is stored and used in the rest of the expression.

 

To be more precise with your issue, when AVERAGEX iterates All( 'Calendar'[Date]) (being "1/1", "1/5", "2/5"), SumLoan is used 3 times with the same value, which is calculated on iterating "1/1". Furthermore, SumLoan is define in the outmost scope of the measure, only initial filter context instead of "1/1", such as slicer, row/column of viz etc., is used in the calculation.

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

Hi, @jmcph , the cause of problem lies in leveraging a variable within iteration.

In fact, variable in DAX is NOT always variable, refer to:

https://radacad.com/caution-when-using-variables-in-dax-and-power-bi

 

 

In DAX, variables are calculated within the scope in which they are written, and then the result of them is stored and used in the rest of the expression.

 

To be more precise with your issue, when AVERAGEX iterates All( 'Calendar'[Date]) (being "1/1", "1/5", "2/5"), SumLoan is used 3 times with the same value, which is calculated on iterating "1/1". Furthermore, SumLoan is define in the outmost scope of the measure, only initial filter context instead of "1/1", such as slicer, row/column of viz etc., is used in the calculation.

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

vanessafvg
Super User
Super User

I think you might be overcomplicating it

AverageLoan = CALCULATE(AVERAGE(MasterList[Value]), MasterList[Attribute] = "Loan")




If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




Yeah. Thanks for the input. I think i'll do normal average calculation first, then make another measure filtering the dates to make it simpler. I appreciate it thanks! 

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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