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

Dax formula to calculate product blending

I have two tables, Sales and Recipe. The recipe table has percentages of each ingredient needed to make the products and it could take up to three different ingredients to make a certain product. The sales table has the pounds sold of each product. I need a DAX formula to look at the sales amount for each product and calculate the total pounds of each ingredient needed to make the products sold.

 

Recipe_Table     
Prod CodeINGRED_1INGRED 1%INGRED_2INGRED 2%INGRED_3INGRED 3%
A134%865%91%
B251%339%910%
C380%520%  
D465%133%92%
E251%449%  
F251%549%  
G742%842%616%
       
Sales_Table  Dax formula to  
Prod CodeTot Lbs calculate Ingred needed 
A100000 IngredIngred Needed 
B121000 150500  
C98000 2116280  
D50000 3125590  
E65000 464350  
F42000 540180  
G200000 632000  
Total676000 784000  
   8149000  
   914100  
   Total676000  
3 ACCEPTED SOLUTIONS
MFelix
Super User
Super User

HI @Anonymous ,

 

Your Recipe table should have the following format:

Prod CodeINGRED%

A 1 0,34
B 2 0,51
C 3 0,8
D 4 0,65
E 2 0,51
F 2 0,51
G 7 0,42
A 8 0,65
B 3 0,39
C 5 0,2
D 1 0,33
E 4 0,49
F 5 0,49
G 8 0,42
A 9 0,01
B 9 0,1
D 9 0,02
G 6 0,16

 

Then make a relationship between recipe and sales and add the following measure:

ingridient quantity = SUMX(Recipe;RELATED(Sales_Table[TOT LBS])*Recipe[%])

Check the PBIX with the transformation of the recipe table and the result of the measure I post above.

 

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

d_gosbell
Super User
Super User

So if you pivot your recipe table out to look like the following you can do this with the measure below

2019-03 nested sumx.png

 

Required Product = SUMX(Sales, 
var _TotLbs = Sales[Tot Lbs]
var _ProdCode = Sales[Prod Code]
return SUMX( CALCULATETABLE(Recipe, TREATAS({_ProdCode}, Recipe[Prod Code]))
, Recipe[INGRED %] * _TotLbs))

 

 

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

 

You may download my PBI file from here.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

 

You may download my PBI file from here.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
d_gosbell
Super User
Super User

So if you pivot your recipe table out to look like the following you can do this with the measure below

2019-03 nested sumx.png

 

Required Product = SUMX(Sales, 
var _TotLbs = Sales[Tot Lbs]
var _ProdCode = Sales[Prod Code]
return SUMX( CALCULATETABLE(Recipe, TREATAS({_ProdCode}, Recipe[Prod Code]))
, Recipe[INGRED %] * _TotLbs))

 

 

Anonymous
Not applicable

Thank you d_gosbell for your solution. All the replies would have worked but I like the flexibility yours provided.

 

Thanks to all that responded.

Note that the solution by @MFelix is a simpler approach to mine, but it does assume that your Sales table only has unique product code, If your Sales table will have other fields (eg. if you record the amount sold by date) then you will not be able to use a relationship and the related() function and will probably need to look at something like my suggestion. 

MFelix
Super User
Super User

HI @Anonymous ,

 

Your Recipe table should have the following format:

Prod CodeINGRED%

A 1 0,34
B 2 0,51
C 3 0,8
D 4 0,65
E 2 0,51
F 2 0,51
G 7 0,42
A 8 0,65
B 3 0,39
C 5 0,2
D 1 0,33
E 4 0,49
F 5 0,49
G 8 0,42
A 9 0,01
B 9 0,1
D 9 0,02
G 6 0,16

 

Then make a relationship between recipe and sales and add the following measure:

ingridient quantity = SUMX(Recipe;RELATED(Sales_Table[TOT LBS])*Recipe[%])

Check the PBIX with the transformation of the recipe table and the result of the measure I post above.

 

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.