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
AlexanderK79
Advocate III
Advocate III

Little gift to the community: remove comments from text string

I was looking for a PowerQuery function that would return a text string that is stripped from comments, so it could be processed afterwards.

Since I could not find one, I decided to build one myself.

 

I'd love to hear your feedback and if this helped you... thank you for the kudo 😉 !

 

(FullText as text) =>
let FT0 = Text.Replace(FullText, Character.FromNumber(13), Character.FromNumber(10)),
    FT1 = Text.Replace(FT0, Character.FromNumber(10)&Character.FromNumber(10), Character.FromNumber(10)),
    FT2 = Text.Replace(FT1, Character.FromNumber(10)&Character.FromNumber(10), Character.FromNumber(10)),
    FT3 = Text.Replace(FT2, Character.FromNumber(10)&Character.FromNumber(10), Character.FromNumber(10)),
    FT9 = FT3,
    CommentStyle1 = // remove the parts encoded with /*  */ 
        List.Transform(
            List.RemoveFirstN(
                Text.Split("/*FAKE START*/"&FT9, "/*")
                , 1
            )
            ,  each Text.Split(_, "*/"){1}
        ),
    TextWithoutCommentStyle1 = Text.Combine(CommentStyle1, Character.FromNumber(10)), // Combine the list back to a text string
    TextWithoutCommentStyle2 = List.Transform(Text.Split(TextWithoutCommentStyle1, Character.FromNumber(10)), each Text.Split(_, "--"){0} ), // now split the line by newline and strip everything after --
    TextWithoutCommentStyle3 = List.Transform(TextWithoutCommentStyle2, each Text.Split(_, "//"){0} ), // now split the line by newline and strip everything after //
    ReturnText = 
    Text.Combine(
        List.Select(
            List.Transform(
                TextWithoutCommentStyle3
                , each Text.Trim(_)
            )
            , each _ <> ""
        )
        , Character.FromNumber(10)
    )

in ReturnText

  

1 REPLY 1
V-pazhen-msft
Community Support
Community Support

@AlexanderK79 

Awesome, thank you for sharing. The code was explained nicely, maybe put the sample table too so viewers can follow the tutorial better. 


Paul Zheng _ Community Support Team

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