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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
AnjanaPothineni
Frequent Visitor

Regex to identify table names from a field with SQL query in it.

HI All,

 

My data set contains a field to store the SQL query that was execute by certain users and I would need to indentify a way to parse the SQL query text from that column and add the table values to new column in Power Query or using DAX in a Power BI report.

 

I am looking for regex to use and indentify table names. If there are more than 1 tables or more then it would need to concatenate them with comma.

 

Regex should be something like this (?:FROM|from|join|into)\s?(\[?\w+\]?\.\[?\w+\]?)

 

Please help.

 

 

 

3 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

Power Query does not support RegExp natively. You can run R or Python scripts though.  

 

Be aware that Power Query is case sensitive. Be aware that joins can be done via comma too.

View solution in original post

Anonymous
Not applicable

Hi @AnjanaPothineni ,
You can try this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk7NSU0uUdBScAvy91UISUzKSTVUitWJVgp29XF1DlFI1NPSUUjSQ5FXSFTw9PNzDVLw8vf0g4gZKSQp+PsBVZdk5qZ6uijYAvVAmGDDPP2CXYNCgLpC/GHqNUDSmSk6Col5mbmJOToKRalpRanFGQX5mXklmgpQ6/EpQnVyLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"SQL QUERY" = _t]),
    ExtractTableNames = Table.AddColumn(Source, "Table", each 
        let
            SQL = [SQL QUERY],
            Words = Text.Split(Text.Select(SQL, {"A".."Z", "a".."z", "0".."9", " ", "_"}), " "),
            TableNames = List.Select(Words, each List.Contains({"FROM", "JOIN", "INTO"}, Text.Upper(_))),
            NextWords = List.Transform(TableNames, each Text.AfterDelimiter(SQL, _ & " ")),
            CleanedNames = List.Transform(NextWords, each Text.BeforeDelimiter(_, " ")),
            NonEmptyNames = List.Select(CleanedNames, each _ <> "")
        in
            Text.Combine(NonEmptyNames, ", ")
    )
in
    ExtractTableNames

Final output

vheqmsft_0-1727163160905.png

Or create a dax

NewColumn = 
VAR Query = 'Table'[SQL QUERY]
VAR FromTable = 
    IF(
        SEARCH("FROM ", Query, 1, 0) > 0,
        MID(Query, SEARCH("FROM ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("FROM ", Query, 1, 0) + 5) - SEARCH("FROM ", Query, 1, 0) - 5),
        BLANK()
    )
VAR JoinTable = 
    IF(
        SEARCH("JOIN ", Query, 1, 0) > 0,
        MID(Query, SEARCH("JOIN ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("JOIN ", Query, 1, 0) + 5) - SEARCH("JOIN ", Query, 1, 0) - 5),
        BLANK()
    )
VAR IntoTable = 
    IF(
        SEARCH("INTO ", Query, 1, 0) > 0,
        MID(Query, SEARCH("INTO ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("INTO ", Query, 1, 0) + 5) - SEARCH("INTO ", Query, 1, 0) - 5),
        BLANK()
    )
RETURN
    CONCATENATE(
        CONCATENATE(
            FromTable,
            IF(NOT(ISBLANK(JoinTable)), ", " & JoinTable, "")
        ),
        IF(NOT(ISBLANK(IntoTable)), ", " & IntoTable, "")
    )

Final output

vheqmsft_1-1727163200509.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

View solution in original post

Please provide usable sample data, not screenshots.

 

Be aware that SQL joins can be done via comma too.

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @AnjanaPothineni ,
You can try this

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKk7NSU0uUdBScAvy91UISUzKSTVUitWJVgp29XF1DlFI1NPSUUjSQ5FXSFTw9PNzDVLw8vf0g4gZKSQp+PsBVZdk5qZ6uijYAvVAmGDDPP2CXYNCgLpC/GHqNUDSmSk6Col5mbmJOToKRalpRanFGQX5mXklmgpQ6/EpQnVyLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"SQL QUERY" = _t]),
    ExtractTableNames = Table.AddColumn(Source, "Table", each 
        let
            SQL = [SQL QUERY],
            Words = Text.Split(Text.Select(SQL, {"A".."Z", "a".."z", "0".."9", " ", "_"}), " "),
            TableNames = List.Select(Words, each List.Contains({"FROM", "JOIN", "INTO"}, Text.Upper(_))),
            NextWords = List.Transform(TableNames, each Text.AfterDelimiter(SQL, _ & " ")),
            CleanedNames = List.Transform(NextWords, each Text.BeforeDelimiter(_, " ")),
            NonEmptyNames = List.Select(CleanedNames, each _ <> "")
        in
            Text.Combine(NonEmptyNames, ", ")
    )
in
    ExtractTableNames

Final output

vheqmsft_0-1727163160905.png

Or create a dax

NewColumn = 
VAR Query = 'Table'[SQL QUERY]
VAR FromTable = 
    IF(
        SEARCH("FROM ", Query, 1, 0) > 0,
        MID(Query, SEARCH("FROM ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("FROM ", Query, 1, 0) + 5) - SEARCH("FROM ", Query, 1, 0) - 5),
        BLANK()
    )
VAR JoinTable = 
    IF(
        SEARCH("JOIN ", Query, 1, 0) > 0,
        MID(Query, SEARCH("JOIN ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("JOIN ", Query, 1, 0) + 5) - SEARCH("JOIN ", Query, 1, 0) - 5),
        BLANK()
    )
VAR IntoTable = 
    IF(
        SEARCH("INTO ", Query, 1, 0) > 0,
        MID(Query, SEARCH("INTO ", Query, 1, 0) + 5, FIND(" ", Query & " ", SEARCH("INTO ", Query, 1, 0) + 5) - SEARCH("INTO ", Query, 1, 0) - 5),
        BLANK()
    )
RETURN
    CONCATENATE(
        CONCATENATE(
            FromTable,
            IF(NOT(ISBLANK(JoinTable)), ", " & JoinTable, "")
        ),
        IF(NOT(ISBLANK(IntoTable)), ", " & IntoTable, "")
    )

Final output

vheqmsft_1-1727163200509.png

 

Best regards,
Albert He


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Hi @Anonymous Thank you so much for you reply. I tried DAX option and I am getting the results like this. (As our users have all sorts of comments in their code that this regex is also picking on like below screen shot

 

Please see belwo screenshot. How do I only identify genuine tables names alone.

 

Hignlighted in yellow.

 

Screenshot 2024-09-30 101310.png

Please provide usable sample data, not screenshots.

 

Be aware that SQL joins can be done via comma too.

Omid_Motamedise
Super User
Super User

See this short video, you can find how to use regex in Power Query

 

 

https://youtu.be/-M4Mh4QFxrg?si=m3G2zd7I0l2jJTMp

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
lbendlin
Super User
Super User

Power Query does not support RegExp natively. You can run R or Python scripts though.  

 

Be aware that Power Query is case sensitive. Be aware that joins can be done via comma too.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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