MDX calculated column based on user defined value - ssas

I am trying to produce an Excel PivotTable which displays and compares Sales Rep revenue figures based on values specified by the user, for example, the InvoiceYear.
This data is coming from an SSAS cube and I have connected to it using PowerPivot. I have columns from the cube in the data model and I am attempting to edit the MDX code in the Table Properties window to create some new columns.
This works so far, but the InvoiceYear value is hardcoded to '2010' at the moment. I need a way for this code to accept a value entered into a cell by the user in the excel worksheet itself.
I am just a beginner at this, so I am not aware if this is even possible.
If this approach is NOT possible, I really need to find another way to do it. Come at it from the other side and refresh the column whenever the cell changes via a macro or vba? Some other solution?
Here is what I have in the Edit Table Properties window. This works correctly, but I can't have the Year value hardcoded like that or users will not be able to specify a custom Year value, obviously. I have tried everything I can think of to reference a cell in a Sheet, but the column usually just appears empty with no errors.
WITH MEMBER RentalSales AS
CASE when [SalesRep_Dim].[InvoiceYear].currentmember.membervalue = '2010' then [Measures].[LineAmountMST] else 0 end
SELECT NON EMPTY { Measures.RentalSales, [Measures].[LineAmountMST] } ON COLUMNS,
NON EMPTY { ([SalesRep_Dim].[InvoiceMonth].[InvoiceMonth].ALLMEMBERS * [SalesRep_Dim].[InvoiceYear].[InvoiceYear].ALLMEMBERS * [SalesRep_Dim].[SaleRep].[SaleRep].ALLMEMBERS * [SalesRep_Dim].[ItemGroup].[ItemGroup].ALLMEMBERS * [SalesRep_Dim].[Site].[Site].ALLMEMBERS * [SalesRep_Dim].[Source].[Source].ALLMEMBERS * [SalesRep_Dim].[Type].[Type].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( -{ [SalesRep_Dim].[InvoiceMonth].&[]&[0], [SalesRep_Dim].[InvoiceMonth].[All].UNKNOWNMEMBER } ) ON COLUMNS FROM [GMFSalesTransRepRevenue]) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

I don't believe it is possible with your current approach. However if you switch to using Power Query to get data from SSAS and load to Power Pivot it should be possible.
Power Query can query SSAS as described here. The you read a cell in the Excel workbook that the user has entered and filter the data coming from SSAS in the Power Query.

You should look into slicers, they are more appealing visually and you can use them to filter multiple pivot table with one selection.
Setup a report filter and setup your slicer to the same attribute, you can use the Cell for your formula references and use the slicer for user input.
This should be as easy as selecting your pivot table, going to the Insert excel Tab, under Filter there show be the "Slicer".
Select the attribute you are trying to filter by, and your slicer is done.

There is an option to create a write-back measure. You may convert its value to calculated member which takes place in your pivot table. However it's not a best practise. I'd recommend to avoid this method.
Steps:
Create a view (one column int, for example).
Crate a measure group from this view.
Add a partition with writeback settings, see: http://bidn.com/blogs/DevinKnight/ssis/1768/ssas-creating-and-using-a-writeback-measure-group
Create a calculated measure, for example:
(StrToMember('[SalesRep_Dim].[InvoiceYear].&[' + Cstr([Measures].[WriteBackMeasure]) + ']'), [Measures].[LineAmountMST])
where [Measures].[WriteBackMeasure] is your writeback measure and if [Measures].[WriteBackMeasure] = 2010 then you'll get the following:
([SalesRep_Dim].[InvoiceYear].&[2010], [Measures].[LineAmountMST])
Open two pivot tables: one with [Measures].[WriteBackMeasure] only, another is your report with the calculated member.

Related

Creating a dynamic calculated column using PowerBI DAX

I am new to PowerBI and I am facing a challenge with creating a calculated column that will get updated automatically as the dataset keeps getting filtered. My dataset looks like this -
I have created the Deliverable_Milestone_Match column using the formula -
Deliverable_Milestone_Match =
IF(
Sheet1[Issue_Type] = "CO Deliverable",
VAR _sel = SELECTCOLUMNS(
Sheet1,
"MilestoneIssueKey",
[Issue_Key]
)
RETURN
IF(
Sheet1[MilestoneIssueKey] IN _sel,
"MAPPED",
"UNMAPPED"
),
"MILESTONE"
)
Now, the challenge is, that I will need to apply some filters to this dataset, and since my calculated column references other columns in the data, it also needs to change accordingly. For example The formula is looking up the presence of MilestoneIssueKey in Issue_Key, and it is populating MAPPED if it gets a match, and UNMAPPED otherwise. Now, if I apply a filter that removes a specific unique Issue_Key, then the corresponding MilestoneIssueKey won't have a match anymore, and the Deliverable_Milestone_Match value should change to UNMAPPED now.
I would appreciate it if anyone can help me with this. I am not even sure if this can be achieved via DAX or whether I will need to use Power Query for this.
Thanks in advance!
You first issue is the following:
[...] calculated column that will get updated automatically as the dataset keeps getting filtered
Calculated columns aren't refreshed dynamicaly as filter context evolves. As explained here :
Since calculated columns live at the same level as your tables, they are only calculated when you first define them and during a dataset refresh.
So, in that regard you should try to solve your problem using Measures.

How to set slicer value to first available value form table in Power BI?

I have a requirement that the selected value in slicer must be valid.
Let us suppose if I select a value in Store slicer and that store gets deleted from the company. The slicer will still show its name with no data in visuals interacting with the slicer.
Default Value Selected
Updated Slicer Value Manually
But I want only the relevant store selected in my Store slicer. I know its slicer's property to retain the value which is set in it while publishing the report, but is there any workaround for it.
The essence of your question has been asked and covered in this thread:
Initial value of Power BI slicer based on another slicer choice
The answer is NO, but I can propose a workaround.
In the dimension table with stores, add a calculated column Rank which will determine the store with the highest sales. You may use RANKX function for that.
Add StoreName calculated column which returns the text value "The biggest store" (or "Top 1" - or whatever) and original store names for all the other stores. Use IF.
Put the column StoreName which contains "The biggest store" value to the slicer.
Add a visual (card, table) where you will display the original name of currently selected store.
Sort the column StoreName by Rank column designed for that purpose so that The biggest store will float up to the top position in the slicer. Here is how to sort the column by another column.
Since there is always a store with the highest sales you may always have that value ticked in the slicer and it will always show data.
In this example "The biggest store" is "Store for girls". I keep it selected on the slicer. Then I remove all the records of that store from fact table. Apply. And the slicer is still selected as "The biggest store" but now the biggest store means "Store for ladybirds".
Here is a sample file for download (with both approaches in M and DAX):
M Default Slicer Value.pbix
You can make DimStore table completely in DAX by adding calculated table:
DimStore_DAX =
SUMMARIZECOLUMNS (
Sales[Store],
"Sales", [Sale],
"Rank", RANKX ( ALL ( Sales[Store] ), [Sale] ),
"StoreName", IF ( RANKX ( ALL ( Sales[Store] ), [Sale] ) = 1, "The biggest store", VALUES ( Sales[Store] )
)
)
This is happening only because it's your default value, otherwise the slicer should lose the values automatically once its deleted from your base. If possible, change your default to reflect a store value, of which you are certain that it's always going to be there. I believe the default value will get retained even if there is no data. Another way to do this could be to default to ALL stores and then let the user select what is needed. This may not be what you are looking for, since I am not aware of your specific requirements, but hopefully it helps you arrive at a solution.

Coloring Excel Cell based on a table condition

I am going through the users of a system and reviewing if they have appropriate role names. I then completed an excel table that looks abit like this:
I'm trying to turn the table into a more readable format. I have made a pivot that looks like this:
But I'm not sure how to highlight the cells to reflect the 'Access Appropriate? Yes/No' column. Ideally, it should be colored yellow if the 'Access Appropriate?' = 'No'. I'm thinking of using VBA, but was wondering if there is an easier solution using formulas or pivot table?
Your pivoted data isn't an actual excel pivot table, is it? I know what the x mean, but where do they come from?
Two possibilities come to mind if you want a flexible setup without VBA, aswell as an rather simple VBA-approach that uses an UDF.
Quick'n'dirty (really dirty) would be to
use 1/0 instead of yes/no (you could write that into a helper column with an if-function)
create a new pivot with ROLE_NAME for columns, USER_NAME for rows and SUM or MAX of [Access appropriate] for values
that means: instead of your x you will end up having 1 and 0. Empty cells will still be empty.
conditional format the value-range, e.g. If 1 then green If 0 then yellow if "" then Nothing
Alternatively, you could build your output-table with formulas like INDEX, MATCH and VLOOKUP-formulas.
An additional Key-Column with USERNAME&ROLE_NAME will be needed
conditional format the value-range
VBA: Provided your Rows are distinct a user defined function could do the following
read data into a recordset IF that hasnt been done already (meaning: declared on module-level, the first function call will fill it)
access the data in your recordset with a Recordset.Filter based on your input parameters - USERNAME and ROLE_NAME, in your case
output a certain Field.Value based on your input parameter - Access Appropriate in your case
conditional format the TRUE/FALSE values you get (since this can't easily be done inside an UDF)

Excel 2007 Pivot Table, Different Formulas in Different Columns

Created a Pivot Table in Excel 2007, and it seems I can only make it do one type of calculation at a time.
Is it possible to provide an average in one column, and a sum in the next?
When I click on "Value Field Settings", choosing any of the options applies it to every column in the Pivot Table, can't figure out how to apply it to only one column.
You can have a different calculation.
When you drop a field into the data section of the pivot change the calculation to sum.
Then drag the same field across again this time change it to average

Dynamic Parameter in Power Pivot Query

We are using Excel 2013 and Power Pivot to build modules that consist of several Pivot tables that are all pulling data from the same Power Pivot table, which queries our T-SQL data warehouse.
In an effort to simplify and fully automate this module, we wanted to create a text field that would allow a user to enter a value (a client ID# for example), and then have that value be used as a parameter in the Power Pivot query.
Is it possible to pass a Parameter in the Power Pivot query, which is housed in a text field outside of the query?
You can also pass a slicer or combobox selection to a cell. Define a name for that cell. Put that cell (and others if you have multiple text variables to use) in a table. For convenience, I usually name this table "Parameters". You can then 'read in' the parameters to your query and drop them in your query statements.
The code at the top of your query to read these parameters in might look like...
let
Parameter_Table = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
XXX_Value = Parameter_Table{1}[Value],
YYY_Value = Parameter_Table{2}[Value],
ZZZ_Value = Parameter_Table{3}[Value],
Followed by your query wherein instead of searching for, say a manually typed in customer called "BigDataCo", you would replace "BigDataCo" with XXX_Value.
Refreshing the link each time a different customer is selected will indeed be a very slow approach, but this has worked for me.
Rather than pass a parameter to the data source SQL query, why not utilize a pivot table filter or slicer to do allow the users to dynamically filter the data? This is much faster than refreshing the data from the source.
If for some reason you need to pass this directly to the source query, you'll have to do some VBA work.