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

SUM value by category

 I am trying to take the following data and calculate the values for each person.

 

personsalesAverage
Jerry3010
Jerry3010
Jerry3010
Tom2512.5
Tom2512.5
James2010
James2010
Joe2525

 

I am looking to take the total sales for each person and create a new column (average) that will average the total sales and break it out for each line. Can someone explain how this can be done? Thank you in advance. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

 

I don't agree that this is the right way to do this, using measures you can calculate this information display it without needing to store the value on every single line in your data set (which is what a calculated column will do).  Never the less, this is the calculated column code to do what you are asking:

This will produce your calculated column for sales

Total Sales = var thisPerson = [person]
RETURN
CALCULATE(
	SUM('YourTable'[sales])
	ALL('YourTable'),
	'YourTable'[person] = thisPerson
)


This calculates the average

Total Average = var thisPerson = [person]
RETURN
DIVIDE(
	[Total Sales],
	CALCULATE(
		COUNTROWS('YourTable')
		ALL('YourTable'),
		'YourTable'[person] = thisPerson
	)	
)

View solution in original post

4 REPLIES 4
v-danhe-msft
Employee
Employee

Hi @Anonymous,

Could you please post me your desired result?

 

Regards,

Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

 

I don't agree that this is the right way to do this, using measures you can calculate this information display it without needing to store the value on every single line in your data set (which is what a calculated column will do).  Never the less, this is the calculated column code to do what you are asking:

This will produce your calculated column for sales

Total Sales = var thisPerson = [person]
RETURN
CALCULATE(
	SUM('YourTable'[sales])
	ALL('YourTable'),
	'YourTable'[person] = thisPerson
)


This calculates the average

Total Average = var thisPerson = [person]
RETURN
DIVIDE(
	[Total Sales],
	CALCULATE(
		COUNTROWS('YourTable')
		ALL('YourTable'),
		'YourTable'[person] = thisPerson
	)	
)

Hi Ross

What would be your solution using a measure? I have difficulties finding a clear answer on how to use CALCULATE + any sort of function (SUM, AVERAGE etc.) for each different value in another column.

 

Example: different locations (cities) and I want the SUM of the electricity usage for each location.

Anonymous
Not applicable

Hi @rbrabec   Measures are all about the context they are used.  When you write a measure, you want to keep your mind on how you will be using the measure.

 

For example, in this problem, where we want to calculate total sales for the given person, we might expect that we will be using the measure on a table, with 1 row per person.  In that case, its unlikely we would need to use a calculate statement, as the context would do all of the work.

 

When you want to use Calculate with Sum, average etc, the easiest way to do it is to create a measure that handles the Sum/Average/Whatever, then in your 2nd measure you call it using your calculate statement.


i.e:

Sum Measure = SUM('YourTable'[YourColumn])

 

Calculate Measure =  CALCULATE(
      [Sum Measure],
      'YourTable'[OtherColumn] = "Other Value"
)

 

A Filter statement is often used in Calculate, but it doesn't have to be.  The above is a fairly simplisitic use case, but hopefully gives you a starting point.

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.