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, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
RAQuest
New Member

Freight Cost per Unit - Invoice Cost Allocation

I am struggling with Invoice Freight Cost allocation per Unit.

 

My data consists of InvoiceNo, FreightAmt, ItemCode and Quantity Shipped (see table below). For example: Invoice 91 cost $23.18 to ship a total of 12 units or $23.18 / 12 = $1.93. The problem is the Freight Cost is allocated on the Inovice Level and I cannot sort out how to sum the qty shipped then divide by the Invoice Freight Amt. Any suggestions?

 

InvoiceNoFreightAmtItemCodeQuantityShipped
9123.18Product 16
9123.18Product 26
92330.36Product 336
92330.36Product 236
92330.36Product 424
92330.36Product 524
92330.36Product 612
386877.09Product 4100
386877.09Product 3120
386877.09Product 5100
386877.09Product 2100
828800.24Product 4120
828800.24Product 3120
828800.24Product 5120
828800.24Product 60
828800.24Product 2120
902428.16Product 160
902428.16Product 760
902428.16Product 860
1054116.79Product 660
9250Product 91000
1 ACCEPTED SOLUTION
v-jingzhan-msft
Community Support
Community Support

Hi @RAQuest 

 

You can create a new calculated column in your data table to calculate the freight cost per unit for each row. You can use the following DAX formula: 

DAX Freight Cost per Unit = [FreightAmt] / SUMX(FILTER('YourTableName', 'YourTableName'[InvoiceNo] = EARLIER('YourTableName'[InvoiceNo])), 'YourTableName'[QuantityShipped])

Replace 'YourTableName' with the actual name of your table.

Here's what this formula does:

- `FILTER` function is used to create a table that contains only the rows with the same `InvoiceNo`.

- `SUMX` function iterates over this filtered table and sums up the `QuantityShipped` for the invoice.

- The total freight amount (`FreightAmt`) for the invoice is then divided by this sum to get the freight cost per unit.

vjingzhanmsft_0-1703641981933.png

 

Once you have created the calculated column, you can use it in your reports to analyze the data. You can format it in Column tools ribbon. 

 

Best regards,

Jing

View solution in original post

4 REPLIES 4
dufoq3
Super User
Super User

Add these steps below your last step:

    ChangedTypeLocale = Table.TransformColumnTypes(Source,{{"InvoiceNo", Int64.Type}, {"FreightAmt", Currency.Type}, {"ItemCode", type text}, {"QuantityShipped", Int64.Type}}, "en-US"),
    #"Grouped Rows" = Table.Group(ChangedTypeLocale, {"InvoiceNo"}, {{"FreightAmt", each List.Average([FreightAmt]), Currency.Type}, {"QuantityShippedTotal", each List.Sum([QuantityShipped]), Int64.Type}, {"All", each _, type table [InvoiceNo=nullable number, FreightAmt=nullable number, ItemCode=nullable text, QuantityShipped=nullable number]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"ItemCode", "QuantityShipped"}, {"ItemCode", "QuantityShipped"}),
    #"Inserted Division" = Table.AddColumn(#"Expanded All", "Freight Cost per Unit", each [FreightAmt] / [QuantityShippedTotal], Currency.Type)
in
    #"Inserted Division"

dufoq3_0-1703689573983.png

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

v-jingzhan-msft
Community Support
Community Support

Hi @RAQuest 

 

If you want to calculate freight cost per unit with Power Query, you can use Group by feature. You need to group your data by `InvoiceNo` to sum the `QuantityShipped` for each invoice and get the maximum `FreightAmt` for each invoice. 

vjingzhanmsft_0-1703643594818.png

 

Then add a custom column to divide the `FreightAmt` by `TotalQuantityShipped` as below. 

Number.Round([FreightAmt] / [TotalQuantityShipped],2)

vjingzhanmsft_1-1703643806013.png

Hope this helps. 

 

Best regards,

Jing

v-jingzhan-msft
Community Support
Community Support

Hi @RAQuest 

 

You can create a new calculated column in your data table to calculate the freight cost per unit for each row. You can use the following DAX formula: 

DAX Freight Cost per Unit = [FreightAmt] / SUMX(FILTER('YourTableName', 'YourTableName'[InvoiceNo] = EARLIER('YourTableName'[InvoiceNo])), 'YourTableName'[QuantityShipped])

Replace 'YourTableName' with the actual name of your table.

Here's what this formula does:

- `FILTER` function is used to create a table that contains only the rows with the same `InvoiceNo`.

- `SUMX` function iterates over this filtered table and sums up the `QuantityShipped` for the invoice.

- The total freight amount (`FreightAmt`) for the invoice is then divided by this sum to get the freight cost per unit.

vjingzhanmsft_0-1703641981933.png

 

Once you have created the calculated column, you can use it in your reports to analyze the data. You can format it in Column tools ribbon. 

 

Best regards,

Jing

Jing - thank you so much for sharing your wisdom. More so, your explain of how and why your measure worked! 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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

Top Solution Authors