Split delimited data into rows (and leave the other column blank where extra line is made)

lisaspencer

New Member
Joined
Jul 20, 2020
Messages
21
Office Version
  1. 365
Platform
  1. MacOS
Hi all,

Basically, I get a report of product sales that get sent per transaction (rather than for each product individually). This makes it very hard when using excel to find out what our sales have been on each specific product. If I put the sales list through Power Query and split the column by the "," delimiter, I can put them into another row. But the other column copies the transaction value downwards, so it's adding extra value to that column that doesn't exist. Is there a way for either: the split into rows action not to copy the other column value and leave it blank? Or even figure out what the individual product was sold for, maybe using a VLOOKUP elsewhere? I won't even begin to try and figure out how many were sold based on if there was a discount or not.

Any help is appreciated!

1657278230428.png
 

Attachments

  • 1657277985415.png
    1657277985415.png
    38.8 KB · Views: 6

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The method below isn't foolproof because your data doesn't contain enough information to definitively determine whether an order was sold at discount or full price in some cases. For example, if the sales amount was 9.00 and the full price is 1.00 and the discounted price is 0.90 then you cannot tell whether you sold nine of the product at full price or ten at the discounted price.

The method below will default to full price assumption for the example case where the data is ambiguous. It would be better if your sales table had a column specifying whether the order was at full price or discount. Then there would be no ambiguity.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    Price = Excel.CurrentWorkbook(){[Name="Price"]}[Content],
    tbl = Table.AddColumn(Source, "Custom", each Text.Split([Product],",")),
    tbl1 = Table.AddColumn(tbl, "SumPrice", each List.Accumulate([Custom], 0, (s,c)=> s + Price{[Product = c]}[Price])),
    tbl2 = Table.AddColumn(tbl1, "SumDiscount", each List.Accumulate([Custom], 0, (s,c)=> s + Price{[Product = c]}[Discount])),
    tbl3 = Table.AddColumn(tbl2, "Quantity", each let s = [Sales]/[SumPrice] in if s = Number.RoundDown(s) then s else [Sales]/[SumDiscount]),
    tbl4 = Table.AddColumn(tbl3, "Full/Discount", each let s = [Sales]/[SumPrice] in if s = Number.RoundDown(s) then "Full" else "Discount"),
    tbl5 = Table.RemoveColumns(tbl4,{"Sales", "Product", "SumPrice", "SumDiscount"}),
    tbl6 = Table.RenameColumns(Table.ExpandListColumn(tbl5, "Custom"), {{"Custom","Product"}}),
    tbl7 = Table.AddColumn(tbl6, "Profit Per", each let rcd = Price{[Product = _[Product]]}, q = [Quantity] in if [#"Full/Discount"] = "Full" then rcd[Price]*q else rcd[Discount]*q),
    tbl8 = Table.ReorderColumns(tbl7,{"Product", "Profit Per", "Quantity"}),
    Result = Table.RemoveColumns(tbl8,{"Full/Discount"})
in
    Result

Book1
ABCDEFGHIJK
1ProductSalesProductPriceDiscountProductProfit PerQuantity
2b,c,o,a10.00b1.000.90b1.001
3b1.00c2.001.80c2.001
4c2.00o3.002.70o3.001
5c,o,s10.00a4.003.60a4.001
6c,b,o5.40s5.004.50b1.001
7o10.80c2.001
8s4.50c2.001
9a,s16.20o3.001
10s5.001
11c1.801
12b0.901
13o2.701
14o10.804
15s4.501
16a7.202
17s9.002
18
Sheet2
Cell Formulas
RangeFormula
F2:F6F2=0.9*E2
 
Upvote 0
Solution
are you...... are you a genius? this is sick. I know exactly what you mean by the ambiguity and I may look into getting that but our current sales report is very lacking in information =, this is seriously amazing though, thanks so much
 
Upvote 0
One more question, if I wanted to add one more column in there (preferably on the left but no real problem) I'd like it to be a vlookup column that could go back to the Price table (where I would add Groups too) so for instance I would put 'Red' next to 'c' and 'r' and then 'Yellow' next to 'b' and so on. Then once the power query is loaded, it adds the group that the product belongs to as well. I've tried playing around with it myself but keep coming up with errors as I'm not really sure what I'm doing with power query yet
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
    Price = Excel.CurrentWorkbook(){[Name="Price"]}[Content],
    tbl = Table.AddColumn(Source, "Custom", each Text.Split([Product],",")),
    tbl1 = Table.AddColumn(tbl, "SumPrice", each List.Accumulate([Custom], 0, (s,c)=> s + Price{[Product = c]}[Price])),
    tbl2 = Table.AddColumn(tbl1, "SumDiscount", each List.Accumulate([Custom], 0, (s,c)=> s + Price{[Product = c]}[Discount])),
    tbl3 = Table.AddColumn(tbl2, "Quantity", each let s = [Sales]/[SumPrice] in if s = Number.RoundDown(s) then s else [Sales]/[SumDiscount]),
    tbl4 = Table.AddColumn(tbl3, "Full/Discount", each let s = [Sales]/[SumPrice] in if s = Number.RoundDown(s) then "Full" else "Discount"),
    tbl5 = Table.RemoveColumns(tbl4,{"Sales", "Product", "SumPrice", "SumDiscount"}),
    tbl6 = Table.RenameColumns(Table.ExpandListColumn(tbl5, "Custom"), {{"Custom","Product"}}),
    tbl7 = Table.AddColumn(tbl6, "Profit Per", each let rcd = Price{[Product = _[Product]]}, q = [Quantity] in if [#"Full/Discount"] = "Full" then rcd[Price]*q else rcd[Discount]*q),
    tbl8 = Table.RemoveColumns(tbl7,{"Full/Discount"}),
    tbl9 = Table.AddColumn(tbl8, "Group", each Price{[Product = [Product]]}[Group]),
    Result = Table.ReorderColumns(tbl9,{"Group", "Product", "Profit Per", "Quantity"})
in
    Result

PQ sales price tables.xlsx
ABCDEFGHIJKLM
1ProductSalesGroupProductPriceDiscountGroupProductProfit PerQuantity
2b,c,o,a10.00Redb1.000.90Redb1.001
3b1.00Redc2.001.80Redc2.001
4c2.00Yellowo3.002.70Yellowo3.001
5c,o,s10.00Yellowa4.003.60Yellowa4.001
6c,b,o5.40Blues5.004.50Redb1.001
7o10.80Redc2.001
8s4.50Redc2.001
9a,s16.20Yellowo3.001
10Blues5.001
11Redc1.801
12Redb0.901
13Yellowo2.701
14Yellowo10.804
15Blues4.501
16Yellowa7.202
17Blues9.002
18
Sheet2
Cell Formulas
RangeFormula
G2:G6G2=0.9*F2
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,856
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top