Insert rows based on text in column with Power Query

EnnyKraft

New Member
Joined
Apr 16, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I need to create a file my database publishing software can use but the export from the database is not exactly what I need. I have created a sample file so that you can understand better has to be changed. This is what the export looks like:
Screenshot1.png

But it should look like this:
Screenshot2.png

Filling the empty cells in the Products is easy. But I haven't got a clue how to convert those rows where Groups and Subgroups aren't empty into rows of their own. My first guess was using Table.InsertRows but I can't figure out how to use it since the position isn't regular by any means.

If anybody would like to download my sample file, here's the link:
Download from Box.com

I would be really grateful if someone could help me since the original file contains thousands of rows and doing this manually would take ages.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I downloaded your file and named the range that contains your original information "RawData".

If you pull that into your Power Query editor then paste the following script into the "Advanced Editor" area, you should get what you need.

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="RawData"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Subcategory", type text}, {"Product", type text}, {"Quantity", type text}, {"SKU", Int64.Type}}),
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Product"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Filled Down", {"Quantity", "SKU"}, "Attribute", "Value"),
    #"Reordered Columns" = Table.ReorderColumns(#"Unpivoted Other Columns",{"Attribute", "Value", "Quantity", "SKU"}),
    #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Product"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Revised Quantity", each if Text.Contains([Product], "Product") then [Quantity] else null),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom", "Revised SKU", each if Text.Contains([Product], "Product") then [SKU] else null),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"Quantity", "SKU"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Removed Columns1",{{"Revised Quantity", "Quantity"}, {"Revised SKU", "SKU"}})
in
    #"Renamed Columns1"
 
Upvote 0
a bit shorter
sku.png

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FillD = Table.FillDown(Source,{"Product"}),
    UOSC = Table.Unpivot(FillD, {"Category", "Subcategory", "Product"}, "Attribute", "Value"),
    IF1 = Table.AddColumn(UOSC, "Custom", each if Text.Contains([Value], "Group") then "" else if Text.Contains([Value], "group") then "" else [Quantity]),
    IF2 = Table.AddColumn(IF1, "Custom.1", each if Text.Contains([Value], "Group") then "" else if Text.Contains([Value], "group") then "" else [SKU]),
    TSC = Table.SelectColumns(IF2,{"Value", "Custom", "Custom.1"}),
    Rename = Table.RenameColumns(TSC,{{"Value", "Product"}, {"Custom", "Quantity"}, {"Custom.1", "SKU"}})
in
    Rename
btw you've error in your source data : SubgroubB ==> SubgroupB
 
Last edited:
Upvote 0
or a bit bit shorter :biggrin: ?
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FillD = Table.FillDown(Source,{"Product"}),
    UOSC = Table.Unpivot(FillD, {"Category", "Subcategory", "Product"}, "Attribute", "Value"),
    IF1 = Table.AddColumn(UOSC, "Custom", each if Text.Contains([Value], "roup") then "" else [Quantity]),
    IF2 = Table.AddColumn(IF1, "Custom.1", each if Text.Contains([Value], "roup") then "" else [SKU]),
    TSC = Table.SelectColumns(IF2,{"Value", "Custom", "Custom.1"}),
    Rename = Table.RenameColumns(TSC,{{"Value", "Product"}, {"Custom", "Quantity"}, {"Custom.1", "SKU"}})
in
    Rename
 
Upvote 0
I noticed my mistake so here is a proper result
sku.png


Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    FillD = Table.FillDown(Source,{"Product"}),
    UOSC = Table.Unpivot(FillD, {"Category", "Subcategory", "Product"}, "Attribute", "Value"),
    IF1 = Table.AddColumn(UOSC, "Custom", each if Text.Contains([Value], "roup") then "" else [Quantity]),
    IF2 = Table.AddColumn(IF1, "Custom.1", each if Text.Contains([Value], "roup") then "" else [SKU]),
    RAR = Table.AlternateRows(IF2,16,1,3),
    TSC = Table.SelectColumns(RAR,{"Value", "Custom", "Custom.1"}),
    Rename = Table.RenameColumns(TSC,{{"Value", "Product"}, {"Custom", "Quantity"}, {"Custom.1", "SKU"}})
in
    Rename
 
Upvote 0
Wow! That worked perfectly. When entering new date the refresh worked without a glitch. Thank you so much! ? Now all I need to do is add an extra step which adds some identifier to the Groups and Subgroups texts since my real data aren't named with any logical pattern and my understanding is that the IF-steps need to find a pattern for the names.
Thank you very, very much! This has helped me a lot!!!
 
Upvote 0
after source (as first step) add prefix ID to each column you need
Transform - Format - Add Prefix
or , as you said a few IFs ?
 
Last edited:
Upvote 0
Managed to edit your code for my real data and add the necessary steps. It worked! This was the first time writing M code since I'm a graphics designer and not a programmer so it was great to learn how to do this. The only part I didn't understand was the Table.AlternateRows. What does that do? Quite often the M formula language guide by Microsoft leaves me totally baffled.
Thank you very much! I am really grateful for your fantastic help!
 
Upvote 0
sandy, are you sure that everything is ok with your query? Try this data below. please

CategorySubcategoryProductQuantitySKU
GroupAProduct15 pcs48956
10 pcs65535
GroupBSubgroupAProduct220 pcs39512
Product350 pcs57262
Product41 pcs81068
Product51 pcs29621
5 pcs61918
10 pcs24788
GroupCSubgroupAProduct61 pcs71058
Product75 pcs68562
10 pcs68948
6 pcs55124
11 pcs69854
GroupCSubgroubBProduct85 pcs58184
10 pcs44528
GroupDProduct1235 pcs58184
10 pcs44528
 
Upvote 0

Forum statistics

Threads
1,223,794
Messages
6,174,643
Members
452,575
Latest member
Fstick546

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