-1

Hello stackoverflow community! This is my first post on here so be gentle!

I have been working on trying to automate the naming system for the creation of new products at the company I work for. We come out with new SKUs pretty often so I want to make it as easy as possible for new items to work with our current systems.

Here is what I would like to accomplish: There are different product types, sizes, flavors, and ratios that we have. I want to be able to generate a list of all of the different products we make based off of information entered into a table. For example, if we want to add a bath bath bomb we might have a small one and a large one, lets say a 50g and a 150g. We might also have a couple different smells, such as one with lavender and one with myrrh. So based off of information entered like this:

Name Scent 1 Scent 2 Size 1 Size 2
Bath Bomb lavender Myrrh 50g 150g

I would like to generate a list like this:

Bath Bomb, Lavender, 50g

Bath Bomb, Lavender, 150g

Bath Bomb, Myrrh, 50g

Bath Bomb, Myrrh, 150g

(It doesn't have to be in the same order shown above)

The problem I have is as follows: Some of our products might have 2 sizes where others might have more, same with flavors/ratios. Due to this, the table I have has many blank spots (since Size 2 might not be filled on items with only one size) I am currently experimenting with creating a matrix of every possible combination based on a row but I am not sure how to do this without spending hours setting up TextJoin functions using ISBlank to get rid of any rows that are trying to reference empty cells.

Any help would be greatly appreciated!

Update: To better show what I am trying to do, here is an image that shows an example of a table with a list I manually created below it:

Example Table and List

  • Just include the empty cells in the range and set the [`Ignore_Empty`](https://support.microsoft.com/en-us/office/textjoin-function-357b449a-ec91-49d0-80c3-0e8fc845691c) parameter to TRUE? – chris neilsen Jun 07 '21 at 19:00
  • Hi Chris thank you for replying! My issue is not so much getting rid of the blanks, its generating the list based on all the criteria entered (one for each size and scent for example) – Orion Hunt Jun 07 '21 at 20:13
  • Also, I am hoping to make this as automated as possible. If I have to select the appropriate column manually for every entry it kind of defeats the purpose. – Orion Hunt Jun 07 '21 at 20:18

1 Answers1

0

If I have understood the objective well, you are trying to do a nested unpivot (normalize or melt). You are nesting two attributes: Scents and Sizes, but it sounded like there could be more - if so, this method can be generalized in the following logic:

You are unpivoting Sizes by Scents and then unpivoting that result by Name with an exception that Scents can be blank. This will be a little more clear as we go.

If this is the input data:

input table

Then this would be the expected output result where the blank sizes are removed:

target output

Note that Rust Remover has no scents/flavors, but this is included, whereas the blank size rows are removed as per the rules above.

In this case, I see two ways to do this (excluding VBA):

  • PowerQuery - requires Excel for Windows 2010+
  • Dynamic Array Formula - requires Excel 2019 or 365 for either Windows or Mac

As the PowerQuery is very straight forward, I will propose that first. If it will not work for you, then the Dynamic Array method could be interesting, if you have the right Excel version, but it takes more time to build and test (on my side).

Power Query Method

This may look intimidating, but it takes very little time, just lots of clicking. 5 mins - tops

First, convert your table above into an Excel Table - you can click anywhere inside of it and press CTRL t. It will ask you if your table has headers - yes, it does.

make excel table

Now go to the Table Design tab and on the left you can rename your table. I will name it prodTable.

rename table

Now go to the Data tab and click From Range/Table.

load to query

This will open the Power Query editor which should look like this:

power query editor

I was just going to paste the PowerQuery M language script here and let you put it in your machine, but it would fail if your table does not match my mock-up, so we need to show the steps instead.

1) Unpivot Scents

Hold CTRL and select the scent columns and then choose Unpivot Columns in the Transform tab.

up scents

Double click the Value header on that column and rename it to "Scents". Right Click on the Attributes column and remove it.

enter image description here

2) Unpivot Sizes

