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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

FIFO COGS finding cost per sale

I am trying to figure out how to get the COGS per individual sales in Power BI. (I put the Sales table from Power BI into excel to show it easier.)
Sales Table

AlvinM_2-1680125684789.png

 

 Invoices Table (Has what products are bought for the store and how much each cost)

AlvinM_1-1680125644204.png
So right now the Sales table shows the total cost of COGS for the highlighted sales of ASIN "B00HSRVBAG" for a Quantity of "8" is $22.00.  It should be a total of 1@$1.00 + 1@$2.00 + 6@$3.00 for a total of $21.00.

Currently for COGS I am using this

COGS =
var CurrentDate = Sales[customer_shipment_date]
var CurrentSoldQTY = Sales[ASIN Sales QTY Running Total]
var CurrentAsin = Sales[asin]
var CurrentQTY = Sales[quantity]
var InvoicesFilteredTable = FILTER(Invoices,CurrentSoldQTY > Invoices[Total QTY Purchased]
&& Invoices[ASIN] = CurrentAsin)
var SalesFilteredTable = FILTER(Sales,Sales[customer_shipment_date] <= CurrentDate
&& Sales[asin] = CurrentAsin)
var ExtraFilteredTable = FILTER(Invoices,CurrentSoldQTY <= Invoices[Total QTY Purchased]
&& Invoices[ASIN] = CurrentAsin)
Return
IF(Sales[quantity]>1,
SELECTCOLUMNS(TOPN(1,ExtraFilteredTable),"Rate",[Rate]) *
IF(ISNUMBER(SELECTCOLUMNS(TOPN(1,InvoicesFilteredTable),"Quantity",[quantity])),
CALCULATE(SUM(Sales[quantity]),SalesFilteredTable) - SELECTCOLUMNS(TOPN(1,InvoicesFilteredTable),"Quantity",[quantity]),
BLANK()
) + IF(ISBLANK(SELECTCOLUMNS(TOPN(1,InvoicesFilteredTable),"Quantity",[quantity])),
BLANK(),
SELECTCOLUMNS(TOPN(1,InvoicesFilteredTable),"Rate",[Rate]) * SELECTCOLUMNS(TOPN(1,InvoicesFilteredTable),"Quantity",[quantity])
),
SELECTCOLUMNS(TOPN(1,ExtraFilteredTable),"Rate",[Rate]) * CurrentQTY)

Which get the right COGS if there is only 2 different purchasing cost it has to look at but once an order has to go through more than 2 different COGS price changes it stops.


Thanks for any help.

1 REPLY 1
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.