Our company is in the F&B business where cost of many sold items changes frequently. Thus, we get to have -on a monthly basis- a different cost for every SKU sold (this is recorded on a table w/columns Month, Year, SKU, UnitCost imported in BI).
I need to calculate cost for sold products but I cannot get to ‘assign’ the right cost (year/month) to the each sales line. Sales table in BI consists of Date/SKU/Qty.
Please check attached solution if it is helpful. If not, please let me know. I have created below measure to derive the cost.
Cost New =
SUMX (
sales,
Sales[Quantity]
*
VAR salesdate = Sales[Date]
VAR MaxCostDate =
MAXX (
CALCULATETABLE('Food Cost',all(dates), 'Food Cost'[Date] <= salesdate),
'Food Cost'[Date]
)
VAR MinCostDate =
CALCULATE(min('Food Cost'[Date]),all(dates))
Var FinalDate = if(isblank(MaxCostDate),MinCostDate,MaxCostDate)
RETURN
CALCULATE (
max ( 'Food Cost'[Cost] ),
all(dates), 'Food Cost'[Date] = FinalDate)
)
Sumx - This will perform operation for each row of the Sales Table filtered by SKU Code in Visual. Here I am multiplying Quantity by Cost from “Food Cost” table. To derive cost I am doing below
MaxCostDate -To identify Max Cost Date based on Sales Date. For example if Sales date for 1002 is 12th Dec 2019, it will get Max Cost Date by filtering Cost table below this date. In this case output will be 1st Dec 2019.
MinCostDate - In case MaxDate is blank , it will get the minimum date available. For example, for 1002 there are sales for Nov 2019 but minimum date in Cost table is 1st Dec 2019.
FinalDate is MaxCostDate. if not available then MinCostDate.
Return will provide the cost value based on FinalDate.