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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
daniegajohnson
Frequent Visitor

Insert row when conditions met on existing data

Hey All,

I am looking to add new rows to my data using values from the existing rows. Their are around 600,000 rows of data at the moment. Here is a parsed down sample:

Lookup IDTransaction DateTypeQuantityNet AmountMembership LevelMembership Transaction TypeMembership Promotion NameMembership Promotion TypeMembership Promotion Amount
12345678912/15/19Membership 1 95 Family JoinGroupon Membership DiscountDiscount 95
12345678015/29/19Membership 1 95 Family Renew Expired Groupon DiscountDiscount 75

 

I would like the result to look like this:

 

 

Lookup IDTransaction DateTypeQuantityNet AmountMembership LevelMembership Transaction TypeMembership Promotion NameMembership Promotion TypeMembership Promotion Amount
12345678912/15/19Membership 1 95 Family JoinGroupon Membership DiscountDiscount 95
12345678912/15/19Membership 1 -95 Family DiscountGroupon Membership DiscountDiscount95
12345678012/29/19Membership 1 95 Family Renew Expired Groupon DiscountDiscount 75
12345678012/29/19Membership 1 -75 Family Discount Expired Groupon DiscountDiscount75

 

In my head I was thinking something like Custom Row = each if [Membership Promotion Type] = "Discount" then Table.InsertRows, but I'm not sure what to do next to pull the values from the proper line (becuase I'm new here).

 

Any help would be greatly appreciated!

 

Thanks,

 

Dan

 

1 ACCEPTED SOLUTION

Hello @daniegajohnson 

 

try out this solution. if filters the table and applys the changes requested by you. After that both tables are combined.

let
	Source = #table
	(
		{"Lookup ID","Transaction Date","Type","Quantity","Net Amount","Membership Level","Membership Transaction Type","Membership Promotion Name","Membership Promotion Type","Membership Promotion Amount"},
		{
			{"123456789","43814","Membership"," 1"," 95"," Family"," Join","Groupon Membership Discount","Discount"," 95"},	{"123456780","43828","Membership"," 1"," 95"," Family"," Renew"," Expired Groupon Discount","Discount"," 75"}

					}
	),
    ChangeType = Table.TransformColumnTypes(Source,{{"Membership Promotion Amount", Int64.Type}, {"Net Amount", Int64.Type}}),
    FilterForDiscount = Table.SelectRows
    (
        ChangeType,
        each [Type] = "Membership" and [Membership Promotion Type] = "Discount"
    ),
    DeleteNetAmountType = Table.RemoveColumns(FilterForDiscount,{"Net Amount", "Membership Transaction Type"}),
    AddedNewAmount = Table.AddColumn(DeleteNetAmountType, "Net Amount", each [Membership Promotion Amount]*-1),
    AddedNewType = Table.AddColumn(AddedNewAmount, "Membership Transaction Type", each [Membership Promotion Type]),
    Combine = Table.Combine({ChangeType, AddedNewType})
in
	Combine

 

Copy paste this code to the advanced editor to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

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

6 REPLIES 6
v-lid-msft
Community Support
Community Support

Hi @daniegajohnson ,

 

We can add a custom column and then expand it to the new rows to meet your requirement:

 

 

if [Type]="Membership" and [Membership Promotion type]="Discount" then {[Net Amount], -[Net Amount]} else  {[Net Amount]}

 

 

6.jpg7.jpg 

 

After change the column name and reorder the columns:

 

8.jpg

 

