Forum Discussion
Extract Text from Cells in excel
- Mar 09, 2020
That could be done by Power Query, see Sheet2 in attached.
Since not all categories are in each description, and I'm not sure if you have full list of them and they are always in same order, first we convert Description into the list and from that list extract each category based on keyword. That require some coding in M-Script, entire script is
let Source = Excel.CurrentWorkbook(){[Name="Report"]}[Content], TextToList = Table.AddColumn(Source, "Custom", each Text.Split([Description],"#(lf)")), AddCatText = Table.AddColumn( TextToList, "Custom.1", each try List.Single( List.Select([Custom], each Text.StartsWith(_,"[")) ) otherwise null ), AddTolText = Table.AddColumn( AddCatText, "Tolerance", each try Text.AfterDelimiter( List.Single( List.Select([Custom], each Text.StartsWith(_,"Tolerance")) ), "=") otherwise null ), AddCurNAVText = Table.AddColumn( AddTolText, "Current NAV", each try Text.AfterDelimiter( List.Single( List.Select([Custom], each Text.StartsWith(_,"Current")) ), "=") otherwise null ), AddPrevNAVText = Table.AddColumn( AddCurNAVText, "Previous NAV", each try Text.AfterDelimiter( List.Single( List.Select([Custom], each Text.StartsWith(_,"Previous")) ), "=") otherwise null ), AddCheckText = Table.AddColumn( AddPrevNAVText, "Check Value", each try Text.AfterDelimiter( List.Single( List.Select([Custom], each Text.StartsWith(_,"Check")) ), "=") otherwise null ), SplitCategory = Table.SplitColumn( AddCheckText, "Custom.1", Splitter.SplitTextByEachDelimiter({"-"}, QuoteStyle.Csv, false), {"Exception Category", "Exception"} ), ReplaceSemicolon = Table.ReplaceValue( SplitCategory, ";", "", Replacer.ReplaceText, {"Exception Category", "Exception", "Tolerance", "Current NAV", "Previous NAV", "Check Value"} ), RemoveLists = Table.RemoveColumns( ReplaceSemicolon, {"Custom"} ) in RemoveLists
calof1 , thank you for the updated file.
It will be match better if you place data in Tables, but since you already have some other calculations I kept as it is. Resulting table is in columns AD:AK, in brief steps are:
- give names to the range we are needed. Here ColumnsBtoG are related columns, Exceptions R1:S18 and Funds Q1:Q100. If real data requires more space expand them.
- query ColumnsPtoG, split by semicolons, add Index to fix intermediate result in memory and keep sorting;
- extract Code and unpivot other than Index, Code and Entity Code columns;
- split texts on "=" and " NAV ", make other adjustments and pivot table values on texts;
- query Validation and merge previous table with it to extract Validation Check
- query Funds and merge again to extract Fund Names
- load resulting table to the Excel sheet
I skipped some cosmetic and adjustment steps which shall be to do, you may check other details in the file. Table looks as
Thank you again for your help with this, you are a true asset and credit to the excel community.
I will explore this in further detail over the weekend, wish you the best for a safe and nice weekend ahead.
All the best.
Many thanks