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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
BBConsultancy
Frequent Visitor

Divide invoice header amount over invoice lines

I have a data table with invoice lines. Invoice headers are also in the table and are marked with invoice line number 0. The invoice discount is marked on the invoice header and I want to take the invoice header discount and divide it over the lines of the invoice.

 

What I want to achieve in Power Query is the following:

If lineNumber is not 0 then take the InvoiceDiscount of the header of this invoice (line number 0) and divide it by the total amount of lines for this invoice.

 

So in the example in thepicture below I want to replace all null with 0,03/7=

PowerBI Example.png

 

Looking forward to your help!

 

 

2 ACCEPTED SOLUTIONS
tackytechtom
Super User
Super User

Hi @BBConsultancy ,

 

How about this? 🙂

 

Before:

tackytechtom_1-1708363669348.png

 

After:

tackytechtom_0-1708363614769.png

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYAAlMlHSUDENYzMFaK1UESNQRiVBEjDBFjDBETDBFTDBEzDBFzsEgsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Invoice Number" = _t, #"Invoice Line Number" = _t, InvoiceDiscount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice Number", Int64.Type}, {"Invoice Line Number", Int64.Type}, {"InvoiceDiscount", type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Invoice Line Number] = 0)),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Invoice Number"}, {{"MaxLineNumber", each List.Max([Invoice Line Number]), type nullable number}}),
    #"Merged Queries 1" = Table.NestedJoin(#"Filtered Rows", {"Invoice Number"}, #"Grouped Rows", {"Invoice Number"}, "Grouped Rows 1", JoinKind.LeftOuter),
    #"Expanded Grouped Rows 2" = Table.ExpandTableColumn(#"Merged Queries 1", "Grouped Rows 1", {"MaxLineNumber"}, {"MaxLineNumber"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows 2", "SpreadInvoiceDiscountOverLines", each [InvoiceDiscount] / [MaxLineNumber]),
    #"Merged Queries 2" = Table.NestedJoin(#"Changed Type", {"Invoice Number"}, #"Added Custom", {"Invoice Number"}, "Grouped Rows 2", JoinKind.LeftOuter),
    #"Expanded Grouped Rows 1" = Table.ExpandTableColumn(#"Merged Queries 2", "Grouped Rows 2", {"SpreadInvoiceDiscountOverLines"}, {"SpreadInvoiceDiscountOverLines"}),
    #"Replace Values" = Table.ReplaceValue(#"Expanded Grouped Rows 1",each [InvoiceDiscount],each if [InvoiceDiscount] is null then [SpreadInvoiceDiscountOverLines] else [InvoiceDiscount],Replacer.ReplaceValue,{"InvoiceDiscount"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replace Values",{"SpreadInvoiceDiscountOverLines"})
in
    #"Removed Columns"

 

Let me know, if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

v-junyant-msft
Community Support
Community Support

Hi @BBConsultancy ,

@tackytechtom Good Answer!
And you can try this:
Here is my sample data:

vjunyantmsft_0-1708397953998.png

Duplicate the original form:

vjunyantmsft_1-1708398011231.png

And in Table(2):

vjunyantmsft_2-1708398032987.png

vjunyantmsft_0-1708398383579.png

 

Then merge Table and Table(2) as a new one and expand:

vjunyantmsft_3-1708398079710.png

vjunyantmsft_4-1708398125357.png

In the new table Merge1, add a custom column:

 

if [Table.lineNumber] = 0 then 0.03 else 0.03/([Count] - 1)

 

The final output is as below:

vjunyantmsft_5-1708398231382.png

 

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-junyant-msft
Community Support
Community Support

Hi @BBConsultancy ,

@tackytechtom Good Answer!
And you can try this:
Here is my sample data:

vjunyantmsft_0-1708397953998.png

Duplicate the original form:

vjunyantmsft_1-1708398011231.png

And in Table(2):

vjunyantmsft_2-1708398032987.png

vjunyantmsft_0-1708398383579.png

 

Then merge Table and Table(2) as a new one and expand:

vjunyantmsft_3-1708398079710.png

vjunyantmsft_4-1708398125357.png

In the new table Merge1, add a custom column:

 

if [Table.lineNumber] = 0 then 0.03 else 0.03/([Count] - 1)

 

The final output is as below:

vjunyantmsft_5-1708398231382.png

 

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

tackytechtom
Super User
Super User

Hi @BBConsultancy ,

 

How about this? 🙂

 

Before:

tackytechtom_1-1708363669348.png

 

After:

tackytechtom_0-1708363614769.png

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYAAlMlHSUDENYzMFaK1UESNQRiVBEjDBFjDBETDBFTDBEzDBFzsEgsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Invoice Number" = _t, #"Invoice Line Number" = _t, InvoiceDiscount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Invoice Number", Int64.Type}, {"Invoice Line Number", Int64.Type}, {"InvoiceDiscount", type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Invoice Line Number] = 0)),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Invoice Number"}, {{"MaxLineNumber", each List.Max([Invoice Line Number]), type nullable number}}),
    #"Merged Queries 1" = Table.NestedJoin(#"Filtered Rows", {"Invoice Number"}, #"Grouped Rows", {"Invoice Number"}, "Grouped Rows 1", JoinKind.LeftOuter),
    #"Expanded Grouped Rows 2" = Table.ExpandTableColumn(#"Merged Queries 1", "Grouped Rows 1", {"MaxLineNumber"}, {"MaxLineNumber"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Grouped Rows 2", "SpreadInvoiceDiscountOverLines", each [InvoiceDiscount] / [MaxLineNumber]),
    #"Merged Queries 2" = Table.NestedJoin(#"Changed Type", {"Invoice Number"}, #"Added Custom", {"Invoice Number"}, "Grouped Rows 2", JoinKind.LeftOuter),
    #"Expanded Grouped Rows 1" = Table.ExpandTableColumn(#"Merged Queries 2", "Grouped Rows 2", {"SpreadInvoiceDiscountOverLines"}, {"SpreadInvoiceDiscountOverLines"}),
    #"Replace Values" = Table.ReplaceValue(#"Expanded Grouped Rows 1",each [InvoiceDiscount],each if [InvoiceDiscount] is null then [SpreadInvoiceDiscountOverLines] else [InvoiceDiscount],Replacer.ReplaceValue,{"InvoiceDiscount"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replace Values",{"SpreadInvoiceDiscountOverLines"})
in
    #"Removed Columns"

 

Let me know, if this helps 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

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 FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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