Hi FranzV,
Thanks so much for your help.
Unfortunately, I'm completely lost on how to use this code.
I am sorry to have missed the basic explanation on how to implement "M" code in Power Query. I didn't realize you are just starting with this amazing feature. Reza Rad has an
intro for Power Query and an
explanation on custom functions that might help you get the hang of how Power Query works.
One of the things that's confusing for me is that your result layout does not contain my original layout. So I can't see how everything is matching up.
I updated the Dropbox file to look very similar to your OneDrive file. I included the Metric 1 - 4 columns, though I do not know if or how you use them.
There are 3 sheets in my workbook. The first is an Excel-Only solution with a simplified version of your formulas. I tested it with 10 category columns and ~500k rows and it worked fine (I removed the extra rows to keep the file's size decent). It looks llike this:
[B]Excel 2016 (Windows) 64 bit[/B] [SIZE=2][TABLE="class: head"]
<tbody>[TR="bgcolor: #888888"]
[TH][/TH]
[TH][COLOR=#FFFFFF]A[/COLOR][/TH]
[TH][COLOR=#FFFFFF]B[/COLOR][/TH]
[TH][COLOR=#FFFFFF]C[/COLOR][/TH]
[TH][COLOR=#FFFFFF]D[/COLOR][/TH]
[TH][COLOR=#FFFFFF]E[/COLOR][/TH]
[TH][COLOR=#FFFFFF]F[/COLOR][/TH]
[TH][COLOR=#FFFFFF]G[/COLOR][/TH]
[TH][COLOR=#FFFFFF]H[/COLOR][/TH]
[TH][COLOR=#FFFFFF]I[/COLOR][/TH]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]1[/B][/COLOR][/CENTER]
[/TD]
[TD][B]Keyword Categorizer[/B][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]2[/B][/COLOR][/CENTER]
[/TD]
[TD][I][COLOR=#C00000]Excel only solution[/COLOR][/I][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]3[/B][/COLOR][/CENTER]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]4[/B][/COLOR][/CENTER]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="bgcolor: #D9D9D9"][B][COLOR=#00B050]Symptom[/COLOR][/B][/TD]
[TD="bgcolor: #D9D9D9"][B][COLOR=#00B050]Cause[/COLOR][/B][/TD]
[TD="bgcolor: #D9D9D9"][B][COLOR=#00B050]Treatment[/COLOR][/B][/TD]
[TD="bgcolor: #D9D9D9"][B][COLOR=#00B050]What[/COLOR][/B][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]5[/B][/COLOR][/CENTER]
[/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Keyword[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Metric 1[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Metric 2[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Metric 3[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Metric 4[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Topic 1[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Topic 2[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Topic 3[/COLOR][/B][/TD]
[TD="bgcolor: #5B9BD5"][B][COLOR=#FFFFFF]Topic 4[/COLOR][/B][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]6[/B][/COLOR][/CENTER]
[/TD]
[TD]migraine headache symptoms[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Symptom[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]7[/B][/COLOR][/CENTER]
[/TD]
[TD]migrane headache symptoms[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Symptom[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]8[/B][/COLOR][/CENTER]
[/TD]
[TD]what causes severe migraine headaches[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Cause[/TD]
[TD][/TD]
[TD]What[/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]9[/B][/COLOR][/CENTER]
[/TD]
[TD]what is migraine headache symptoms[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Symptom[/TD]
[TD][/TD]
[TD][/TD]
[TD]What[/TD]
[/TR]
[TR]
[TD="bgcolor: #888888"][CENTER][COLOR=#FFFFFF][B]10[/B][/COLOR][/CENTER]
[/TD]
[TD]migraine causes[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Cause[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[/SIZE][TABLE="class: grid"]
<tbody>[TR]
[TD]Sheet: [B]ExcelOnly[/B][/TD]
[/TR]
</tbody>[/TABLE]
I changed the LOOKUP() for an ISERROR() that will return FALSE whenever the word just above the header (green on grey) is found in the Keyword column. The outer if will return the searchword when it is found an empty string when it isn't. There is no need for the Topic Tables with this formula.
Worksheet Formulas
Cell | Formula |
---|
F6 | =IF(ISERROR(SEARCH(F$4,[@Keyword])),"",F$4) |
---|
<tbody>
</tbody> |
<tbody>
</tbody>
I have no clue why you were using the BigNumber LOOKUP.
The second sheet holds a table with the Keyword column and the Metric columns called 'tKeywords' that is used as a source for the PQ query.
The third has a table called 'tPowerQuery' that is loaded into the worksheet after adding the Topic columns.
I'm assuming this is the result you were looking for. If it isn't, I must have understood you incorrectly and would need further details that might help me help you.
Where do I create the function that adds the columns, from Excel or Power Query? And do I need to set up the topic tables like in my sample data?
Now on to the Power Query explanation.
To create a new query using a table as a source, simply select any cell within the table and click 'From Table' in the 'Get & Transform' group of the 'Data' tab in the Menu Ribbon. That will automatically open the Power Query Editor window and display the data in the table without any formatting. From there, you can use the options in the Ribbon to transform your data, add columns, filter rows, etc.
The user interface is very intutive and I would encourage you to play around with your data to grasp the capabilities of PQ. As you apply new transformation steps to your data, you will see a list of them on the right side of the screen. If you select any of the steps you can preview how the infromation looks right after applying that step.
Sometimes you need to repeat certain operations over and over and it becomes tedious and error prone to keep rewriting the same formulas every time. That's when Custom Functions are especially useful because they let you reuse a piece of code in other queries and can take in arguments that make them quite flexible.
The most common way to create a function is by applying the desired steps through the user interface and then go to the Advanced Editor to tweak the code turning it to a function. That is how I created the function for adding the Topic columns. If the number of categories will be changing oftenly, it might need some tweaking to use a table as a source for searchwords and add all the columns at once.
After that, what do I need to do to invoke?
So simple steps would be tremendously helpful.
Once the function is ready, you can return to your original query and click on
fx just left of the formula bar. This will add a new step to the query whose default setting is '= PreviousStep' where PreviousStep is the name of the queries previous step. You can edit the formula and call the custom function just as you would call an Excel function, including the parameters inside parenthesis.
You have to do this once for each Topic column, changing the header and the searchword.
PS. You can fin great posts on Power Query of you follow the links in my signature for Imke Feldmann, Ken Puls, or Chris Webb.