Now repeat those steps for Sizes. Hold CTRL and select the size columns and then choose Unpivot Columns in the Transform tab.

enter image description here

Double click the Value header on that column and rename it to "Sizes". Right Click on the Attributes column and remove it.

enter image description here

At this point, you have what you described,

enter image description here

but you said that you wanted to have a text joined name, so let's add that here:

3) Create a Merged Name

Hold CTRL and select the three columns in the order that you want them to appear in the text join. Then select Merge Columns in the Add Column tab.

enter image description here

It will ask you what you want for a separator - I just used the pipe character. It will also ask you what you want to name this column - I chose SKU Name.

enter image description here

enter image description here

4) Load it into Excel

Now, you just need to move this back into Excel. Go to the Home tab and choose Close and Load to.

enter image description here

Then tell it that you want to load it into a new worksheet - that's easiest.

enter image description here

Now you will have a new worksheet called prodTable and it will have your transformed table of products. This is all a one-time set up. From now on, as users create new combinations, you would only need to refresh the table by right-clicking it and Refresh - PowerQuery will execute the M script that you just made and will generate all of the SKU's based on the inputs from the users.

enter image description here

If you have Excel for Mac or you want it to be based on a formula, then shout out in the comments. The only way that I know of to do this with a Dynamic Array required Excel 2019 because it will rely heavily on SEQUENCE and LET. I've already done unpivots/melts, but nested melts would take some thinking.

One More Approach

Here is an M script that you can load into the PQ Advanced Editor that will give you basically what you want where there are no scents/flavors, but there are sizes. It does not handle all of the nested attributes that you want and, unfortunately, it would have to be rewritten each time you add an attribute, but it give you an idea of the method:

let
   Source = Excel.CurrentWorkbook(){[Name="prodTable"]}[Content],
   #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Scent 1", type text}, {"Scent 2", type text}, {"Size 1", type text}, {"Size 2", type text}}),
   #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,"WASNULL",Replacer.ReplaceValue,{"Scent 1"}),
   #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"Name", "Size 1", "Size 2"}, "Attribute", "Value"),
   #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Value", "Scents"}}),
   #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Attribute"}),
   #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Removed Columns", {"Name", "Scents"}, "Attribute", "Value"),
   #"Renamed Columns1" = Table.RenameColumns(#"Unpivoted Columns1",{{"Value", "Sizes"}}),
   #"Removed Columns1" = Table.RemoveColumns(#"Renamed Columns1",{"Attribute"}),
   #"Replaced Value1" = Table.ReplaceValue(#"Removed Columns1","WASNULL",null,Replacer.ReplaceValue,{"Scents"}),
   #"Inserted Merged Column" = Table.AddColumn(#"Replaced Value1", "SKU Name", each Text.Combine({[Name], [Scents], [Sizes]}, "|"), type text)
in
    #"Inserted Merged Column"

I tried to make the attributes dynamic, but this becomes hard without doing a lot of ETL. Somehow your objective seems within reach. Your idea of generating all possible combinations could work, but it will take a lot of set up in order to allow for the rules that you want in place.

