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

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.

Related

Why does Power Query not honor the sort in the output table when loaded to the data model?

I'm using Excel 365 and pulling in a table via Power Query. In the underlying SQL, I use an ORDER BY clause to sort the data, which works correctly in the PowerQuery preview and in my SQL client. But I noticed the table that winds up getting loaded in Excel isn't sorted in the same order. I added a sort step to Power Query as the final step, and it still didn't work, even after I wrapped the step in Table.Buffer(). I have also tried it with and without the Preserve column order/sort checkbox checked.
It only honors the sort settings if I uncheck the box to load the query into the data model.
How can I make it follow the sort as specified and load into the data model?

Dynamic date query in Power Query

I'm fairly new to power pivot / query / BI so please excuse my lack of understanding.
Summary of what I'm doing
I have access to an OLAP cube which I am choosing to connect to via Power Pivot Data Model in Excel.
In the Table Import Wizard, I am able to produce what I need from the cube and develop in the Power Model using fixed date ranges. However, this requires me to come back in and alter the date ranges for a number of reasons.
I now want to limit my data dynamically based on a date filter.
I want to do this in the query to limit my model size.
I am working in the "Table Import Wizard" shown below
Snapshot of where I am working, note filter expression
Question:
Am I correct that to achieve this I write an MDX expression? If I add the [Received Date.Day] as a dimension, and change the operator to MDX, it is my understanding I can write an expression to dynamically filter the dataset? When the user refreshes the query they will only get that last 91 days of data based on the received date.
Essentially something like this (excuse my syntax as I have no idea what is required in this platform so I am just relating it to an SQL type syntax):
SELECT Item_Code, Received_Amount
FROM Purchasing_Cube
WHERE Received_Date > = Today()-91
Thanks in advance for any assistance.

Freeze columns from Excel SQL Query

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

How do I send data from excel sheet to a Power BI parameter?

After some searching I found a way to create an Excel function to get parameter value upon invoking. Is there a way that the same thing could be done by passing a date from excel cell to the Power BI as a parameter value.This is for making the parameters dynamic and they should change values when the excel cell value is changed.
The excel function which I am using is
(ParameterName as text) =>
let
ParamSource = Excel.CurrentWorkbook(){\[Name="Parameter_Table"\]}\[Content\],
ParamRow = Table.SelectRows(ParamSource, each (\[Parameter\] = ParameterName)),
Value=if Table.IsEmpty(ParamRow)=true
then null
else Record.Field(ParamRow{0},"Value")
in
Value
and the excel parameter table has two columns namely 'Parameter'(this is passed to the function) and 'Value'.
Can the value from the 'Value' column be used to change the parameter in the Power BI. I have already loaded this excel table into the Power BI model.
Any help will be appreciated.
You can pass a cell value as a parameter by loading it into Power Query. If you have a parameters table, load the table into Power Query, create a reference to the table query, go to the item that should be the parameter, right-click > drill-down, then rename it as your parameter. Then you can replace the value in your m code with the parameter. After you've done that, you can change the value of the parameter by editing the cell on the worksheet.
For example, here is a snippet of a parameter table I have that allows the location of the file and it's other data sources to be dynamic.
When you right-click and drill-down, you are left with only one record, which is now your parameter.
Then you can go to the Advanced Editor and swap out the portion of your code that needs to be variable.
This is the code for a string variable that I use. Perhaps try without the extra steps. Just drill down save and use it.

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.

Resources