How to change source of a power query using VBA? - excel

So in Excel 2016, they have this neat tool called Power Query, basically a glorified excel table. Every table has steps in it for filtering, removing columns, etc... The first step is the source step, to assign a connection string basically to retrieve data, normally this source just points back to the query which created it.
Anyways, I'm trying in VBA to dynamically change the source of these power queries, anyone have any ideas?
I tried using the whole connections vibe, but was unsuccessful.

You can access the query through ActiveWorkbook.Item. You can then modify the Formula property. You can find the documentation on these objects here.
Please note that the Power Query object model was only added to VBA in Excel 2016 and cannot be accessed in prior versions.

The queries can be accessed via the Queries collection in a Workbook object. The relevant property for the source is Formula.
Example code:
ActiveWorkbook.Queries.Item("MyQuery").Formula = "[Insert actual M formula here]"

Many years later, but I'm adding a different solution for those, like me, still stuck with Excel 2013. As stated by #Alejandro in his response, Power Query was only added to the object model in Excel 2016.
If you are using an older version of Excel, you can use a cell-based solution similar to the one talked about here for relative source paths:
https://techcommunity.microsoft.com/t5/excel/power-query-source-from-relative-paths/m-p/206150
Basically have a cell some where in your workbook that contains your query's source. Name that cell, using standard Excel names. In the example below, I've named the cell SourceFileName, and I'm trying to load an Excel file to PowerQuery. The full name of the source Excel file (including path) is in SourceFileName . You can then access the contents of that cell via Power Query:
<previous M code>
sourceFileName= Excel.CurrentWorkbook(){[Name="SourceFileName"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(sourceFileName ), null, true),
<rest of M code>

Related

Updating a table from a power query changes formula reference

Saw this was previously asked here on SO and had no solution and I can't find a solution through Google either.
I have a workbook with a tab called "Data" containing a table which is updated via a power query and another tab called "Calcs" with formulas referencing the cells in the table from "Data". When I refresh the table, it pulls data via the power query, but when it's done, the formula references change.
For example, before the refresh, I'll have formulas like this in the "Calcs" tab:
=COUNTIFS('Data'!$A$2:$A$26886,$A1060,'Data'!$K$2:$K$26886,'BY CAT'!$B1060)
After the refresh, the references for column A only change to
=COUNTIFS('Data'!$A$10242:$A$26886,$A1060,'Data'!$K$2:$K$26886,'BY CAT'!$B1060)
And it results ina #VALUE! error message.
How can I prevent Excel from creating this reference shift?
You might be able to get around this by using full column references:
=COUNTIFS('Data'!$A:$A,$A1060,'Data'!$K:$K,'BY CAT'!$B1060)
Full column references aren't always a good idea, but it might just work in this case.
Edit:
Table column references would be ideal, TableName[ColumnName]. These should work given that you are reading from a power query generated table.

How to Link a Excel Table with Access and prevent NULL Values due to wrong Data Type Conversion?

In the current Project i Need to Keep a Excel File which gets Values from a Machine to the Access Database to work with them and Import them in the Data Model.
Problem is some of the Values give invalid results due to the way they are saved. For example the timestamp is saved like
030420 instead of 03:04:20 and Access cant handle that and gives me a #NUMBER
I can not simply Change the datatype in Excel because the whole Excel gets refreshed every hour by a source that i cant influence.
Any help appreciated.
If Erik's proposal does not work, you can
- create a backup copy of your Excel source
- tweak the file: enter text in the first row of the problematic columns
- link the tweaked file into Access
- put back the real file in place.
Now the problematic columns should be read as Text, and you can build a query that solves any issue like conversion, null handling...
Link, don't import, the Excel file, and you have a linked table.
Now, use this linked table as source in a simpel select query where you modify the data and alias the fields as needed. For example:
Select
F1 As SomeName,
F2 As OtherName,
TimeSerial(Mid([F5],1,2),Mid([F5],3,2),Mid([F5],5,2)) As TrueTime
From
LinkedTable
Where
F7 Is Not Null
The use this query for your import.
Consider querying the Excel file instead of using a linked table.
The query can directly query an Excel range:
SELECT * FROM
[Excel 12.0 XML;DATABASE=PathToMyExcel;HDR=Yes;IMEX=1].[MyRange] t
Then, you can use functions like TimeSerial to cast numbers to time values.

Excel PowerPivot - change data source type

I have an Excel 2016 with 30 graphs based on PowerPivot. PowerPivot fetches the data from another Excel sheet, but I want it to get the data from a SQL server table instead.
How can I change the data source type in PowerPivot? I've tried looking in the Excel xml without any luck. Would be a lot of work re-creating all graphs over again just to switch data source
Thanks
Dennis
One suggestion I would make for the future, if all the users are using 2016 is to use Power Query which comes standard with that version of excel. In the Power Query loading data into Power Pivot scenario, all Power Pivot cares about is the column names. This means that the query can be changed between data source types without causing issues, as long as the same column names are changed.
As an example, I have one file that based on a parameter flag rips data out of a series of excel files on a shared network drive or Share Point. Both of which would be different data sources. The first opening a folder as the data source, then excel files listed within the folder. The other opening a share point list as its data source, then navigating though excel files.

CSV Connection into Named Table not Range

If you make a data connection to another Excel file or an Access table it will import that data into excel as a Table. This is great! But when I do the same with "From Text" and choose a CSV, it loads no problem, but it loads as a RANGE not a TABLE. This is highly frustrating as I NEED it to be in the table format so that I can take advantage of the dynamic column names.
Am I missing a tickbox somewhere? I'm not opposed to using VBA but it really seems odd that Excel can't do CSV to Table so I'm hoping for a native solution.
I should mention that if it's a VBA solution, it CANT break the workbook. So if I'm doing something like:
=SUMIFS(CSVDATA[SalesDollars], CSVDATA[RepName], "BOB")
It will still work after refreshing.
So you can get around the 'data connection' not creating a table by downloading the MS Excel 2010 plugin (from Microsoft) called Power Query. It's free and is a default feature in MS Excel 2013.
It will allow you to choose to create a Power Query Table from External Data >> From File >> From CSV
Upon doing this, it will create a named table for you and a Power Query object attached to the table. You can use the table itself the same way you normally would - with the Columns as references for formulas.
For instance, the default table that I just created using the steps above was auto-named: Table_ExternalData_1
I can then select it using the normal method in formulas:
=Table_ExternalData_1[Column2]
etc...
Hope that helps.

Excel wouldn't import Access query that contains VBA calls

Access and Excel 2013
Trying to use an Access base as a data source in Excel, it seems like if a query calls a VBA function, Excel doesn't see it.
This query shows up in Excel:
SELECT "StaticValue" AS static_value;
This one doesn't:
SELECT my_function() AS value_from_vba;
The code of the module containing my_function being:
Option Compare Database
Function my_function() As String
my_function = "ValueFromVBA"
End Function
How can I call a VBA function in an Access query and link this query to Excel ?
Hmm perhaps;
Make table and query the table (as stated in comments)
My preferred is just to export the data from access into a new worksheet that you need in excel, then you can do what you want with it if you are only getting values. Then you can go back in and update anything after (in your access tables) with VBA should you need it
I ended up creating a table (SELECT … INTO …) that I refresh on a regular basis. This table is linkable within Excel. It is probably a dirty trick, but it does the job.

Resources