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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
gauravnarchal
Post Prodigy
Post Prodigy

Calculate differences of each order

How can I calculate the total difference (Balance) of each order and display the result as shown below?

 

 

Data - Table 1

 

ReferenceOrderDiff
1096989BLQ00888183728.61
 BLQ00888183-735.00
1097010BLQ008881941580.01
 BLQ00888194-790.00
 BLQ00888194-790.00
1097012BLQ00888200-0.26
1096965BLQ00888207-17.94
1097023BLQ008882111146.00
 BLQ00888211-580.00
 BLQ00888211-580.00
1097019BLQ00888224-18.70
1097105BLQ008882812080.10
 BLQ00888281-2165.00
1097140BLQ00888285-30.54
1097141BLQ00888287-0.94
1097154BLQ00888297-4.19
1097165BLQ008883005034.33
 BLQ00888300-1595.00
 BLQ00888300-1595.00
 BLQ00888300-1925.00
1097186BLQ00888303-44.80
1097241BLQ00888309-31.63
1097253BLQ00888310-10.18
1097257BLQ00888311-56.84
0BLQ008883120.00
1097376BLQ008883159228.99
 BLQ00888315-4690.00
 BLQ00888315-4690.00
1097402BLQ00888319-14.39
1097460BLQ00888321-57.94
1097457BLQ00888322-40.15
1097463BLQ0088832317092.26
 BLQ00888323-6565.00
 BLQ00888323-5355.00
 BLQ00888323-5355.00
1097478BLQ00888324-16.86
1097469BLQ008883253148.55
 BLQ00888325-3190.00
1097549BLQ008883283387.65
 BLQ00888328-3495.00
1097565BLQ00888330266.12
1097594BLQ00888331-13.82
1097628BLQ00888332-45.40

 

Result

 

ReferenceOrderDiff Balance 
1096989BLQ00888183728.61            (6)
 BLQ00888183-735.00-
1097010BLQ008881941580.01             0
 BLQ00888194-790.00-
 BLQ00888194-790.00-
1097012BLQ00888200-0.26-0.26
1096965BLQ00888207-17.94-17.94
1097023BLQ008882111146.00-14.00
 BLQ00888211-580.00-
 BLQ00888211-580.00-
1097019BLQ00888224-18.70-18.70
1097105BLQ008882812080.10-84.90
 BLQ00888281-2165.00-
1097140BLQ00888285-30.54-30.54
1097141BLQ00888287-0.94-
1097154BLQ00888297-4.19-
1097165BLQ008883005034.33-80.67
 BLQ00888300-1595.00-
 BLQ00888300-1595.00-
 BLQ00888300-1925.00-
1097186BLQ00888303-44.80-44.80
1097241BLQ00888309-31.63-31.63
1097253BLQ00888310-10.18-10.18
1097257BLQ00888311-56.84-56.84
 BLQ008883120.000.00
1097376BLQ008883159228.99-151.01
 BLQ00888315-4690.00-
 BLQ00888315-4690.00-
1097402BLQ00888319-14.39-14.39
1097460BLQ00888321-57.94-57.94
1097457BLQ00888322-40.15-40.15
1097463BLQ0088832317092.26-182.74
 BLQ00888323-6565.00-
 BLQ00888323-5355.00-
 BLQ00888323-5355.00-
1097478BLQ00888324-16.86-16.86
1097469BLQ008883253148.55-41.45
 BLQ00888325-3190.00-
1097549BLQ008883283387.65-107.35
 BLQ00888328-3495.00-
1097565BLQ00888330266.12266.12
1097594BLQ00888331-13.82-13.82
1097628BLQ00888332-45.40-45.40

 

1 ACCEPTED SOLUTION
v-chenwuz-msft
Community Support
Community Support

Hi @gauravnarchal ,

 

You can try use this code to do that in power query.

