mdx with aggregate last periods not working - ssas

I am using Analysis Services on SQL server 2014, enterprise edition.
I am trying to create a new member for Timeframe dimension that aggregates last 3 months values. The following mdx code gets compiled without errors, but when I browse it with a measure, it still shows me the month level numbers, instead of last three month sum. What am I doing wrong? Any help is appreciated.
CREATE MEMBER CURRENTCUBE.[Timeframe].[Timeframe].[ROLLING 3 MONTH]
AS IIF([Accounting Date].[Accounting Date].CurrentMember.Level.Name="Month", AGGREGATE(LASTPERIODS(3),[Timeframe].[Timeframe].&[1]), NULL),
VISIBLE = 1;
Thanks.

Please try the following:
CREATE MEMBER CURRENTCUBE.[Timeframe].[Timeframe].[ROLLING 3 MONTH]
AS
IIF(
[Accounting Date].[Accounting Date].CurrentMember.Level.Name="Month",
AGGREGATE(
LASTPERIODS(3, [Accounting Date].[Accounting Date].CurrentMember),
[Timeframe].[Timeframe].&[1]
),
NULL),
VISIBLE = 1;
Specifying the member in the LASTPERIODS function is hopefully what you were missing. SSAS was probably assuming some other Date hierarchy or you don't have any hierarchies and dimensions properly marked. That's ok. Just specify the member and then it should work.
By the way, for performance reasons I would prefer to see a SCOPE statement instead of checking the level's name. What you have should work but if you hit performance issues research SCOPE statements or create a new thread asking to optimize that MDX.
I assume you are using Excel 2010 or higher. If not watch out!

Related

Is there any ways to dynamic cumulative measure in MDX?

All of the measure that I want to cumulative has the same formula. So, is there any way to use the thing like function or any thing in calculate measure to resolve this issue?
There are two ways to achieve your aim:
1- the first solution is based on using the business intelligence wizard to add time intelligence to your solution.
The time intelligence enhancement is a cube enhancement that adds time calculations (or time views) to a selected hierarchy. This enhancement supports the following categories of calculations:
List item
Period to date.
Period over period growth.
Moving averages.
Parallel period comparisons.
The wizard will let you chose the calculations and measures you want to apply.
Visit : https://learn.microsoft.com/en-us/analysis-services/multidimensional-models/define-time-intelligence-calculations-using-the-business-intelligence-wizard
Visit : http://www.ssas-info.com/analysis-services-articles/62-design/2465-ssas-time-intelligence-wizard
2- Use a dimension table to calculate your calculations, this solution is more complicated, but very powerful and one of the best practices.
The first step is to create a new physical dimension, with real
members for each of the calculations we're going to need. We don't
actually need to create a table in our data warehouse for this
purpose, we can do this with an SQL view like this
CREATE VIEW DateTool AS SELECT ID_Calc = 1, Calc = 'Real Value' UNION ALL SELECT ID_Calc = 2, Calc = 'Year To Date'
Next, we need to add this view to our DSV and create a dimension based
on it. The dimension must have one hierarchy and this hierarchy must
have its IsAggregatable property set to False. The DefaultMember
property of this hierarchy should then be set to the Real Value
member. Giving this dimension a name can be quite difficult, as it
should be something that helps the users understand what it does –
here we've called it Date Tool. It needs no relationship to any
measure group at all to work.
Our next task is to overwrite the value returned by each member so
that they return the calculations we want. We can do this using a
simple SCOPE statement in the MDX Script of the cube:
this code let you create the YEAR-TO-DATE aggregation for all your measures.
SCOPE ([Date Tool].[Calculation].[Year To Date]); THIS = AGGREGATE ( YTD ([Date Order].[Calendar].CurrentMember), [Date Tool].[Calculation].[Real Value]); END SCOPE;
Visit:https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781849689908/6/ch06lvl1sec35/calculation-dimensions

SSAS - No of Working Days as Named Calculation

I am new to SSAS. I have a requirement, I need to calculate no of working days between user selected date range (either in Excel or SSRS or PowerBI). I found the MDX query, I need assistance with create a named calculation with MDX expression.
Date Dimension (Filtered):
MDX:
WITH MEMBER Measures.WorkingDays AS
COUNT
(
exists( EXISTING {[Dim Date].[Date].[Date].members}
, [Dim Date].[Is Weekday].&[1] )
)
Select {Measures.WorkingDays} on 0 ,
[Dim Date].[Month].[Month] on 1
from [Project Cube]
where ([Dim Date].[Date].&[2018-01-01T00:00:00]:[Dim Date].[Date].&[2018-04-25T00:00:00])
I need to add this named column on Fact table as measurement. I am having trouble with the below items:
Creating named query with MDX expression mentioned.
Adding a [Number of Working Days] as measure in Fact table.
Please correct me, If I am doing it in wrong way. My requirement is I need a [NoOfWorkingDays] as measure in fact table, so that I can use SSAS aggregate to use it as input on other measure, such as ([utilization%] = ([ActualDaysWorked] / [NoofWorkingDays]).
Note that, I can do analysis with the given MDX, but I need to deploy it with precalculated values in cube, so that end user can directly use the cube.
Kindly let me know, if more details required, Thank you.
Welcome to SSAS and MDX. Now to the answer.
I need to add this named column on Fact table as measurement. I am
having trouble with the below items:
Creating named query with MDX expression mentioned. Adding a [Number
of Working Days] as measure in Fact table.
You dont need to add it to the Fact table at all. Open your SSAS project, in your object explorer double click your cube. Now on the top left hand you will see a CALCULATIONS tab. In the CALCULATION tab, Click new calculated member, the icon has a calculator on it.
Please correct me, If I am doing it in wrong way. My requirement is I
need a [NoOfWorkingDays] as measure in fact table, so that I can use
SSAS aggregate to use it as input on other measure, such as
([utilization%] = ([ActualDaysWorked] / [NoofWorkingDays]).
If I remember correctly, the calculated members will not be added into the Aggregations, however the underlying measures would be. Secondly if you are wondering that you can use your calculated Measure in another calculated measure. The answer is yes you can use it in another calculated measure. So this is totally possible
> ([utilization%] = ([ActualDaysWorked] / [NoofWorkingDays])
where [utilization%] and [NoofWorkingDays] are calculated measures.

SSAS MDX Except statement issue

I am working on an requirement in Cube MDX like data for year 2008 and prior should not be displayed in report.
So thought of using MDX 'EXCEPT' function in SCOPE statement to exclude the mentioned years.
Time dimension is hierarchy with structure Year-->Quarter-->Month
Initial code was like
SCOPE([Measure Item].[Measure Item].&[SAL]);
SCOPE(EXCEPT({[Time].[Time].[Year]},{[Time].[Time].&[2006],[Time].[Time].&[2007],[Time].[Time].&[2008]}));
this=([Measure Item].[Measure Item].&[SAL]);
END SCOPE;
END SCOPE;
With the code after deploying, data for year 2008 and prior got excluded. But the issue here is, I can only see data on Grand Total on each Year. Please see the example in below screenshot, I cannot see data on Quarter level and month level.
Not able to see data on quarter and month level
Tried few other codes with descendants function, which is working in SSMS but when write it with SCOPE statements its not working.
Code written in SSMS:
select [Measures].[Value] on 0,
EXCEPT({[Time].[Time].members} ,
{descendants([Time].[Time].&[2006],0,SELF_AND_AFTER),descendants([Time].[Time].&[2007],0,SELF_AND_AFTER)}) on 1 from [TOPAS Reporting]
Working in SSMS but not working in SCOPE statement
I can see data on even Quarter and month level. But same code is not working in SCOPE statements of cube MDX.
Please help me to solve this issue and let me know for further details .
Thanks in advance.
Do you really need a scope statement?
You can achieve it by using the following calculation:
Aggregate(
existing [Time].[Time].&[20090101]:NULL,
[Measure Item].&[SAL]
)
where [Time].[Time].&[20090101] is the first day on the Day level.

MDX YTD calculated measure

I'm still new to MDX and I'm trying to get some basic functions to work in my SSAS cube. Can you guys point out what I'm doing wrong here? I've added a calculated measure to my cube with the following code:
CREATE MEMBER CURRENTCUBE.[Measures].[Amount YTD]
AS
AGGREGATE(
YTD([OrderDate].[Calendar].CurrentMember)
,[Measures].[Amount]),
VISIBLE = 1, ASSOCIATED_MEASURE_GROUP = 'MyMeasureGroup';
After that I'm trying to get some data going...
SELECT
NON EMPTY
{
[Measures].[Amount]
, [Measures].[Amount YTD]
} ON COLUMNS,
NON EMPTY
{
[OrderDate].[Month].ALLMEMBERS *
[Product].[Product Group].ALLMEMBERS
} ON ROWS
FROM (SELECT ([OrderDate].[Year].&[2014-01-01T00:00:00]:
[OrderDate].[Year].&[2015-01-01T00:00:00]) ON COLUMNS
FROM [SalesOrderIntake])
This is the output I'm getting:
I'm not seeing any errors in my Output messages, which makes it difficult for me to figure out what is acting up. Hoping you guys can help me out on this one.
FYI: the actual select is just for testing purposes. I just want to get that YTD running. I've tried several things and it always comes out empty, so I was hoping to get some actual errors if I would query it directly in SSMS instead of using a BI tool. Also, the OrderDate dimension is a generated Time dimension which was provided to me by VS.
In your query you're using what looks like an attribute hierarchy:
[OrderDate].[Month].ALLMEMBERS
Whereas the measure uses the user hierarchy:
[OrderDate].[Calendar]
If you use Calendar in your script does it work ok?
#Error usually crops up when there are run time errors in MDX code. I could think of one scenario where the error might crop up. You are using [OrderDate].[Calendar].CurrentMember in the calculated member. But if instead of one, there are multiple members from this hierarchy in scope, it will throw an error.
The below is a scenario from Adventure Works.
with member abc as
sum(YTD([Date].[Calendar].currentmember), [Measures].[Internet Sales Amount])
select abc on 0
from [Adventure Works]
where {[Date].[Calendar].[Date].&[20060115], [Date].[Calendar].[Date].&[20060315]}
P.S. Thanks to #whytheq for teaching me this trick of checking this error by double clicking the cell :) Cheers.
I know, its an old post, but in the interest of posterity..
The correct approach is :
Aggregate
(
PeriodsToDate
(
[OrderDate].[Calendar].[Fiscal Year]
,[OrderDate].[Calendar].CurrentMember
)
,[Measures].[Amount]
)

Why is my attempt to fully qualify an MDX calculated member failing? (Mondrian)

This is Pentaho 3.8 CE which I think comes with Mondrian 3.5
This MDX produces a result:
WITH MEMBER [Items].[ItemType].[NonSales] as
[Items].[ItemType].[Deposit] + [Items].[ItemType].[Gift_Voucher]
SELECT
{[Items].[NonSales]} ON COLUMNS,
[Measures].[AmtPaidExclGST] on rows
FROM [sales]
(the Dimension is Items. It has one Hierarchy, unnamed, and one level called ItemType.)
However, this doesn't:
WITH MEMBER [Items].[ItemType].[NonSales] as
[Items].[ItemType].[Deposit] + [Items].[ItemType].[Gift_Voucher]
SELECT
{[Items].[ItemType].[NonSales]} ON COLUMNS,
[Measures].[AmtPaidExclGST] on rows
FROM [sales]
The difference is that successful Select Row uses a less-qualified version of the name of the calculated member.
My reading of the MDX documentation indicates that a fully qualified member name should work. I expect both queries to produce the same result.
It does work for a real member. This works.
SELECT
{[Items].[ItemType].[Deposit]} ON COLUMNS,
[Measures].[AmtPaidExclGST] on rows
FROM [sales]
I'm new to MDX and this is driving my crazy because I doubt my understanding of how to write member names.
Some old version of mondrian does not allow you to add member to level other than the root level (example of a root level member is ALL in some dimension). Possibly mondrian is adding calculated member (you can query using the dimension) but not able to associate to level (your query using [query 2]).
Thanks