A database consists of master data table with product information and a transaction table with sales volumes.
The master data table includes sales price, discount, and sales margin. The product is normally sold at sales price but sometimes discount is granted. Sales price minus costs equal to margin. The net sales equals to sales minus discount.
The data is stored in MySQL database.

Master data table md_prod.

Data is very minor but the idea is to demonstrate the logic.
I have in earlier blogs demonstrated how to report in Excel or in Power BI a data in MySQL database, therefore those parts are not included in this blog.
First we need to count the sales in money, that is sales volume multiplied by sales volume.
The DAX:
=SUMX(tr_sales,[sales]* RELATED(md_prod[price]))
This is also something I have presented in my earlier blogs.

The results.
Check the same from the database.

The results are similar in MySQL. Left join was used to take all the materials from left master data table. There are no sales records for the product P2, but as the P2 is in master data table, we will take that into our report.
To calculate the COGS we need to minus margin from sales price.
The DAX:
=SUMX(tr_sales,[sales]* (RELATED(md_prod[price] )))-SUMX(tr_sales,[sales]* (RELATED(md_prod[margin] )))

Then we check from MySQL.

The results are the same in BI and in SQL.
The DAX:
=SUMX(tr_sales,[sales]* ((RELATED(md_prod[price]))- (((RELATED(md_prod[price]))) * (RELATED(md_prod[disc])))))

I almost got lost with parentheses in DAX.
When counting sales in money, COGS, or net sales, we took sales volume in the transaction table and material specific data in the master data table. In all three cases we used RELATED function.
However, I felt that SQL simpler than DAX is.




