MDX multiple conditions filter on multiple dimensions - mdx

I am relatively new to MDX, about a month or so, and I am now writing MDX queries against a remote cube I also work on (Java 8 ActivePivot).
This query works when they are ORs, but when I add parentheses and change that first OR to an AND it works, kind of, but drops the measure resulting in the CDR and BOOK dimensions coming back correctly. Can someone with more MDX knowledge tell me what I have missed or do not yet know?
WITH
Member [Measures].[CDR_Label] AS [CDR].[CDR].CURRENTMEMBER.MEMBER_CAPTION
Member [Measures].[Book_Label] AS [Book].[Book].CURRENTMEMBER.MEMBER_CAPTION
SELECT
NON EMPTY
{[Measures].[CDR_Label],
[Measures].[Book_Label],[Measures].[RepoRate.LATEST]}
ON COLUMNS,
NON EMPTY
FILTER(
([CDR].CHILDREN,[Book].CHILDREN), (LEFT([CDR].[CDR].CURRENTMEMBER.MEMBER_CAPTION,1) = "8") AND
(LEFT([Book].[Book].CURRENTMEMBER.MEMBER_CAPTION,2) = "ST" OR
RIGHT([Book].[Book].CURRENTMEMBER.MEMBER_CAPTION,4) = "CIES"))
ON ROWS
FROM [TraderCube]
WHERE ([Date].[Date].[2019-10-23])
And here the query before I changed it, with OR OR etc which works. I wanted the above to return only the CDR beginning with 8, which it does, but it loses the measure (and also breaks my headers - as this is actually being fired from inside an xll / custom Excel function, to a dll calling the remote cube using AdomdClient package as I am building a very custom plugin - Essentially the ability for users to use simple words/from enumerations presented and I then translate and construct MDX in C# to fire at the cube. Data comes back and I send 2D arrays back to Excel :)).
WITH
Member [Measures].[CDR_Label] AS [CDR].[CDR].CURRENTMEMBER.MEMBER_CAPTION
Member [Measures].[Book_Label] AS [Book].[Book].CURRENTMEMBER.MEMBER_CAPTION SELECT NON EMPTY {[Measures].[CDR_Label],[Measures].[Book_Label],[Measures].[RepoRate.LATEST]}
ON COLUMNS,
NON EMPTY
FILTER(
([CDR].CHILDREN,[Book].CHILDREN),
LEFT([CDR].[CDR].CURRENTMEMBER.MEMBER_CAPTION,1) = "8" OR
LEFT([Book].[Book].CURRENTMEMBER.MEMBER_CAPTION,2) = "ST" OR
RIGHT([Book].[Book].CURRENTMEMBER.MEMBER_CAPTION,4) = "CIES")
ON ROWS
FROM [TraderCube]
WHERE ([Date].[Date].[2019-10-23])

Aah...ignore me!
I think I was tired and didn't see the pattern. Essentially, and I just reproduced this, if you ask for two dimension patterns that do exist with a measure, you get back the dimensions you expect (in my case 3, a real measure and two fake / labels for the dimensions to bring them all back as rows).
If there are no matches it seems to return you back what you asked for without any measures as they do not exist for the dimension combination.

Related

Add row number/rank to ssas tabular model

I'm trying to add row numbers (by some sorting column) or at least ranks into a calculated table inside azure analysis services tabular model (I need this rank column to use in TOPNSKIP, it should be precalculated column in the model's table. Really it will not be precalculated column for one sorting column only. It will be some expression I want to have precalculated). I used some thoughts from here DAX - Get current row number
I use Visual Studio with Microsoft Analysis Services Projects extension to deploy the model to ssas and SSMS to debug the requests. But it seems there are differences between dax requests in the SSMS and in the tabular models definitions.
The SSMS uses evaluate syntaxis and the tabular constructor = VAR RETURN. And it seems some functions are not available for the tabular models. It seems like ADDCOLUMNS and RANKX are not available for tabular models. Also , is used as operand separator in the SSMS and ; in the model DAX editor, (possible due to some internationalization options on my computer) and that is not very convenient. I could not find a way to get error report for the bad DAX request for the model definition. I only see for incorrect request that has been retrieved 0 records while I deploy a model and if I try to do some request to a table after deploying I get error message that it's empty because of incorrect construction, but no details. There is also a possibility to use calculated columns during the table construction, but I could not embed the RANKX function to the formula for them. Also I don't know how to use a MEASURE inside a construction function. The filter approach from the mentioned stackoverflow question was very slow on my amount of data. I was waiting about half an hour and stopped the deploying.
Because I don't see the ADDCOLUMNS works I've tried GENERATE and SELECTCOLUMNS. For example:
=
VAR NewTable = GENERATE(BaseTable; ROW("RowNumber"; RANKX(BaseTable; BaseTable[SortName];;1)))
RETURN NewTable
It works nice in the SSMS but can't be used to construct a table. While:
=
VAR NewTable = GENERATE(BaseTable; ROW("RowNumber"; 50))
RETURN NewTable
works fine everywhere. So I think RANKX is not working in the tabular model definitions.
Is there any way to rank or number rows in ssas calculated table by some sorting column?
Adding high cardinality columns such as an index number column is generally not advisable for tables in a tabular model, since these columns compress very very badly, thereby reducing query performance. Especially if the table is large. But if you must do it, use the technique shown here. If you don't want to add a calculated column to an existing table, you can wrap everything in a call to ADDCOLUMNS:
=
ADDCOLUMNS(
BaseTable,
VAR CurrentSortName = BaseTable[SortName]
RETURN
COUNTROWS(
FILTER(BaseTable, BaseTable[SortName] < CurrentSortName)
) + 1
)

