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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Lars_Denmark
Frequent Visitor

Cost allocation with two fact tables

I have run into a problem with distribution that I cannot solve myself, and I therefore hope that there is someone here who can help.

 

Lars_Denmark_0-1717763489468.png

 

 

I have two sales tables, consisting of an Invoice Header table and one with associated Invoice Lines. On each invoice header, there is information about total sales and gross profit. The table with invoice lines contains information about the item, quantity sold, and unit price. For each invoice header, there are one or more lines with item numbers belonging to the "Other" category, as well as invoice lines with item numbers belonging to all other categories.

 

I now want to be able to calculate the Gross Margin for each invoice line and for this, a product cost must be calculated for each invoice line.

 

There is no profit on the sale of items belonging to the category "Other", and therefore the cost of goods sold (COGS) on these lines must be equal to the total sales of the line (quantity sold * unit price). The remaining part of the invoice's COGS must then be distributed to the remaining invoice lines based on the line's sales as a percentage of the total sales for all lines excluding the lines with category "Other".

 

When the COGS has been distributed from header to the lines, the Gross Margin must be calculated. It must then be possible to make a statistic for each category with information on sales of goods, Gross Margin and Gross Margin%.

The distribution should be as shown in the example below:

 

Lars_Denmark_0-1717762655428.png

 

My PBIX file can be downloaded from WeTransfer with this link: https://we.tl/t-Ano1FDOKxq 

Any help is greatly appreciated!

 

 

1 ACCEPTED SOLUTION

Hi @Lars_Denmark ,

In order for your allocation % to also respect the LineNo filter for your % of sales calculation you can tweak the two dax measures as follows:

First, modify your denominator of the % of sales measure by including all function to ignore filter for the LineNo field. 

DataNinja777_0-1717848228799.png

The above measure is then used in the % calculation in your data model. 

Also, another part you need to modify is the following measure by including the yellow highlighted part for the same reason as above. 

 

DataNinja777_0-1717898148426.png

 

The resultant output produces the consistent output for sales % allocation respecting also the LineNo field.  

DataNinja777_2-1717848588608.png

I attach the updated pbix file.  

Best regards,

 

 

View solution in original post

7 REPLIES 7
DataNinja777
Super User
Super User

Hi @Lars_Denmark 

I think your data modeling is very good 👌  I like the way you put the dimension tables at the top and the fact tables at the bottom, and used easy way to identify all the measures in one location which I think is a good idea.  One of the ways to achive your required output is to use ALL function to calculate the % of sales by category and multiply the costs to be allocated by that.  The steps I have taken are as follows.  

First identify the sales excluding the "Other" category.  

DataNinja777_0-1717771717417.png

Then use the all function to remove filter for the category using the output above.

DataNinja777_1-1717771798202.png

Then, divide the 1st measure by the 2nd one to get the % of the sales excluding the "Other" category by customers and by invoice numbers.  

DataNinja777_2-1717771867245.png

 

Next identify the COGS to be allocated:

DataNinja777_1-1717898288905.png

 

Then multiply the COGS to be allocated by the % of sales category by customers and invoice numbers.

DataNinja777_2-1717898559319.png

 

Then, combine the allocated and not allocated COGS to get the total COGS in one line.  

DataNinja777_5-1717772211706.png

Then finally show the GM by category after COGS allocation.  

DataNinja777_6-1717772293816.png

The resultant output is as shown below.  

DataNinja777_7-1717772329454.png

The above is just one of multiple ways to achieve your required output, and I suppose you can also try to combine all the steps above in just one big formula using VAR and RETURN (haven't tried, but maybe).  But I find it easier to break the steps down into bite-sized smaller components.  The advantage of smaller formulas is that is it is easier to debug while simultaneously visualizing output of the each step taken by showing those measure outputs in a matrix table and checking that they are in line with your expected results.   

 

I attach an example pbix file. 

Best regards,

 

 

 

 

Thank you very much DataNinja777 for your kind words about my data modeling and your solution, which looks absolutely right.

 

After I have worked a bit with a few visualizations, it has turned out that I get an incorrect calculation on rows if I insert a column from the fact table InvoiceRow on the rows. In that situation, it looks like it's because measure Row.Sales(<>Other) (All) is calculating an incorrect value.

 

I cannot figure out why this error appears, and I therefore really hope that you have the opportunity to help me solve that challenge as well, so that the following visualization can be made:

 

Lars_Denmark_1-1717843721911.png

This table is fundamental in my Power BI model.

 

I will appreciate to hear from you again.

 

Hi @Lars_Denmark ,

In order for your allocation % to also respect the LineNo filter for your % of sales calculation you can tweak the two dax measures as follows:

First, modify your denominator of the % of sales measure by including all function to ignore filter for the LineNo field. 

DataNinja777_0-1717848228799.png

The above measure is then used in the % calculation in your data model. 

Also, another part you need to modify is the following measure by including the yellow highlighted part for the same reason as above. 

 

DataNinja777_0-1717898148426.png

 

The resultant output produces the consistent output for sales % allocation respecting also the LineNo field.  

DataNinja777_2-1717848588608.png

I attach the updated pbix file.  

Best regards,

 

 

Unfortunately, it has turned out that cost allocation is not calculated correctly when there is a calculation of Gross Margin and GM% for each Category, where InvoiceNo is not included in Rows.

 

Lars_Denmark_0-1718092804426.png

 

It seems that the problem is caused by the fact that Row.Sales(<> Other) (ALL) is calculated for all rows shown in the table, and this results in Row.GM% being the same value for all categories. The reason for this is probably that there is no filter on InvoiceNo in the table.

 

If I insert the InvoiceNo on the rows everything is perfect.

 

Lars_Denmark_1-1718093373658.png

 

I have tried to solve the problem by inserting calculated columns in the table InvoiceRow, to have the allocated cost value on each row, but it has not been possible for me to do this.

 

I therefore hope that you or another skilled Power BI expert can help solve this challenge - then I have a perfect model.

Hi @Lars_Denmark ,

To me, with and without the  InvoiceNo seem to be yielding the same Row.GM% as it is.  Please could you point to me which part you are not seeing the expected result?  

DataNinja777_0-1718108378612.png

 

 

 

DataNinja777_1-1718108682157.png

 

Thank you & best regards,

Thank you for taking the time to respond so quickly.

 

In the two tables (2 and 4 from top) where you only have Category as a row, GM by Category and Row.GM% are correct in the Total. But when you look at GM by Category and Row.GM% for each category, they are not correct.

 

I have created the table below in Excel, where at the bottom highlighted in yellow you can see the expected value for GM by Category and Row.GM% for each category.

 

It is based on the calculations per category, which is created at invoice level.

 

Lars_Denmark_0-1718117730322.png

 

3CloudThomas
Super User
Super User

You would need to combine these into one table and distriubte the total sales and profit proportionality by number of lines or something on those lines.
That is a high level thought on this, but to implement in a chat like this would not be possible.

Helpful resources

Announcements
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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.