let

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZM7bsQwDETv4joS+BfZpk6TepH7XyP6ILa4XmCBlMI8kcMR9XgcCGHhcXwcn1/fAO6Ozv3UyKvh8fPxOG5aaaxT6ZcbIOxASD+hOlS4355iaQFvlFWXNoBgdClQyf4IC9NEtEFgq73WWYR4RxCHORR77r+U0m2/UZazPS6iaR29tpNASM58lCDomdwiWVohtCtRFEjEqFUYqsqGYELaCmebHFV2IiYhtVs/iRQfz4AVWCrzs8slFtTQf0hB22xuiZjrJFL9JCiNxhBzeqzGF6L7qzKuNj3drYq2hKxHtOorIUjiWLTrebklizhSCurfIeI24RSL2H2jn6VRWYASMUfDHvj1KGLJGi3faaclj0Y0+/TpdauSApqfABsE/X2fm1pM7f6AS1LWt9Ls2jwR61/0zG0zFgkZGTGK11cd1t7jFqBKvj76MXurr7z7vC5xGdS88TyiJrPaF+BEQhIy80eufiFGaUxe+WvtL/TzCw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Reference = _t, Order = _t, Diff = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Reference", Int64.Type}, {"Order", type text}, {"Diff", type number}}),
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Reference"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Reference"}, {{"balance", each List.Sum([Diff]), type nullable number}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type" , "Query1", each (if [Reference] <> null then Query1(#"Grouped Rows", [Reference]) else "-"))
in
    #"Invoked Custom Function"

 

Query1:

let
    Source = (a as table,b) => let s1 = Table.SelectRows(a,each [Reference] = b)[balance]{0} in s1
in
    Source

Result:

vchenwuzmsft_0-1661235971521.png

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

4 REPLIES 4
v-chenwuz-msft
Community Support
Community Support

Hi @gauravnarchal ,

 

You can try use this code to do that in power query.

let

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZM7bsQwDETv4joS+BfZpk6TepH7XyP6ILa4XmCBlMI8kcMR9XgcCGHhcXwcn1/fAO6Ozv3UyKvh8fPxOG5aaaxT6ZcbIOxASD+hOlS4355iaQFvlFWXNoBgdClQyf4IC9NEtEFgq73WWYR4RxCHORR77r+U0m2/UZazPS6iaR29tpNASM58lCDomdwiWVohtCtRFEjEqFUYqsqGYELaCmebHFV2IiYhtVs/iRQfz4AVWCrzs8slFtTQf0hB22xuiZjrJFL9JCiNxhBzeqzGF6L7qzKuNj3drYq2hKxHtOorIUjiWLTrebklizhSCurfIeI24RSL2H2jn6VRWYASMUfDHvj1KGLJGi3faaclj0Y0+/TpdauSApqfABsE/X2fm1pM7f6AS1LWt9Ls2jwR61/0zG0zFgkZGTGK11cd1t7jFqBKvj76MXurr7z7vC5xGdS88TyiJrPaF+BEQhIy80eufiFGaUxe+WvtL/TzCw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Reference = _t, Order = _t, Diff = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Reference", Int64.Type}, {"Order", type text}, {"Diff", type number}}),
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Reference"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Reference"}, {{"balance", each List.Sum([Diff]), type nullable number}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type" , "Query1", each (if [Reference] <> null then Query1(#"Grouped Rows", [Reference]) else "-"))
in
    #"Invoked Custom Function"

 

Query1:

let
    Source = (a as table,b) => let s1 = Table.SelectRows(a,each [Reference] = b)[balance]{0} in s1
in
    Source

Result:

vchenwuzmsft_0-1661235971521.png

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

Greg_Deckler
Community Champion
Community Champion

@gauravnarchal Sorry, I feel like I am missing some vital piece of information. For example, where does the (6) come from in the first row of the second table?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

If you sum the diff of order BLQ00888183 will give you the result -6

 

gauravnarchal_0-1660847185007.png

 

@gauravnarchal So, first, if you haven't already, you will want to do a Fill down for your Reference column so that the appropriate Reference # appears on each row. Once you do that, put your Reference column in a table visual along with Order. Then just add your Diff column with a default SUM aggregation.

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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