Can someone explain the following Essbase code: FIX, #relative

Can someone please explain the below Essbase code to me please? This is my first time looking at any Essbase code and I'm getting a bit confused as to what it is actually doing.
FIX(&Mth, &Yr, &Version,
"Sector1","Sector2", #relative("Source Code",0), #relative("Channel", 0) )
FIX("AccountNo","DepNo")
DATACOPY "1A11"->"A-500" TO "1BCD"->"C-800";
ENDFIX
ENDFIX
From what I have googled the following is my understanding:
Creates a new command block which restricts database calculations to this subset.
Passes the following members into the command to be used:
Mth
Yr
Version
Returns the following fields:
Sector1
Sector2
returns the 0-level members of the Source Code member - meaning it returns the members of the Total Source Code without children (no other dimensions)
returns the 0-level members of the Channel member - meaning it returns the members of the Channel without children (no other dimensions)
Begins a new command block and passes the following members into the command to be used:
AccountNo
DepNo
Copies the range of cells 1A11, A-500 over to the range 1BCD, C-800
The above is what I understand from the oracle documents on each of the functions, but I can't actually figure out what is happening.
Welcome to the world of Essbase; it can be a little daunting at first especially if you're new to the concept of multidimensionality. You are on the right track regarding analyzing your calc script.
Try not to think of the FIX statement as a command block, per se. A FIX is used to select a portion of cells in the cube. Every single piece of data in your cube has a particular address that consists of one member from every dimension, plus the actual data value itself. For instance, a cube with the dimensions Time, Year, Scenario, and Location might have a particular piece of data at Jan->2018->Actual->Washington. The number of possible permutations of data in a cube can quickly get very large. For instance, if you're organization has 4 years of data, 12 months in a year, 100 locations, 10000 accounts, 3 versions, and 10 departments, you are talking about 4 * 12 * 100 * 10000 * 3 * 10 = 1.4 billion different potential addresses (cells) of data – and that's actually fairly small for a cube, as they tend to grow much larger.
That said, FIX statements are used to narrow down the scope of your calculation operation, rather than operating on the ENTIRE cube (all 1.4 billion cells in my hypothetical example), the FIX essentially restricts the calculation to cells that match certain criteria you specify. In this case, the first FIX statement restricts the calculation to a particular month, yr, version, sectors, sources, and channels. Note that the ampersand on Mth, Yr, and Version means that a substitution variable is to be used. This means your server or cube has a substitution variable value set, such as the variable Mth = "Jan" and Yr = "FY2018" and Version might be "Working" or "Final" or something similar. I would guess that Sector1 and Sector2 are possibly two different members from the same dimension. #RELATIVE("Source Code", 0) is a function that finds the level-0 members (leaf/bottom-level members in a dimension, that is, members that do not have children below them) of the specified member.
In other words, the first FIX statement is narrowing the scope of the calculation to a particular month in a particular year in a particular version (as opposed to all months, all years, all versions), and for that particular month/year/version (for either Sector1 or Sector2) it is fixing on all of the level-0/bottom/leaf members in Source Code and Channel dimensions.
The next FIX statement just further narrows the current scope of cells to calculate on in addition to the outer FIX. It's not uncommon to see FIX statements nested like this.
Lastly we get to the part where something actually happens: the DATACOPY. In the given FIX context, this DATACOPY command is saying that for EACH cell in the current FIX, copy values from the source to the destination. DATACOPY is a little more straightforward when it's just DATACOPY "Source" TO "Target" as opposed to using the inter dimensional operator (->)... but this is perhaps more easily understood in terms of the time/year dimensions. For example, imagine the data copy was written like this:
DATACOPY "FY2018"->"Dec" TO "FY2019"->"Jan";
In this DATACOPY I'd be telling Essbase that for the given FIX context I would like to copy values from the end of the year (data values where the year is FY2018 AND the month is Dec) to the beginning of the next year (data values where the year is FY2019 AND the month is Jan). Your DATACOPY is working in a similar fashion, but using cost centers or something else. It all just depends on how the cube is setup.

Showing measure values as "not applicable" which are not related to dimensions

I have a requirement where report should show measures as "not applicable" if one selects a attribute which is not linked to that measure Group.
1) unrelateddimesnion= 'false' is not solving my problem because i have few default members.
2) I could able to show measure value as "not applicable " by Writing this MDX statement
([Customer].[customer name].[customer name], [measures].[sales forecast]) = 'not applicable'
but with this i have to repeat the same line for each and every attribute present in the dimension ( and also for each and every measure present in the measure Group)
can someone help me Writing the MDX for entire dimension instead for individual attribute. Thanks in advance.
Kind Regards
Mah
Bad news! An MDX script on your cube can't reference such a sub-cube in a simple way. You may have seen the LEAVES(dimension) function for a scope statement but that won't work when one attribute in a dimension has the [All] level and another has a selection. (That is to say the function returns the leaves of the dimension's key attribute). What you can do is use nested scope statements with the outer one filtering down to the list of measures you want to affect. That will at least save you typing a formula num_attributes * num_measures times. The scope statement may even accept the MEASUREGROUPMEASURES function. (When I last used that it only returned visible measures but that's probably what you want anyway.)
It may be easier to link measure group and dimension and let your data sit on the UNKNOWN member. (Or an explicit dummy member.) Then filters against or slices to real customer hierarchy values will exclude your [Sales Forecast] rows and show it as null. That's not something I've done and it'll have ramifications for error processing and you'll have to allow users sight of the unknown or dummy member. So recommend you play with the idea before you rely on it.
I hope this helps some.

What does an MDX query return?

I just started learning SSAS and cannot understand the basic idea.
What happens when query fixes fewer dimensions than cube has?
All examples usually present queries where intersection of dimensions gives either a point or an axis; in the former case we have the value and in the latter one we get some aggregated value.
Yet I cannot understand what happens when fixed dimensions produce a cube with fewer dimensions. What will be the result of such query?
I'll try to explain it simplier, then the previous answer.
In SELECT statement you define a 'space' more like mathematical space of result. The whole cube is being projected on that space using aggregation functions.
If you want to project part of the cube you use a WHERE clause.
That's the key defference between SQL which was hard for me to grasp in the beginning:)
An MDX query actually returns a cube - well a sub-cube.
I suppose this is enough to show it in action:
SELECT
NON EMPTY
{
[Measures].[Internet Order Count]
} ON COLUMNS
,NON EMPTY
{
[Sales Territory].[Sales Territory].[Country].MEMBERS
*
[Date].[Calendar].[Month].ALLMEMBERS
} ON ROWS
FROM
( //>>>>>>following is a sub-select>>>>>
SELECT
[Date].[Calendar].[Month].&[2008]&[1] ON COLUMNS
FROM [Adventure Works]
);
The section I've marked is a sub-select which returns a cube. That cube is then further queried by the outer script.
If a cube is returned then why do we just see a table of values? Returning a cube and what is actually visible are two different things. All dimensions are used whenever we run an mdx script - if we do not explicitly use the dimension in our script then it is evaluated at the [(All)] level. So even when only a small table is shown by say ssms all the dimensions are being returned by the script and then only certain aspects are being made visible via what is specified in your script.

