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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
kobia10
Microsoft Employee
Microsoft Employee

Edit multiple queries at the same time

Is there a way to edit multiple queries at the same time? 

I have ~30-40 queries I need to do search and replace in... 

I see I can copy\paste between PQ windows, but if I paste to a text editor and do changes I cannot copy text and paste the multiple queries back to PQ, probably as the copy from PQ contain proprietary format in the clipboard.

7 REPLIES 7
WyldKnyght
Frequent Visitor

Has this been changed or anyone found a work around?

I have a spreadsheet with over 100 woksheets that get created as individual queuries, and I want to make the same changes  to each worksheet? From what I can gather I need to modify them individually or add them as a reference only, or append them into a new query. Unfortunately these options take a heavy toll on my resources and performance, and sometimes even crashes.

 

Wrote two functions a to implement serialization/deseralization of PQ

Here goes... with a disclaimer, haven't looked for a while...

Function savePQ() As Boolean
    Dim Q As WorkbookQuery
    
    Dim Xml As Object
    Dim Parent As Object, Node As Object
    Dim Query As Object
    
    'Dim Xml As New DOMDocument60
    'Dim Parent As IXMLDOMElement, Node As IXMLDOMElement
    'Dim Query As IXMLDOMCDATASection
    'Dim Desc As IXMLDOMAttribute
    
    Dim a&
    
    Dim Path$
    
    On Error GoTo savePQ
    
    If Xml Is Nothing Then
        Set Xml = CreateObject("Msxml2.DOMDocument")
    End If
    
    Set Parent = Xml.createElement("Queries")
    
    For a = 1 To ActiveWorkbook.Queries.Count
        Set Q = ActiveWorkbook.Queries(a)
        Set Node = Xml.createElement("Query" + CStr(a))
        
        Set Query = Xml.createCDATASection(Q.Formula)
        Node.appendChild Query
        Node.setAttribute "Description", Q.Description
        Node.setAttribute "Name", Q.Name
        Parent.appendChild Node
    Next
    
    
    Xml.appendChild Parent
    
    Path = getOneDrivePath(ActiveWorkbook.Path) + "\PQ Queries.xml"
    Xml.Save Path
    
    Exit Function
    
savePQ:
    
    MsgBox Error
    
End Function

Function loadPQ() As Boolean
    Dim Q As WorkbookQuery
    
    Dim Xml As Object
    Dim Parent As Object, Node As Object
    Dim Query As Object
    
    Dim col As New Collection
    Dim Desc As String
    
    'Dim Xml As New DOMDocument60
    'Dim Parent As IXMLDOMElement, Node As IXMLDOMElement
    'Dim Query As IXMLDOMCDATASection
    'Dim Desc As IXMLDOMAttribute
    
    Dim a&
    Dim Path$
    
    If isPQQueriesLoaded Then
        Exit Function
    Else
        isPQQueriesLoaded = True
    End If
    
    On Error GoTo loadPQ

    If Xml Is Nothing Then
        Set Xml = CreateObject("Msxml2.DOMDocument")
    End If
    Path = getOneDrivePath(ActiveWorkbook.Path) + "\PQ Queries.xml"
    
    On Error Resume Next
    Xml.Load Path
    If Err <> 0 Then
        Exit Function
    End If
    
    On Error GoTo loadPQ
    
    If Xml.ChildNodes.Length Then
        
        For Each Q In ActiveWorkbook.Queries
            col.Add Q, Q.Name
        Next
        
        Set Parent = Xml.FirstChild
        
        For a = 0 To Parent.ChildNodes.Length - 1
            Set Node = Parent.ChildNodes(a)
            Set Query = Node.FirstChild
            Set Q = ActiveWorkbook.Queries(Node.getAttribute("Name"))
    
            If Not Q Is Nothing Then
                Desc = Node.getAttribute("Description")
                If Q.Description <> Desc Then
                    Q.Description = Desc
                End If
                If Q.Formula <> Query.Text Then
                    Q.Formula = Query.Text
                End If
                col.Remove Q.Name
            Else
                Set Q = ActiveWorkbook.Queries.Add(Node.getAttribute("Name"), Query.Text, Node.getAttribute("Description"))
            End If
        Next
        
        For Each Q In col
            Q.Delete
        Next
    End If
    
    Exit Function

loadPQ:
    
    MsgBox Error

End Function

 

That's a lot of code.

Anonymous
Not applicable

Seems from that thread it does not work anymore

Jimmy801
Community Champion
Community Champion

Hello @kobia10 

 

I think there is not yet any way to do this. However there should be some external programs like Power BI Helper where you can at least export all your queries into one file. But I don't know if it's possible to import it again.

 

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

kobia10
Microsoft Employee
Microsoft Employee

Thanks. Yes, I can use the helper or simply copy queries and paste to notepad to see the queries. I am looking for a way to edit them and write the back.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors