Convert LastNotEmpty Query to Member - sql-server

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]

Related

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.

SSRS MDX for Data from previous two weeks

I am trying to get the data fromt the present calender week to the previous two weeks. If I specify the date range as parameters in the MDX query, the table gets filled up properly.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* { [Date].[Date - CW].[Week].&[2014]&[201420] : [Date].[Date - CW].[Week].&[2014]&[201422]})} ON ROWS FROM [DWH]
But if I try and use the lag function, I get an error. Here is the MDX query.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* [Date].[DATECW - CW].[Week].CurrentMember.Lag(2) ) } ON ROWS
FROM [DWH]
Your first query (quite rightly) identifies a range of two weeks, by using two members from the dimension separated by a colon i.e. [Week X] : [Week Y].
Your second query uses the LAG function, which will return a member on your dimension, not a range of members.
Try this:
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY {[Project].[Projekt-Task].[Task].ALLMEMBERS * TAIL([Date].[Date - CW].[Week],3) } ON ROWS
FROM [DWH]
Also, your first query refers to [Date].[Date - CW].[Week] whereas your second query uses [Date].[DATECW - CW].[Week]. I have gone with the first option as you say that query works.
Let me know if that helps.
Ash
EDIT: Apologies for that. I have amended the query to use the TAIL function, which will give you a number of tuples from a set starting at the end and working backwards. So with TAIL ({Set Of Weeks},3) this should give you the current week and the previous two weeks. I have tested a similar query on my cube and it seems produce the expected results.
Hope that solves it for you.
Ash

MDX Query: Trying to bring back calculated years

I have a piece of a report which currently works fine with hard-coded years, I am trying to make them a bit more dynamic so as to show the current year and previous year returns to avoid having to update this every year. This is a simple thing to do in SQL, but I'm having a much harder time figuring this out in MDX, which I am still learning. The row in question is the [Date].[Year].&[2013]:[Date].[Year].&[2014]. Here is my current query:
SELECT {
[Measures].[Users],
[Measures].[Sessions]
} ON COLUMNS,
(
{[User Type].[Description].&[Customer], [User Type].[Description].&[Vendor]},
[Date].[Year Month].[Year Month],
[Date].[Month Name].[Month Name],
[Date].[Year].&[2013]:[Date].[Year].&[2014]
) ON ROWS
FROM [My Cube]
Thanks for any help.
You can't use a range inside a tuple. You first need to create a member.
Put this in before the SELECT clause:
WITH Member [Date].[Periods] as Aggregate( [Date].[Year].&[2013]:[Date].[Year].&[2014])
and then replace the range in your tuple by [Date].[Periods]

How to pass multivalues parameters in MDX calculated members?

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.

Resources