Aggregation of an MDX calculated measure when multiple time periods are selected

In my SSAS cube, I've several measures defined in MDX which work fine except in one type of aggregation across time periods. Some don't aggregate (and aren't meant to) but one does aggregate but gives the wrong answers. I can see why, but not what to do to prevent it.
The total highlighted in the Excel screenshot below (damn, not allowed to include an image, reverting to old-fashion table) is the simplest case of what goes wrong. In that example, 23,621 is not the grand total of 5,713 and 6,837.
Active Commitments Acquisitions Net Lost Commitments Growth in Commitments
2009 88,526 13,185 5,713 7,472
2010 92,125 10,436 6,837 3,599
Total 23,621 23,621
Active Commitments works fine. It is calculated for a point in time and should not be aggregated across time periods.
Acquisitions works fine.
[Measures].[Growth in Commitments] = ([Measures].[Active Commitments],[Date Dimension].[Fiscal Year Hierarchy].currentMember) - ([Measures].[Active Commitments],[Date Dimension].[Fiscal Year Hierarchy].prevMember)
[Measures].[Net Lost Commitments] = ([Measures].[Acquisitions] - [Measures].[Growth in Commitments])
What's happening in the screenshot is that the total of Net Lost Commitments is calculated from the total of Acquisitions (23,621) minus the total of Growth in Commitments (which is null).
Aggregation of Net Lost Commitments makes sense and works for non-time dimensions. But I want it to show null when multiple time periods are selected rather than an erroneous value. Note that this is not the same as simply disabling all aggregation on the time dimension. The aggregation of Net Lost Commitment works fine up the time hierarchy -- the screenshot shows correct values for 2009 and 2010, and if you expand to quarters or months you still get correct values. It is only when multiple time periods are selected that the aggregation fails.
So my question is how to change the definition of Net Lost Commitments so that it does not aggregate when multiple time periods are selected, but continues to aggregate across all other dimensions? For instance, is there a way of writing in MDX:
CREATE MEMBER CURRENTCUBE.[Measures].[Net Lost Commitments]
AS (iif([Date Dimension].[Fiscal Year Hierarchy].**MultipleMembersSelected**
, null
, [Measures].[Acquisitions] - [Measures].[Growth in Commitments]))
ADVthanksANCE,
Matt.
A suggestion from another source has solved this for me. I can use --
iif(iserror([Date Dimension].[Fiscal Year Hierarchy].CurrentMember),
, null
, [Measures].[Acquisitions] - [Measures].[Growth in Commitments]))
CurrentMember will return an error when multiple members have been selected.
I didn't understand much of the first part of the question, sorry...but at the end I think you ask how to detect if multiple members from a particular dimension are in use in the MDX.
You can examine either of the two axes as a string, and use that to form a true/false test. Remember you can use VBA functions in Microsoft implementations of MDX.
I suggest InStr(1, SetToStr(StrToSet("Axis(1)")), "whatever") = 0 as a way to craft the first argument of your IIF.
This gets the set of members on axis number one, converts it to a string, and looks to see if a certain string is present (it returns the position of that string within the other). Zero means not found (so it returns true). You may need to use axis zero instead, or maybe check both.
To see if multiple members from the same dimension were used, the test string above would have to be more complicated. You want to know if whatever occurs once or twice. You could test if the first occurance of the string was at the same position as the last occurance (by searching backwards); though that could also mean the string wasn't found at all:
IIF(
InStr(1, bigstring, littlestring) = InStrRev(bigstring, littlestring),
'used once',
'used twice or not at all'
)
I came across this post while researching a solution for my own issue with grand totals of calculated measures over time when filters are involved. I think you could have fixed the calculations instead of suppressing them by using dynamic sets. This worked for me.