Forum Discussion

calof1's avatar
calof1
Iron Contributor
Mar 09, 2020
Solved

Extract Text from Cells in excel

Hi All.   I have a report which is run daily and from this we follow up each item if required. To help make the process easier to follow up i wish to separate the information on the cell into small...
  • SergeiBaklan's avatar
    Mar 09, 2020

    calof1 

    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

Resources