Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
ModelFear
Frequent Visitor

Need help with DAX formula

Dear community, 

 

I post this question as a fresh question if it is ok. 

 

Below is my Sales table of various dishes :

 

Sales.jpg

 

Below is my table called Recipe, where Subtotal is the cost to make each Dish:

Recipe.jpg

 

Below is my model. I created a bridge table to avoid many to many relationship of Sales and Recipe tables:

Model.jpg

 

I am trying to calculate margin for each dish using formula of (Selling price - Subtotal/Selling price). But obviously newbie here cannot DAX very well. 

Thank you in advance for your kind help!

 

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
halfglassdarkly
Responsive Resident
Responsive Resident

Note LOOKUPVALUE wouldn't work here either because there are multiple records per [Dish ID] in the Recipe table, so some form of aggregation is required. I've opted to sum 'Recipe'[Cost] but you could also use:

 

Margin = 

VAR RecipeCost = Calculate(Max('Recipe'[Subtotal]),

'Recipe'[Dish ID]=EARLIER('Sales'[Dish ID]))

RETURN

'Sales'[Selling Price per kg] - RecipeCost

 

Also note calculations like this as well as the Subtotal field in 'Recipe' would be good candidates for creating measures rather than calculated columns.

View solution in original post

5 REPLIES 5
halfglassdarkly
Responsive Resident
Responsive Resident

Note LOOKUPVALUE wouldn't work here either because there are multiple records per [Dish ID] in the Recipe table, so some form of aggregation is required. I've opted to sum 'Recipe'[Cost] but you could also use:

 

Margin = 

VAR RecipeCost = Calculate(Max('Recipe'[Subtotal]),

'Recipe'[Dish ID]=EARLIER('Sales'[Dish ID]))

RETURN

'Sales'[Selling Price per kg] - RecipeCost

 

Also note calculations like this as well as the Subtotal field in 'Recipe' would be good candidates for creating measures rather than calculated columns.

Thank you so so so so much halfglassdarkly. I have been working on this table as a simple manufacturing model to take on harder manufacturing model. I cannot say how little we all know about dax formulas etc even after attending power bi courses. I tried tried to use chatgpt etc etc to no avail. This just shows that coding, programming is still important as the nuance of each model requirement is different. Thank you and so grateful. Yours sincerely, Modelfear (Lin) 

halfglassdarkly
Responsive Resident
Responsive Resident

As Manvishah17 has said, Related isn't going to work here. Try using something like:

 

Margin = 

VAR RecipeCost = Calculate(Sum('Recipe'[Cost]),

'Recipe'[Dish ID]=EARLIER('Sales'[Dish ID]))

RETURN

'Sales'[Selling Price per kg] - RecipeCost

danextian
Super User
Super User

Hi @ModelFear ,

 

The name of the fact tables are covered.  That aside, you can't use RELATED with the way your relationships are set up. RELATED works on the many side of a one to many or many to one relationship.  Both Sales and Recipe are on the many side and relationships do not flow from of any these tables to the other. They're only bridged by Bridge DishID. RELATED will not function either if used on a many to many relationship -  between Sales and Recipe, out of the many rows for say Dish ID 3001 in Recipe, which one should Sales pick? Try using LOOKUPVALUE instead.










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
manvishah17
Responsive Resident
Responsive Resident

Hi @ModelFear , 
RELATED Dax can be use when there is direct relation btw two tables.

Refer this for more information.

https://learn.microsoft.com/en-us/dax/related-function-dax 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.