Forum Discussion

LoMorrissey's avatar
LoMorrissey
Regular Visitor
1 year ago
Solved

Extracting HTML Code from Power Bi

I am attempting to extract HTML text by creating a custom column but keep the rows with regular text at the same time. I have used this code to remove the HTML text but am missing the part where it keeps the rows with regular text:

 

= if Text.StartsWith([Final Decision Notes], "<") then
Table.Combine({
Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "div"}}),
Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "br"}})
})
else
#table({"Final Decision Notes"}, {{ [Final Decision Notes] }})

 

What am I missing? 

  • Hi LoMorrissey 


    Welcome to the Microsoft Fabric Community Forum.
     

    The issue observed in Power BI, involving the extraction of HTML content while retaining plain text rows, is primarily due to inconsistencies in the conditional logic used within the custom column formula. The current implementation utilizes Html.Table to parse HTML content when the text begins with an HTML tag indicator (such as <), but fails to maintain a consistent output structure for rows containing plain text. This discrepancy can lead to schema misalignment during data transformation.

     

    To address this, it is recommended to ensure that both HTML and non-HTML rows are returned in a uniform table format. This can be achieved by wrapping plain text rows in a table structure that mirrors the output of the HTML parsing logic.

     

    = if Text.StartsWith([Final Decision Notes], "<") then
    
        Table.Combine({
    
            Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "div"}}),
    
            Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "br"}})
    
        })
    
    else
    
        #table({"Final Decision Notes"}, {{ [Final Decision Notes] }})

     

    For reference:
    Html.Table - PowerQuery M | Microsoft Learn


    Thank you for being part of Fabric Community Forum.

    Regards,
    Karpurapu D,
    Microsoft Fabric Community Support Team.


7 Replies

  • Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
    Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
    Please show the expected outcome based on the sample data you provided.

    Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
    Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

  • v-karpurapud's avatar
    v-karpurapud
    Community Support

    Hi LoMorrissey 


    Welcome to the Microsoft Fabric Community Forum.
     

    The issue observed in Power BI, involving the extraction of HTML content while retaining plain text rows, is primarily due to inconsistencies in the conditional logic used within the custom column formula. The current implementation utilizes Html.Table to parse HTML content when the text begins with an HTML tag indicator (such as <), but fails to maintain a consistent output structure for rows containing plain text. This discrepancy can lead to schema misalignment during data transformation.

     

    To address this, it is recommended to ensure that both HTML and non-HTML rows are returned in a uniform table format. This can be achieved by wrapping plain text rows in a table structure that mirrors the output of the HTML parsing logic.

     

    = if Text.StartsWith([Final Decision Notes], "<") then
    
        Table.Combine({
    
            Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "div"}}),
    
            Html.Table("<div>" & [Final Decision Notes] & "</div>", {{"Final Decision Notes", "br"}})
    
        })
    
    else
    
        #table({"Final Decision Notes"}, {{ [Final Decision Notes] }})

     

    For reference:
    Html.Table - PowerQuery M | Microsoft Learn


    Thank you for being part of Fabric Community Forum.

    Regards,
    Karpurapu D,
    Microsoft Fabric Community Support Team.


  • v-karpurapud's avatar
    v-karpurapud
    Community Support

    Hi LoMorrissey 

    I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If your issue resolved, kindly mark the helpful response and accept it as the solution to assist other community members in resolving similar issues more efficiently. If not, please provide detailed information so we can better assist you.

    • LoMorrissey's avatar
      LoMorrissey
      Regular Visitor

      Hi,

       

      I asked one of my colleagues and he was able to give me this code which solved the issue: 

       


      #"Added Custom" = Table.AddColumn( #"Replaced Value24" , "Custom", each if [Review Team Notes] = null or not Text.Contains([Review Team Notes], "<") then
          "<p>" & [Review Team Notes] & "</p>"
      else
          [Review Team Notes]),
      #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each if [Custom] <> null then
      Html.Table([Custom], {{"text", ":root"}})
      else
      [Custom]),
      #"Expanded Custom.1" = Table.ExpandTableColumn(#"Added Custom1", "Custom.1", {"text"}, {"Custom.1.text"}),
      #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom.1",{"Review Team Notes", "Custom"}),
      #"Renamed Columns12" = Table.RenameColumns(#"Removed Columns",{{"Custom.1.text", "Review Team Notes"}}),
      #"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns12",{{"Review Team Notes", type text}})
      in
      #"Changed Type2"

       

      • v-karpurapud's avatar
        v-karpurapud
        Community Support

        Hi LoMorrissey 

        We are glad to hear that you identified the cause and resolved the issue. Thank you for providing the details here. Please mark your insights as 'Accept as solution' so others with similar issues can find it easily.


        Thank you.

  • you can define the following function and use it in your code

     

    (inputText as text) as table =>
        if Text.StartsWith(inputText, "<") then
            Table.Combine({
                Html.Table("<div>" & inputText & "</div>", {{"Final Decision Notes", "div"}}),
                Html.Table("<div>" & inputText & "</div>", {{"Final Decision Notes", "br"}})
            })
        else
            #table({"Final Decision Notes"}, {{ inputText }})
    
  • Hi LoMorrissey 

    You're trying to create a custom column in Power Query that extracts meaningful text from rows containing HTML content, while preserving rows that already contain plain text. Your current code attempts to parse HTML when the Final Decision Notes column starts with a < character (which typically indicates HTML), and otherwise returns the original value. However, you're using Table.Combine(...) inside a row-level expression, which is designed to return a table, not a value, and that causes inconsistency—Power Query expects each row to return a scalar (like a single text value), not an entire table.

     

    What you're missing is the extraction of text from the HTML table structure into a single value per row. Also, you need to ensure the output is consistently a single text string, regardless of whether the input is HTML or plain text.

     

    Here’s a corrected version of your logic:

    = if Text.StartsWith([Final Decision Notes], "<") then
        let
            html = "<div>" & [Final Decision Notes] & "</div>",
            divTable = Html.Table(html, {{"Text", "div"}}),
            brTable = Html.Table(html, {{"Text", "br"}}),
            allText = List.Combine({divTable[Text], brTable[Text]}),
            mergedText = Text.Combine(List.Select(allText, each _ <> null), " ")
        in
            mergedText
    else
        [Final Decision Notes]