I would suggest to use the following function GetFieldValue to extract the text after the fields like "First Name", "Last Name" etc.
(fieldName as text, inputText as text) =>
let
// Split the input text into lines
Lines = Text.Split(inputText, "#(lf)"),
// Find the line that starts with the field name followed by ":"
MatchingLine = List.First(List.Select(Lines, each Text.StartsWith(_, fieldName & ":"))),
// Extract the value after the colon and optional space
Value = Text.Trim(Text.AfterDelimiter(MatchingLine, ":"))
in
Value
You can add a user defined column for each field and call the function like that in case "First Name"
= Table.AddColumn(pqStep, "FirstName", each GetFieldValue("First Name", [Body.TextBody]))
pqStep is the name of the previous Power Query step.
Assuming your workbook looks like you posted , complete M-Code
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
pqStep = Table.TransformColumnTypes(Source,{{"DateTimeReceived", type datetime}, {"Subject", type text}, {"Body.TextBody", type text}}),
addFirstName = Table.AddColumn(pqStep, "FirstName", each GetFieldValue("First Name", [Body.TextBody])),
addLastName= Table.AddColumn(addFirstName, "LastName", each GetFieldValue("Last Name", [Body.TextBody])),
addEmail = Table.AddColumn(addLastName, "Email", each GetFieldValue("Email", [Body.TextBody])),
addCostCenter = Table.AddColumn(addEmail, "CostCenter", each GetFieldValue("Cost Center", [Body.TextBody]))
in
addCostCenter
Another approach is splitting and filtering the input accordingly.
let
// Sample input text
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
// Split the text into rows by line feed
SplitLines = Table.ExpandListColumn(
Table.TransformColumns(Source, {
"Body.TextBody", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv)
}), "Body.TextBody"
),
FilterRows = Table.SelectRows(SplitLines, each ([Body.TextBody] <> "")),
SplitFields = Table.SplitColumn(FilterRows, "Body.TextBody", Splitter.SplitTextByEachDelimiter({":"}, QuoteStyle.None, false), {"Body.TextBody.1", "Body.TextBody.2"}),
FilterFields = Table.SelectRows(SplitFields, each [Body.TextBody.1] = "First Name" or [Body.TextBody.1] = "Last Name" or [Body.TextBody.1] = "Email" or [Body.TextBody.1] = "Cost Center"),
Pivot = Table.Pivot(FilterFields, List.Distinct(FilterFields[Body.TextBody.1]), "Body.TextBody.1", "Body.TextBody.2")
in
Pivot