Forum Discussion

kobia10's avatar
kobia10
Microsoft Employee
5 years ago

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

  • Jimmy801's avatar
    Jimmy801
    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's avatar
      kobia10
      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.

    • kobia10's avatar
      kobia10
      Microsoft Employee

      Seems from that thread it does not work anymore

  • WyldKnyght's avatar
    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.

     

    • kobia10's avatar
      kobia10
      Microsoft Employee

      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