How to pass multivalues parameters in MDX calculated members? - sql-server

I'm actually working on a SSAS Cube using SQL Server Reporting Services.
In order to filter a field, I need to use a calculated member which specifically use a parameter.
Here is my calculated member:
WITH MEMBER [Measures].[Comparable Stock Room] AS Iif (
[Stock room].[Stock room code].currentMember IS (Iif (STRTOMEMBER(#StockRoomCode).Count = 1, STRTOMEMBER(#StockRoomCode), xxx)),
0,
[Measures].[Retail sales amount invoiced including tax]
)
This works well if #StockRoomCode only have one value. But I don't know how to make it works when the parameter has multiple values. How to replace the 'xxx' ?
Can you help me or tell me how to do it a better way, thanks !

You could create a calculated member aggregating the members you want to report on and define the measure in tems of the calculated member, something like:
with member [Stock Room].[Stock room code].[foo] as
aggregate (strtoset (#StockRoomCode)),
member [Measures].[Comparable Stock Room] as
([Stock Room].[Stock room code].[foo],
[Measures].[Retail sales amount including tax])
(Note: not tested, just off the top of my head). Or,
with set [foo] as strtoset (#StockRoomCode),
member [Measures].[Comparable Stock Room] as
sum ([foo], [Measures].[Retail sales amount including tax])
select [Measures].[Comparable Stock Room] on columns,
(Slicing dimension such as time) on rows
from [cube]
where [other slice]
Note, with either, get your set expression right and test that first.

Related

Convert LastNotEmpty Query to Member

I would like to report the state of data availability resp. the max date specific measure(groups) have data loaded. This is a functionality I would then try to incorporate into each report. In theory each measure could have another "last member with data" in the date dimension however it is OK for the moment to simplify that to one measure per measure group or the max for the whole measure group.
I have written an MDX query which gives me the relevant answer. However to leverage easier maintenance I am striving to incorporate that information as a calculated member into the cube itself. Using MDX only every leap year or so I did not successfully manage to convert the query into a calculated member definition.
// Repro-Query on Adventure Works
// with Adventure Works 2012 EE result should be "July 31, 2008"
// contrasted to last date in date dimension which is "December 31,2010
WITH Member [Measures].[Data Availability] AS
[Date].[Calendar].CurrentMember.Name
SELECT
{
[Measures].[Data Availability]
} ON COLUMNS
FROM
[Adventure Works]
WHERE
{
TAIL(
FILTER
(
[Date].[Date].Members,
[Measures].[Internet Sales Amount] >0
)
,1
)
}
Result shoud be the same as in the query but encapsulated within one calculated measure statement. Remember I don't need the value of the measure but just the name of the last dimension element with data in the date dimension.
Regarding code it should be something like this:
WITH Member [Measures].[Data Availability] AS
<your great translation to a member >
SELECT
{
[Measures].[Data Availability]
} ON COLUMNS
FROM
[Adventure Works]
Feel free to suggest different approaches to get the answer to the problem like better performing ways to calculate the last member with data.
Thanks for the great question and for using Adventure Works. Despite what some commenters say, that’s a great way to get an answer here.
Try this. The Tail function returns a set so .Item(0).Item(0) turns it into a member by choosing the first tuple and first member.
WITH Member [Measures].[Data Availability] AS
TAIL(
FILTER
(
[Date].[Date].Members,
[Measures].[Internet Sales Amount] >0
)
,1
).Item(0).Item(0).Name
SELECT
{
[Measures].[Data Availability]
} ON COLUMNS
FROM
[Adventure Works]

Group by array or shared value

My invoice report pull due dates depending on the selection of Payment Plan Code on UI (either semi, monthly, quarterly, annually, or even 18 installments.) It also accordingly pulls gross premium per due date. I need to pull this table per due date and the sum of the gross premium if they fall into one due date.
What I do is break and save the due dates into array. How can I group by them? Crystal doesn't seem to allow me to group by a shared value, or group by array.
You don't need array for this purpose.. using array complicates the report instead you can manipulate grouping like below:
Create a formula like below and use this formula to group.
if parameterselection = "monthly"
then Month(duedate)
else if parameterselection = "yearly"
then Year(duedate)
.
.
.
.
formula till end
Edit-------------------------------------------
In this case as per your comment you need to create one more group (Group2) with due date.
Now you have two groups group 1 is the one I wrote first and group2 using due date and this works

MDX - How to know if a measure has been pivoted with a dimension?

I was hoping someone could help me with this problem.
In a sum type measure, i need to know if this measure has been pivoted with my time dimension. If is not, the measure must show the sum of the last day. In other case, the measure must show the sum of the day with has been pivoted.
I have the measure "Users", if this measure hasn't been pivoted with my time dimensión, it shows the sum of all users of all the time. For example it shows: 5,000,000. When it has pivoted with the time dimension, it shows de users from the day. For example: 100,000 for today 21/05/2015.
When this measure hasn't been pivoted with the "time" dimension, the measure must show 100,000 and not the total sum of all days.
I've been trying white some MDX formulas but i have not found some solution for this.
Thanks
I've interpreted this as when pulling your Users measure without pivoting on the Time dimension you would prefer to see the value that corresponds to the current date.
This can be achieved by modifying the Default Member of your Time dimension. Setting the default date to this last available date
Within your Dimension in SSAS, browse to the Dimension Structure tab and select the attribute which represents the key column (ie. the Usage property is set to Key for only one column)
Within the Properties pane (strike F4 if you do not see it already to open it) select the Default Member property, then select the elipses (...)
The Set Default Member - [Dimension Name] pane will have 3 options, select the last Enter an MDC expression that specifies the default member
Use ONE of the following expressions
TAIL([Date].[DateAttribute].Members).Item(0)
TAIL(NONEMPTY([Date].[DateAttribute].Members)).Item(0)
TAIL(EXCEPT([Date].[Date].Members,{[Date].[Date].[Unknown]}),1).Item(0)
In my case I used the last as I only want to exclude my UnknownMember member
Now when you pull the measure down the value will be for the last date in your cube. This will affect other cubes built on this date dimension of course.

Is it possible to disable multiple selection of dimension values in OLAP cubes in SSAS?

I'm designing a MOLAP cube in SSAS and one of my dimensions has a very high cardinality >1000 members. I want to disable multiple selection on this dimension and only allow users to select one member at a time. Currently, when users browse a cube through SSMS, they can drag this dimension into filters section and multi select multiple values in it. One of my distinct measures doesn't make sense when you multi select multiple values and hence the reason I want to disable multi selection. I couldnt find any such property on the dimension properties. Any ideas how to do this?
Unfortunately, there is no way to disable multi-selection.
What if make this measure NULL if several members are selected?
Let's say we need to disable [Measures].[Count] if several dates are selected in [Report Date].
First, add member, which calculated selected members of this dimension:
CREATE MEMBER CURRENTCUBE.[Measures].[Report Dates Count]
AS
sum(existing [Report Date].[Report Date ID].MEMBERS
,count(existing [Report Date].[Report Date ID].CurrentMember))-1,
VISIBLE = 1;
Than write a SCOPE that NULLifies necessary measure:
SCOPE ([Report Date].[Report Date ID].Members,[Measures].[Count]);
THIS = IIF([Measures].[Report Dates Count]>1,NULL,[Measures].[Count]);
END SCOPE;
Let's verify it!
One member:
Two members:
Four members:
Many members:
[Measures].[Count] is shown only when one member is selected.
Hope this technique may help somehow.

How to add an non aggregated column in a fact table?

I'm working on a SSAS cube to allow user to analyze some sales.
So, I created a fact table to record all sales and few dimensions to browse inside data (category / location & store, etc...).
This is a example of the fact table output (from SQL Server Management Studio) :
When I browse my cube, I can review all sales including date, quantity, etc.
However, when I add some fields like the "unit price" or the "unit cost", it returns me a strange result probably due to an aggregate behavior.
It seems it return the sum of all matching rows (aggregateFunction property).
How to simply display the unit price of a sale without apply any calculation to the unit price column. The None value for the AttributeFunction display BLANK/NULL.
If your unitCost and unitPrice are the same for each product (I mean unitCost can be only 77.6 for product_id = 2), you can just use average (or even emulate average by SUM/COUNT) - but only for product dimension on axis!
Another dimensions will show real average values.
Maybe it's better to use this 'static' fields like attribute properties in 'Product' dimension? But you still need to add some logic to choose one value for several (or all) product members selected.

Resources