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
jabrouni1
Frequent Visitor

Weighted average of columns in different tables

Hi,

 

I have a table called Products and another table called Sales. They are related by productId. I need to find the weighted average for a selected list of products in Products table. The formula is (Product1.UnitCost * Sales[ProductId1].ItemsSold + Product2.UnitCost * Sales[ProductId2].ItemsSold) + So on/ Total sum of items sold of the chos products. How can i derive the DAX formula for this?

 

Sample data

 

Products
			
ProductId   |	Name  |	Description   |	UnitItemCost
------------|---------|---------------|----------------
id1	    |	Name1 |	Description1  |	10
id2	    |	Name2 |	Description2  |	20
id3	    |	Name3 |	Description3  |	30
			
			
Sales	
		
ProductId  |	ItemsSold
-----------|--------------		1714.126984
Id1	   |	20		
id2	   |	30		
id1	   |	10		
id2        |    40		
id3	   |	50		
id3	   |	39		

Average unit cost = 1714.126984    (10*30+20*70+30*89)/189
1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @jabrouni1

Average unit cost = 1714.126984    (10*30+20*70+30*89)/189

It is werid, the result of this formula should be 23, is anything missing here?

 

If the result of this formula should be 23, I have a solution as below.

1. Create relationships between two tables

Products(one) to Sales(many), Cross filter direction is Both.

2. In Products table, create columns

Sales.ItemsSold = CALCULATE(SUM(Sales[ItemsSold]),ALLEXCEPT(Products,Products[ProductId]))

sum = [UnitItemCost]*[Sales.ItemsSold]

total = SUM(Sales[ItemsSold])

weighted average = SUM(Products[sum])/[total]

6.png

 

Best Regards

Maggie

View solution in original post

4 REPLIES 4
v-juanli-msft
Community Support
Community Support

Hi @jabrouni1

Average unit cost = 1714.126984    (10*30+20*70+30*89)/189

It is werid, the result of this formula should be 23, is anything missing here?

 

If the result of this formula should be 23, I have a solution as below.

1. Create relationships between two tables

Products(one) to Sales(many), Cross filter direction is Both.

2. In Products table, create columns

Sales.ItemsSold = CALCULATE(SUM(Sales[ItemsSold]),ALLEXCEPT(Products,Products[ProductId]))

sum = [UnitItemCost]*[Sales.ItemsSold]

total = SUM(Sales[ItemsSold])

weighted average = SUM(Products[sum])/[total]

6.png

 

Best Regards

Maggie

v-juanli-msft
Community Support
Community Support

Hi @jabrouni1

Could you share an example data?

 

The formula is (Product1.UnitCost * Sales[ProductId1].ItemsSold + Product2.UnitCost * Sales[ProductId2].ItemsSold) 

Do "Product1.UnitCost" and "Sales[ProductId1].ItemsSold" refer to columns in  Sales table?

 

find the weighted average for a selected list of products in Products table

Do you mean when you put column "products" from Products table in a slicer, then select some products, the measure should calculate weighted average for these selected products?

Also, I can't see your formula have any relation with weighted average.

 

Best Regards

Maggie

 

Hello,

 

I have added sample data

jabrouni1
Frequent Visitor

Hi,

 

I have a table called Products and another table called Sales. They are related by productId. I need to find the weighted average for a selected list of products in Products table. The formula is (Product1.UnitCost * Sales[ProductId1].ItemsSold + Product2.UnitCost * Sales[ProductId2].ItemsSold) + So on/ Total sum of items sold of the chos products. How can i derive the DAX formula for this?

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.