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
davidinternship
New Member

Average calculation in same column but for different values

Hello everyone,

I'm trying to calculate the average of some different values in the same column. And after that, using that result to divide another value in the same column, resulting a value in porcetage.

 

The example of the table and the equation are shown below:

tablebi.PNG


For ID CODE 41:            X =[ 10 / (20+5/2) ] - 1
                                               |        |    |
TYPE-------------->               A       B   C -> media between B and C


I really appreciate for the help.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

// Of course, your notation is incorrect.
// The formula should read:
//
// 		X = [2A / (B + C)] - 1
//
// In mathematics IT DOES MATTER where you put
// brackets. Note that such a calculation should
// be performed in Power Query, not in DAX.

[New Column] = // column, not a measure
var __code = T[ID Code]
var __a =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "A"
		),
		T[Value]
	)
var __b =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "B"
		),
		T[Value]
	)
var __c =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "C"
		),
		T[Value]
	)
return
	DIVIDE(
		2 * __a,
		__b + __c
	) - 1

// If you want a calculated table,
// you can do this:

[Calc Table] =
ADDCOLUMNS(
	VALUES( T[ID Code] ),
	"X",
		var __code = T[ID Code]
		var __a =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "A"
				),
				T[Value]
			)
		var __b =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "B"
				),
				T[Value]
			)
		var __c =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "C"
				),
				T[Value]
			)
		return
			DIVIDE(
				2 * __a,
				__b + __c
			) - 1
)

Best

D

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

// Of course, your notation is incorrect.
// The formula should read:
//
// 		X = [2A / (B + C)] - 1
//
// In mathematics IT DOES MATTER where you put
// brackets. Note that such a calculation should
// be performed in Power Query, not in DAX.

[New Column] = // column, not a measure
var __code = T[ID Code]
var __a =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "A"
		),
		T[Value]
	)
var __b =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "B"
		),
		T[Value]
	)
var __c =
	MAXX(
		filter(
			T,
			T[ID Code] = __code
			&&
			T[Type] = "C"
		),
		T[Value]
	)
return
	DIVIDE(
		2 * __a,
		__b + __c
	) - 1

// If you want a calculated table,
// you can do this:

[Calc Table] =
ADDCOLUMNS(
	VALUES( T[ID Code] ),
	"X",
		var __code = T[ID Code]
		var __a =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "A"
				),
				T[Value]
			)
		var __b =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "B"
				),
				T[Value]
			)
		var __c =
			MAXX(
				filter(
					T,
					T[ID Code] = __code
					&&
					T[Type] = "C"
				),
				T[Value]
			)
		return
			DIVIDE(
				2 * __a,
				__b + __c
			) - 1
)

Best

D

Thank you for the answer. It helpde me a lot.

I just needed to change little things to adapt on my need, but your answer was fundamental. 

Best wishes!

Greg_Deckler
Super User
Super User

Not sure I follow this, but if you want the average of each ID CODE, for example, you could do this:

 

New Column = AVERAGEX(FILTER('Table',[ID CODE] = EARLIER([ID CODE])),[Value])


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors