Freeze columns from Excel SQL Query - excel

I have an Excel SQL query made using the Query wizard / power query. Sometimes, when I refresh the data, the columns shuffle order! I have already tried checking/unchecking "preserve column sort" as suggested here: https://www.mrexcel.com/board/threads/sql-changing-column-order-when-put-into-excel.207385/

#Nathan brings up a good point. Try specifying in the query itself, the order of the columns.
If that does not work, The solution would be to accomodate the different order of columns as a possible outcome every time by making the result query into a ListObject Table. (I believe you can check this as part of the Query Wizard, to import as Table)
Then you can use the name of the columns without knowing the range address in your worksheet formulas and VBA code. You could simply refer to the column name in the format shown in this tutorial, then get the properties for column or row number using any number of methods.
Tutorial on using ListObject Tables

Related

Functions pulling from a refreshing power query giving REF! error

I have a changing power query which size changes based on the amount of PDF's from a folder. I have functions set up on a separate sheet tab to pull data from a range of cells from the power query tab. The main problem I have is if I load only a few of these pdf's my references will get deleted since the table range is getting removed. I have been trying to find work arounds to this, any help would be appreciated.
I tried to do a Pivot table of this data I need but I am trying to match certain criteria like an invoice and specific words to get a value. This did not work for my situation.
Example of Power Query
this is the data I am pulling that will turn into REF! if I take a pdf out and the rows shrink.

Excel Power Pivot / Data Model - passing a parameter from worksheet

I have a SQL query I am using for a workbook requiring multiple pivot views from the same dataset. Part of the query is looking at a declared value and based on that value it will either return one result or another, that declared value however needs to be a parameter that end users will input into the workbook.
Normally I would use the excel function to pull the data from the SQL server and then pass a parameter in the advanced query option using " Let Parameters = Excel.CurrentWorkbook()".. however the result set it too large and therefore I need to use Power Pivot.
I however for the life of me can find no help on how to pass a parameter from a cell in a worksheet to power pivot - is this possible, is there a VB script I can use to update the query, alternatively what other options are available other than having to get the end users to go into power pivot and edit the query in the properties each time?
As the variable is used in a computation, I am not using the parameter to filter the dataset and that seems to be the only advice I have been able to find. I am new to Power Pivot.
I came right by doing the following:
Add the SQL query in Power Query, as a data connection only. In the advanced query options, I added the required parameters.
I then selected the query and selected the option to load to Data Model.
Seems to be working.

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.

Querying single data points from the Excel Data Model / Power Query (Get & Transform Data)

I'm using an up-to-date version of Excel 2016 (via O365 E3 license) and using Power Query / Get & Transform Data. I can successfully create queries and load them to the page. I have also successfully created Power Pivot reports.
I would like to query single data points from the data loaded via Power Query. For instance, imagine a dataset called DivisionalRevenue with:
Date Division Revenue
2016-01-01 Alpha 1000
2016-01-02 Alpha 1500
2016-01-01 Beta 2000
2016-01-02 Beta 400
I could easily load that to an Excel workbook or include it in the data model and create a power pivot. However, Power Pivot doesn't always meet my requirements, particularly around how the data is displayed on the page. In order to achieve my goal I may want to be able to query individual data points.
I would like to have a cell on the page with a formula in it that I can use to query individual data points. If it was in a pivot table I could use something like:
=GETPIVOTDATA("Revenue",$A$3,"Date",DATE(2016,1,1),"Division","Alpha")
The lookup values (date and division) could be retrieved from a cell on the page or hard-coded into the formula. This is a requirement for several reports I'm working on.
Or, I could add a combined lookup column with Date and Division concatenated and use a vlookup to pull the values like:
=VLOOKUP("42371Alpha",I9:L13,4,FALSE)
Finally, I could use a combination of INDEX and MATCH to identify the correct row number and then pull the data.
All of these solutions require the data to be loaded onto a sheet. One requires a pivot table that has to be refreshed to work properly. The other two require creating arbitrary lookup columns so that you can match a row based on more than one field (date and division in this example), and you have to ensure that that lookup field's formula is properly extended down the length of the data table. In both cases I would have concerns when sharing this workbook with my colleagues in case someone affects the rather fragile setup of the pivot table or the lookup.
So, what I truly want to find is something equivalent to pivot table querying against a dataset.
** This doesn't exist, but I would like to know if something like it does **
=GETQUERYDATA("Revenue","DivisionalRevenue","Date",DATE(2016,1,1),"Division","Alpha")
Does such a thing exist? Can such a thing be done? Can I retrieve arbitrary data points from the dataset created through Power Query / Get & Transform Data?
I think that what you want are cubefunctions:
Some Background
How to easy create cubefunctions from a pivot table
There is a feature in Excel that allows you to query off of a PowerPivot model, but it's not highly advertised for some reason.
Once you have the data in your PowerPivot model, go to your Excel -> Data tab -> Existing Connections -> Tables tab
From there, choose the table that you want to start with. Once that table's data is on your excel sheet, you can actually right click that table -> go to "Table" -> "Edit DAX"
From there you can enter the following DAX function, as an example
EVALUATE
FILTER(SampleData,[Date]=DATE(2016,1,1) && SampleData[Division]="Alpha")
Make sure to choose Command Type=DAX in the drop-down. Here's how it looks on my screen:
To further improve your querying power, you can install the optional "DAX Studio" plugin for Excel, which allows you to write custom DAX queries and then export the results directly back to an Excel sheet.

Create a Table with data filtered from data in another worksheet

I have a table with 105 columns and around 300 rows in Sheet 1. I need in Sheet 2 a reduced version of the same table, filtered by some column values (not the first column).
I've looked at Pivot Tables but it seems that I can not get the same tabular structure. I have tried with Advanced Filter and I get an error:
"The extract range has a missing or illegal field name".
Could you help?
Microsoft's PowerQuery addin supports this. One of its many sources can be Excel Data-From Table.
I have discovered that one needs to run Advanced Filter from the destination sheet, in an unused place (best over the intended destination, not on it or below it).
Thanks
You can use the add-in for table-valued functions I developed to make any operations (including filtering, partitioning, aggregation, distribution etc.) on data tables in Excel.
Each table (ListObject in Excel) is an input or output parameter for a table-valued function. You can for example feed three tables as input parameters to a table function which generates some resultant tables.

Resources