mark fitzpatrick
  • 3,162
  • 2
  • 11
  • 23
  • Wow this looks incredible! I will be testing this out over the next few days thank you Mark! – Orion Hunt Jun 07 '21 at 21:08
  • I would love to see the Array Formula as well if it isn't too much trouble. I use Excel for Microsoft 365 and my version is 2105. I have used Array formulas in the past and I believe that would be best for my purposes. I will still be testing out the PowerQuery method regardless. – Orion Hunt Jun 07 '21 at 21:14
  • I have tested this out a bit and it appears that Unpivot removes rows if any of the columns selected contain null. Because of this I do not believe the PowerQuery method done exactly as shown would return all of the items I need. This is because there are products that do not have a flavor/scent associated and these SKUs are removed entirely. – Orion Hunt Jun 07 '21 at 22:48
  • Ahh - OK, that's easy to solve in PQ. DA would do the same as what is shown in this answer - and worse, my first DA approach does not filter blanks. To make sure I am understanding the requirement, there is a rule that **scents/flavors are treated as blank if there are no scents/flavors**. I will add a table the mock-up data and expected results. Can you validate that these are what you want? If so, then it is quick work in PQ. – mark fitzpatrick Jun 08 '21 at 05:59
  • OK - so I added the tables at the top. The difference here is in the Rust Remover - in the first answer, it is not included whereas in the target solution, it should be. Is that right? If so, it's easy to do that in PQ. – mark fitzpatrick Jun 08 '21 at 06:11
  • Hi Mark, in order to clarify this please take a look at the image I added at the end of my question. It shows a better example of what I am trying to do. As you can see, there are many blank fields. For items with just one Scent and Ratio they simply join them (Such as the Cream). Looking at an item with multiple ratios (Such as the oil) returns an item with each. Lastly, items with multiple sizes and ratios have every combination (Such as the tablet). One possibility I forgot to add is that an item may not have a ratio at all, in that case it would just need an item for each size – Orion Hunt Jun 08 '21 at 15:27
  • I would prefer not to have to use PowerQuery as it appears to have limited flexibility. If I were to change a header name for example it looks like I would have to go through PowerQuery to change it otherwise the connections between any tables will remain broken. I am hoping to make this as automated and flexible as possible so that my coworkers can add and change information without having to learn to use PowerQuery. – Orion Hunt Jun 08 '21 at 16:49
  • Actually, these new requirements are pushing a DA based solution out of the realm of possibility. Here is a non-nested unpivot that actually was the trigger for me to participate in Stack. I was looking for this solution and could not find it so I wrote it and then posted it. It gives you some idea of the complexity involved: https://stackoverflow.com/questions/32115219/unpivot-an-excel-matrix-pivot-table/67545928#67545928 And this treats blanks as 0's, so that's even more lines of massaging. Your complex rules & triple nesting will make this an impossible stretch. – mark fitzpatrick Jun 08 '21 at 17:50
  • Changing headers can be managed in PQ. But you have to think about some data control rules: e.g., *Header Names are Restricted to* `Scents x, Sizes x, Ratios x, Flavors x, ...` Another approach that might be more flexible is a rule: *1) Only Name is fixed - every other Header Name is an attribute AND 2) All Common Attributes must start with a common word and number like Size 1, Size 2, Flavor 1, Flavor 2.* PQ will definitely respond to such rules. The ETL steps will become complex, but not rocket science. PQ is one of the easiest and most powerful tools in modern Excel. – mark fitzpatrick Jun 08 '21 at 18:03
  • Oh wow I was worried that might be the case. I may have to use PowerQuery to solve this issue after all. One workaround that I was considering before posting this question was to create a large matrix that contains every possible combination of sizes, scents, ect. with criteria to return blank cells if certain values are not entered (For example every SKU has a size but not necessarily a scent). However when I started writing this out it seemed like there may be a much better solution to creating this matrix and breaking it down into a list. – Orion Hunt Jun 08 '21 at 18:37
  • I will be doing more experimenting with PowerQuery as well as trying to create a matrix of my own. Thank you for taking the time to help me out Mark! I'll post an update on here when I make some progress. – Orion Hunt Jun 08 '21 at 18:39
  • Hey @Andrew - I added a small M script that that will generate the result of allowing SKU's that only have sizes if scents are blank. This is just a demo - it does not look at all of the other attributes such as ratios, but the techniques would be the same. Unfortunately, I cannot see a way to make it adaptive to new attributes without a lot of PQ work. Another approach is good old VBA. Maybe someone else can bring a bright approach. – mark fitzpatrick Jun 08 '21 at 20:39