Dynamic date query in Power Query - excel

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.

Related

Power pivot relation between tables from data model created with power Query

I made a relation 1 to many between to table but when I try to use it in a pivot table it fails. I get the usual yellow message saying it may lack a relation. When I let it try to detect one it fails to find anything possible and when I check the existing ones, mine is there and done properly.
Here is a mock-up of the situation :
These are the simple tables :
To send the values to the data model I make those tables go through Power Query because the real situation requires Power Query. No additional steps are added, it's just the "From table or range" source.
Then, in Power Pivot, I create the relation with the diagram view :
When I then create a pivot table, the relation has not impact :
I also tried to add a measure and use it at as the value but to no avail :
Price:=SUMX(Rqt_Ref;Rqt_Ref[C_B]*Rqt_Ref[C_C])
I did not create the pivot table with the original tables instead of the one that were send to the date model because I want this to work even if there are more than 1 048 576 rows.
I have no idea as for why it fails and I welcome any comment.
Edit : Since I don't want to do the job in Power Pivot with DAX (related) I did the merge with Power Query Merge queries :
I think you might be misunderstanding how PowerPivot works. Your model should be related on C_A and C_4 for a start.
When I add fields from the dimension (C_A) and then one from fact C_4, it will show a cross join of everything and appear not to be working. Excel does this until you place a value in the values well.
Once I drop a count in the well like follows, the relationship works fine.
Where you might be getting confused is if you drop a field from Rqt_Ref into the values well and then you will get this error again and a full cross join. The reason for this is that you are asking Power Pivot to go from dimension to fact (OK) but then from fact to dimension (not OK)

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.

How to invoke power query from DAX

I have power query M code that generates the table from JSON input, I am able to load to the DAX using close and apply but I need a way to programmatically load the result set from power query to DAX.
Where can I start?
Edit
Data in power query window:
Using below controls I load the data into DAX (here DAX means load to power pivot data model) or Excel sheet.
Everything looks fine here when I used the Excel/power query controls to load data.
My question here is instead of using Close/Load (Second image) is their any way using code to pull data out of power query.

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.

DAX return ColumnName as the row value

Problematic data source
There is a SharePoint list with columns: Person, Customer, Responsibility, Week_1, Week_2, Week_n. Values are workload estimates in percentage ( and estimated hours are then calculated from avg. working hours).
Reasoning
The reason for this is that the list is much easier to use as there is no need to create new line entry for every single week per employee.
My take on the issue
You might already see my problem. For painless analytics there should be a similar data model as described in Reasoning chapter.
However it should be possible to create required data model. A new column would be created and it should get its "Week"-value from a lookup-function that could return "ColumnName". This is possible in Excel, but for the record I haven't succeeded in PowerBI with DAX functions.
What would be your recommendation?
I would recommend using Power Query to read from the SharePoint list and the unpivot the data. You can install Power Query in Excel 2010 and Excel 2013. It ships with Excel 2016 named as Get & Transform. And Power Query is available in Power BI Desktop. It is probably a better choice for this than DAX.

Resources