All the queries are here:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1M7ewVNIBsvUNTfUNQUzf1Nyk1KLijMwCkDgQW5oCCbfE3MycSiDDKz8zD0i5F+WXFuTnKSBUK7hkFifnl+aVAGWRmEDdsTrRYIMMDPWBCGyJf0lGahGQNgJicwMg4Zefl0qG6YYGENOhXgGZBPSHkSVRXglKzUstB9KuFQWZRakpCjBbsVtlDvGIEapHUKwAyZki+YYIC6AqjYD+iAUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Lookup ID" = _t, #"Transaction Date" = _t, Type = _t, Quantity = _t, #"Net Amount" = _t, #"Membership Level" = _t, #"Membership Transaction Type" = _t, #"Membership Promotion Name" = _t, #"Membership Promotion type" = _t, #"Membership Promotion Amount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Lookup ID", Int64.Type}, {"Transaction Date", type text}, {"Type", type text}, {"Quantity", Int64.Type}, {"Net Amount", Int64.Type}, {"Membership Level", type text}, {"Membership Transaction Type", type text}, {"Membership Promotion Name", type text}, {"Membership Promotion type", type text}, {"Membership Promotion Amount", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Type]="Membership" and [Membership Promotion type]="Discount" then {[Net Amount], -[Net Amount]} else  {[Net Amount]}),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded Custom",{"Lookup ID", "Transaction Date", "Type", "Quantity", "Custom", "Net Amount", "Membership Level", "Membership Transaction Type", "Membership Promotion Name", "Membership Promotion type", "Membership Promotion Amount"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Net Amount"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "NetAmount"}})
in
    #"Renamed Columns"

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
artemus
Microsoft Employee
Microsoft Employee

What you need to do is to add a new column that is of a table type (one column for each column you are changing, and one row no replacement, 2 rows for an insert)

 

Like:

Custom Row = each #table(type table [New QuantityNet Amount=number, New Membership Transaction Type=text], if [Membership Promotion Type] = "Discount" then {{[QuantityNet Amount], [Membership Transaction Type]}, {-[QuantityNet Amount], "Discount"}} else {{[QuantityNet Amount], [Membership Transaction Type]}})

 

Then just do a table expand on the new column, followed by delete old columns and rename new ones to old names.

Jimmy801
Community Champion
Community Champion

Hello @daniegajohnson 

 

don't get the trigger.

So whenever "Discount" is stated in the promotion type, you would like to have the same line but changed to transaction type "Discount" and the amount *-1?

 

Jimmy

@Jimmy801Yes, the trigger should be ([Type] = "Membership" and [Membership Promotion Type] = "Discount")

 

and then, in the new row the value for Net Amount would be equal to ([Membership Promotion Amount]*-1) from the triggered row and the the value for Type in the new row would be equal to [Memberhsip Promotion Type].

Hello @daniegajohnson 

 

try out this solution. if filters the table and applys the changes requested by you. After that both tables are combined.

let
	Source = #table
	(
		{"Lookup ID","Transaction Date","Type","Quantity","Net Amount","Membership Level","Membership Transaction Type","Membership Promotion Name","Membership Promotion Type","Membership Promotion Amount"},
		{
			{"123456789","43814","Membership"," 1"," 95"," Family"," Join","Groupon Membership Discount","Discount"," 95"},	{"123456780","43828","Membership"," 1"," 95"," Family"," Renew"," Expired Groupon Discount","Discount"," 75"}

					}
	),
    ChangeType = Table.TransformColumnTypes(Source,{{"Membership Promotion Amount", Int64.Type}, {"Net Amount", Int64.Type}}),
    FilterForDiscount = Table.SelectRows
    (
        ChangeType,
        each [Type] = "Membership" and [Membership Promotion Type] = "Discount"
    ),
    DeleteNetAmountType = Table.RemoveColumns(FilterForDiscount,{"Net Amount", "Membership Transaction Type"}),
    AddedNewAmount = Table.AddColumn(DeleteNetAmountType, "Net Amount", each [Membership Promotion Amount]*-1),
    AddedNewType = Table.AddColumn(AddedNewAmount, "Membership Transaction Type", each [Membership Promotion Type]),
    Combine = Table.Combine({ChangeType, AddedNewType})
in
	Combine

 

Copy paste this code to the advanced editor to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

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

Thanks @Jimmy801 ! I made a few modifications, but your code worked! New rows were added with the information I need.

Thanks for answering another one of my questions!

Cheers.

Dan

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Top Kudoed Authors