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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Timo1980
Advocate I
Advocate I

Need help with list.Sum custom colomns

Hi All,

 

i have the following issue in Power Query i want to create a customer colomn ( i know i can do this with grouping or in Dax, but i need to to this in power query) Table 1 is the data and i want to create the colomns Total Revenue and Sales Percentage. For the Total Revenue i only want to take into account positive revenue. Any help would be appreciated!!

 

CustomerQTYRevenueTotal RevenueSales Percentage
A1102836%
A2182864%
B22525100%
C22323100%
C-2-5nullnull
D-1-8nullnull
D292438%
D2152463%

 

Timo1980_0-1616533135164.png

 

 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Timo1980 

 

add a new column and enter this formula. The only thing you have to pay attention is that the variable ChangedType you have to replace with your prior step name.

List.Sum(Table.SelectRows(ChangedType, (sel)=> sel[Customer]=[Customer] and sel[Revenue]>=0)[Revenue])

here the complete code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYQOlWB0I1wjEtQBznaBcI1Mw1xnGNYZzdUF8XYi0C4gJMkzXAs4HSVui8AyBimMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, QTY = _t, Revenue = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"QTY", Int64.Type}, {"Revenue", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(ChangedType, "Total Revenue", each List.Sum(Table.SelectRows(ChangedType, (sel)=> sel[Customer]=[Customer] and sel[Revenue]>=0)[Revenue])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "%", each [Revenue]/[Total Revenue],Percentage.Type)
in
    #"Added Custom1"

and here is the output

Jimmy801_0-1616595933222.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

3 REPLIES 3
Jimmy801
Community Champion
Community Champion

Hello @Timo1980 

 

add a new column and enter this formula. The only thing you have to pay attention is that the variable ChangedType you have to replace with your prior step name.

List.Sum(Table.SelectRows(ChangedType, (sel)=> sel[Customer]=[Customer] and sel[Revenue]>=0)[Revenue])

here the complete code

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYQOlWB0I1wjEtQBznaBcI1Mw1xnGNYZzdUF8XYi0C4gJMkzXAs4HSVui8AyBimMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, QTY = _t, Revenue = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"QTY", Int64.Type}, {"Revenue", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(ChangedType, "Total Revenue", each List.Sum(Table.SelectRows(ChangedType, (sel)=> sel[Customer]=[Customer] and sel[Revenue]>=0)[Revenue])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "%", each [Revenue]/[Total Revenue],Percentage.Type)
in
    #"Added Custom1"

and here is the output

Jimmy801_0-1616595933222.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

Anonymous
Not applicable

let
    Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYQOlWB0I1wjEtQBznaBcI1Mw1xnGNYZzdUF8XYi0C4gJMkzXAs4HSVui8AyBimMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, QTY = _t, Revenue = _t]),
    #"Modificato tipo" = Table.TransformColumnTypes(Origine,{{"QTY", type number}, {"Revenue", type number}}),
    #"Raggruppate righe" = Table.Group(#"Modificato tipo", {"Customer"}, {{"Total Revenue", each List.Sum(List.Select([Revenue], (r)=>r>0)), type nullable text},{"rev", each Table.AddColumn(_, "%rev", (pc)=> pc[Revenue]/List.Sum(List.Select([Revenue], (r)=>r>0))) }} ),
    #"Tabella rev espansa" = Table.ExpandTableColumn(#"Raggruppate righe", "rev", {"QTY", "Revenue", "%rev"}, {"QTY", "Revenue", "%rev"})
in
    #"Tabella rev espansa"
HotChilli
Super User
Super User

Duplicate the table.

Filter out the -ve figures from the Revenue column (you can use the Number filter feature from the column header dropdown).

Then select the Customer column, choose 'Group By' from the ribbon (Sum the Revenue column, give it a name 'Tot Rev').

This will give you a 2 column table with the +ve revenues summed.

--------------------

Go back to the original table.

Pick 'Merge Queries as new' from the ribbon.

Merge the original with the new table (created above) on Customer field. Use Inner Join.

Expand the column header to return only the 'Tot Rev' column.

Add a conditional column (from Menu), use the interface to mimic the code below

 

if Revenue < 0 then null else 'Tot Rev'

 Change the column type to be  whole number.

Then select the original Revenue Column and the new Group Revenue column.

Pick 'Add Column' from the menu->go to the From Number section->pick Standard-> Divide.

That should add a column with the right arithmetic.

I would leave that column as a decimal (i.e. don't change it to percenatge type- it'll make it easier in powerbi front end).

Let me know how you go.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors