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
Anonymous
Not applicable

Reference a measure or write directly into your dax code?

If you had asked me this morning whether there would be any difference between writing a DAX statement in a separate measure and referencing it to directly into your DAX code, I would have told you there wasnt.  I would have been wrong. 

 

To showcase this, I was writing a measure today with the purpose of returning the Average of the total Revenue of each month.  Here is the first formula i tried:

Average Revenue = IF(
	COUNTROWS(VALUES('Monthly Sales'[YearMonth])) = 1, 
	SUM('Monthly Sales'[TOTALS]), 
	AVERAGEX(
		VALUES('Monthly Sales'[YearMonth]), 
		SUM('Monthly Sales'[TOTALS])
	)
)

This does not provide the correct answer.  So, i took the SUM portion of the function and made it a measure.  This gave me:

Revenue = SUM('Monthly Sales'[TOTALS])
Average Revenue = IF(
	COUNTROWS(VALUES('Monthly Sales'[YearMonth])) = 1, 
	[Revenue], 
	AVERAGEX(
		VALUES('Monthly Sales'[YearMonth]), 
		[Revenue]
	)
)

This change now gives me the correct answer.

 

I thought it was a good topic to highlight and share.

1 REPLY 1
v-huizhn-msft
Employee
Employee

Hi @Anonymous,

It's really a good idea, thanks for sharing it.

Best Regards,
Angelia

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.