Power Query manual M code modification - help needed

Matt Allington

MrExcel MVP
Joined
Dec 18, 2014
Messages
1,629
Hi

I am trying to write a recursive query that will search for strings (column F) in a single column (column C). If the string is found, then a new column should create the category (Column E).

qq.png


I have produced a sample workbook and written the starting code. Currently I have a single line of code that searches for "Apples" and adds the word "Fruit" into a new column. Now I am thinking that I should be able to manually alter the query I have using the "_" operator and pass a list of search terms (Column F) to the existing single step and have it return a table of all the matching values (sourced from column E) into a single column. I think this should be possible (Marcel, Imke)? however it is beyond my current understanding.

I would appreciate any help, even if you can tell me it can't be done.

Matt

Sample http://xbi.com.au/files/forum_question.xlsx
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi Matt,

Is this any help?

Created a new Query called "Output"

Code:
let
    Source = Subst_table,
    #"Added Custom1" = Table.AddColumn(Source, "AllContents", each Data),
    #"Expanded AllContents" = Table.ExpandTableColumn(#"Added Custom1", "AllContents", {"Contents"}, {"Contents"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded AllContents", each Text.Contains([Contents], [Search])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Search"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Category", type text}, {"Contents", type text}})
in
    #"Changed Type"

At the moment this will return multiple rows for multiple matches, would concatenation be more desirable?


Excel 2010
BC
2IDContents
31Apples, Apples anyone?
42Oranges make good juice
53Big Steaks are Juicy
64I like Sausages
75Bananas and Sausages
86apples for sale
9
Sheet1



Excel 2010
FG
1CategoryContents
2FruitApples, Apples anyone?
3Fruitapples for sale
4FruitOranges make good juice
5MeatBig Steaks are Juicy
6MeatI like Sausages
7MeatBananas and Sausages
8FruitBananas and Sausages
9
Sheet3
 
Last edited:
Upvote 0
Too slow to edit.

Updated to concatenate multiple results

Code:
let
    Source = Subst_table,
    #"Added Custom1" = Table.AddColumn(Source, "AllContents", each Data),
    #"Expanded AllContents" = Table.ExpandTableColumn(#"Added Custom1", "AllContents", {"Contents"}, {"Contents"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded AllContents", each Text.Contains([Contents], [Search])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Search"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Contents"}, {{"Grouped", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows" , "Result", each Text.TrimEnd(List.Accumulate([Grouped][Category], "", (state, current) => state & current & ", "), {"," , " "})),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Grouped"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns1",{{"Contents", type text}, {"Result", type text}})
in
    #"Changed Type"


Excel 2010
FG
1ContentsResult
2Apples, Apples anyone?Fruit
3apples for saleFruit
4Oranges make good juiceFruit
5Big Steaks are JuicyMeat
6I like SausagesMeat
7Bananas and SausagesMeat, Fruit
8
Sheet3
 
Upvote 0
Ok! This is my solution
Code for FirstAttempt:
Code:
let
    Source = Data,
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Subst_table ),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Category", "Search"}, {"Category", "Search"}),
    #"Added Conditional Column" = Table.AddColumn(#"Expanded Custom", "Custom", each if Text.Contains([Contents], [Search]) then "1" else "0" ),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = "1")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom", "Search"})
in
    #"Removed Columns"
 
Last edited:
Upvote 0
Thanks to you both citizenbh and Comfy. I hadn't thought about the approach you both have suggested but I do like it. This will solve my issue and I will use it. I would still be interested in knowing if a solution using my idea is possible if anyone else would like to contribute.
 
Upvote 0

Forum statistics

Threads
1,225,726
Messages
6,186,675
Members
453,368
Latest member
xxtanka

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