Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
vscott
New Member

How to configure the Body.TextBody to import rows from table in an email body

I am struggling to configure an expanded column (Body.TextBody)  in a mail table to pick up each table row in the email message to create a record in Power BI.  See a picture of the body. 

 

 

vscott_1-1678681193760.png

 

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @vscott 

 

You may try my solution as below.

 

First I assume your email body is similar to below. I assume that there is only one table in each email's body and the ending Sign-Off is "BR", so we need to extract the text starting from "File Created at ..." and ending before "BR". 

vjingzhang_0-1678858744030.png

We can add a custom column with below code to get the table data. 

Text.Range([Body.TextBody], Text.PositionOf([Body.TextBody], "File Created at (EST)"), Text.PositionOf([Body.TextBody], "BR") - Text.PositionOf([Body.TextBody], "File Created at (EST)"))

vjingzhang_1-1678859199870.png

Then split the "Table Data" column by delimiter #(lf) and split it to rows.

vjingzhang_2-1678859327329.pngvjingzhang_3-1678859402247.png

Then remove the redundant rows to keep only the highted rows as above.  

= Table.SelectRows(#"Split Column by Delimiter", each ([Table Data] <> "" and [Table Data] <> "#(cr)" and (not Text.Contains([Table Data], "File Created at (EST)"))))

Split "Table Data" column by the left-most delimiter "   " into columns. (Or use other split methods per your need.)

vjingzhang_4-1678859664760.pngvjingzhang_5-1678859791850.png

At last, trim the second column after splitting and rename both columns. Then you will have below result. 

vjingzhang_6-1678859898199.png

 

The full code is

let
    Source = Exchange.Contents("xxxxxxxxxxxx@xxxxxxx.com"),
    Mail1 = Source{[Name="Mail"]}[Data],
    .......................,
    #"Expanded Body" = Table.ExpandRecordColumn(#"Kept First Rows", "Body", {"TextBody"}, {"Body.TextBody"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Body",{"Subject", "Body.TextBody"}),
    
    /* Replace above code with your source data */
    
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Table Data", each Text.Range([Body.TextBody], Text.PositionOf([Body.TextBody], "File Created at (EST)"), Text.PositionOf([Body.TextBody], "BR") - Text.PositionOf([Body.TextBody], "File Created at (EST)"))),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Custom", {{"Table Data", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Table Data"),
    #"Filtered Rows2" = Table.SelectRows(#"Split Column by Delimiter", each ([Table Data] <> "" and [Table Data] <> "#(cr)" and (not Text.Contains([Table Data], "File Created at (EST)")))),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Filtered Rows2", "Table Data", Splitter.SplitTextByEachDelimiter({"   "}, QuoteStyle.Csv, false), {"Table Data.1", "Table Data.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter1",{{"Table Data.2", Text.Trim, type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Trimmed Text",{{"Table Data.1", "File Created at (EST)"}, {"Table Data.2", "File path"}})
in
    #"Renamed Columns"

 

Hope this solution helps! 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

1 REPLY 1
v-jingzhang
Community Support
Community Support

Hi @vscott 

 

You may try my solution as below.

 

First I assume your email body is similar to below. I assume that there is only one table in each email's body and the ending Sign-Off is "BR", so we need to extract the text starting from "File Created at ..." and ending before "BR". 

vjingzhang_0-1678858744030.png

We can add a custom column with below code to get the table data. 

Text.Range([Body.TextBody], Text.PositionOf([Body.TextBody], "File Created at (EST)"), Text.PositionOf([Body.TextBody], "BR") - Text.PositionOf([Body.TextBody], "File Created at (EST)"))

vjingzhang_1-1678859199870.png

Then split the "Table Data" column by delimiter #(lf) and split it to rows.

vjingzhang_2-1678859327329.pngvjingzhang_3-1678859402247.png

Then remove the redundant rows to keep only the highted rows as above.  

= Table.SelectRows(#"Split Column by Delimiter", each ([Table Data] <> "" and [Table Data] <> "#(cr)" and (not Text.Contains([Table Data], "File Created at (EST)"))))

Split "Table Data" column by the left-most delimiter "   " into columns. (Or use other split methods per your need.)

vjingzhang_4-1678859664760.pngvjingzhang_5-1678859791850.png

At last, trim the second column after splitting and rename both columns. Then you will have below result. 

vjingzhang_6-1678859898199.png

 

The full code is

let
    Source = Exchange.Contents("xxxxxxxxxxxx@xxxxxxx.com"),
    Mail1 = Source{[Name="Mail"]}[Data],
    .......................,
    #"Expanded Body" = Table.ExpandRecordColumn(#"Kept First Rows", "Body", {"TextBody"}, {"Body.TextBody"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Body",{"Subject", "Body.TextBody"}),
    
    /* Replace above code with your source data */
    
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Table Data", each Text.Range([Body.TextBody], Text.PositionOf([Body.TextBody], "File Created at (EST)"), Text.PositionOf([Body.TextBody], "BR") - Text.PositionOf([Body.TextBody], "File Created at (EST)"))),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Custom", {{"Table Data", Splitter.SplitTextByDelimiter("#(lf)", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Table Data"),
    #"Filtered Rows2" = Table.SelectRows(#"Split Column by Delimiter", each ([Table Data] <> "" and [Table Data] <> "#(cr)" and (not Text.Contains([Table Data], "File Created at (EST)")))),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Filtered Rows2", "Table Data", Splitter.SplitTextByEachDelimiter({"   "}, QuoteStyle.Csv, false), {"Table Data.1", "Table Data.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter1",{{"Table Data.2", Text.Trim, type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Trimmed Text",{{"Table Data.1", "File Created at (EST)"}, {"Table Data.2", "File path"}})
in
    #"Renamed Columns"

 

Hope this solution helps! 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors