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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Carlo1975
Helper I
Helper I

insert new calculated field

Hi, I have a table with 2 fields, one is the description and one is the amount related to description.

But in some case our management system can.'t know if the description is equal to "buy" or to "sell" and so, the system insert in the description the value "-1"

 

DescriptionAmount
Sell150
Buy256
Sell895
Sell744
Sell122
Sell252
Buy253
Buy1236
-1500
-1300

 

Now, to our investor I can't show in my report that there are record equal to -1, and so I would to create a new column and take "Buy" or "Sell" when are "Buy" or "Sell", and I would to insert in place of -1 a value related to this calculation:

 

1) Sum all the "Buy" and all the "sell". In my example:

 

DescriptionAmount
Sell2163
Buy1745

 

2) and assign the description of the lowest value to -1. So in my example, in the new column there will be in place of -1, Buy.

 

New DescriptionDescriptionAmount
SellSell150
BuyBuy256
SellSell895
SellSell744
SellSell122
SellSell252
BuyBuy253
BuyBuy1236
Buy-1500
Buy-1300

 

thank you.

Carlo

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@Carlo1975 
You can do it in a power query with simple steps and one custom column. Past the below code on a Blank Query on the Advanced Editr and check the :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7NyVHSUTI0NVCK1YlWciqtBPKMTM3APKikhaUpMtfcxASZa2hkhMw1MjVCMckYiWdoZAwxWNcQyDM1MEBwjEGcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Description] <> "-1")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Description"}, {{"Total", each List.Sum([Amount]), type nullable number}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Total", Order.Ascending}}),
    #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1),
    Custom1 = #"Changed Type",
    #"Added Custom" = Table.AddColumn(Custom1, "Custom", each if [Description] = "-1" then #"Kept First Rows"[Description]{0} else [Description])
in
    #"Added Custom"

 

Fowmy_0-1606315177958.png

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 





Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

1 REPLY 1
Fowmy
Super User
Super User

@Carlo1975 
You can do it in a power query with simple steps and one custom column. Past the below code on a Blank Query on the Advanced Editr and check the :

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7NyVHSUTI0NVCK1YlWciqtBPKMTM3APKikhaUpMtfcxASZa2hkhMw1MjVCMckYiWdoZAwxWNcQyDM1MEBwjEGcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Description] <> "-1")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Description"}, {{"Total", each List.Sum([Amount]), type nullable number}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Total", Order.Ascending}}),
    #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1),
    Custom1 = #"Changed Type",
    #"Added Custom" = Table.AddColumn(Custom1, "Custom", each if [Description] = "-1" then #"Kept First Rows"[Description]{0} else [Description])
in
    #"Added Custom"

 

Fowmy_0-1606315177958.png

________________________

If my answer was helpful, please consider Accept it as the solution to help the other members find it

Click on the Thumbs-Up icon if you like this reply 🙂

YouTube  LinkedIn

 





Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.