How to filter measures in MDX - ssas

I need to filter the measure values as
MeasureA MeasureB
10 10
15 15
5 20
20 20
Here I need to get only the measures are not equal, I am using filter function as but not working
Select Filter({[Measures].[A],
[Measures].[B]},
([Measures].[A]-
[Measures].[B])=0)
on 0
from [Cube]
Expected result set
MeasureA MeasureB
5 20
What am I missing?

Try using a dimension instead of your measures for the first part of the filter statement. Assuming you are querying products then your query might look like:
select {[Measures].[A],[Measures].[B]} on columns,
filter ({[Products].Members},[Measures].[A] = [Measures].[B]) on rows
from [Sales Cube]

You might want to try creating a calculated field in the DSV for this Fact table...
CASE
WHEN MeasureFieldA != MeasureFieldB THEN 1
ELSE 0
END
Then you can create a "fact dimension" and have this calculated field as an attribute to be used in your queries or calculated measures.

Related

MDX Using multiple date dimensions

What I want to do is generate a report containing an overview of gains and losses (of contracts). For example:
Year | contract_gains | contract_losses
_______________________________________
2015 | 10 | 2
2016 | 15 | 4
Showing the gains is quite easy because I can just count distinct contracts (which is the aggregator for the measure value) with a start period.
SELECT
{[contract_start_date].[year].MEMBERS}
ON ROWS,
{[Measures].[value]}
ON COLUMNS
FROM Cube
Showing the losses seperately is also easy because I can do the same with the second date dimension.
SELECT
{[contract_end_date].[year].MEMBERS}
ON ROWS,
{[Measures].[value]}
ON COLUMNS
FROM Cube
But what I want to do is generate a report containing both of the values in a single report. Sadly I have no idea how I can do this.
A little slow method, going cell by cell. Does this work for you -
WITH MEMBER [Measures].contract_losses AS
(
GENERATE
(
[contract_start_date].[year].CURRENTMEMBER,
FILTER
(
[contract_end_date].[year].MEMBERS,
CStr([contract_start_date].[year].CURRENTMEMBER.MEMBER_VALUE) = CStr([contract_end_date].[year].CURRENTMEMBER.MEMBER_VALUE)
)
).ITEM(0)
,
[Measures].[value]
)
MEMBER [Measures].contract_gains AS
[Measures].[value]
SELECT
NON EMPTY {[contract_start_date].[year].MEMBERS}
ON ROWS,
{[Measures].contract_gains, [Measures].contract_losses}
ON COLUMNS
FROM [Cube]

Variance column in QlikView

I need to create a "Variance" column in qlikview:
2013 2014 Variance
Measure 1 100 110 10%
Measure 2 105 100 -4.8%
...
Can this be done in Qlikview with just one "Calculated dimension" column that says something like:
[Value for Column2]/[Value for Column 1] - 1
So that it works for any new measures I add in the table and regardless of what the column 1 and column 2 are?
EDIT:
Sample Data:
Year Measure1 Measure2
2012 9750 197
2013 10000 200
2014 11000 210
2015 11500 215
I need the output to be structured as shown below with the Variance column as a calculation between 2 selected Year dimension values.
You can do this with the Column() function.
Column(2) / Column(1)
The number refers to the Expression column -- first Expression is #1, etc. Dimension columns are not counted.
An alternative that is insensitive to column postion, you can use column labels in the expression. Assume expressions with labels of "Sales" & "Margin". The variance expression can be written as:
[Margin] / [Sales]
I am 99% certain there is no built in function to do it.
I've played with a few options I thought might work, but only this is proving of any use.
I've defined the 2 calculated dimensions as variables Year1 and Year2 which I change through an input box. Then the simple calculated dimension =[$(Year2)]-[$(Year1)] gives me a new dimension of the variances.
This assumes the Measure is something that comes from the data, something like this, and now you just want to display it over varying years. I haven't considered what to do if the measures are all expressions.
Here is how to do it using variables and expressions. This will create 2 new columns that will look like a new dimension but will actually be defined by the use of an if() statement.
First step in the script we need to create a dimension that has the Measures in it. this should not associate to any of the column already in the data. (UDR stands for User Defined Report). the dual just allows us to define a sort order that is not alphabetical.
UDR:
load dual(UDR,Sort) as Rows inline [
UDR, Sort
Measure1, 1
Measure2, 2];
the result should be something like this.
Next step is creating the 2 variables for the years.
I do this in the script, but you could use any method of variable creation.
set vMaxYear="=max(Year)";
set vMinYear="=min(Year)";
Now we use the field Rows as the dimension.
And we need to create an expression for the max selected year. Notice that the first if() is testing which rows of the UDR the expression is on and then the definition of the expression for that line is provided. The second if() is testing the year dimension against the variable vMaxYear which will change as selections are made. Min year is the same just replace vMaxYear with vMinYear.
if(Rows='Measure1',sum(if(Year=vMaxYear,Measure1)),
if(Rows='Measure2',sum(if(Year=vMaxYear,Measure2))))
Lastly we use the column() function to calculate the variance in the third expression.
column(1)/column(2)
To make the expression labels dynamic I simply add =vMinYear into the label.
The result is this table, that will respond to my selections in the year list box.
2013 vs 2014
2014 vs 2015

MDX Query Can't connect Fiscal Month/Quarter to my Measures

I've been building an MDX query using excel's powerpivot. I connect to my cube, drag and drop Measures /Dimensions and my query has been working just fine. Up until I try to pull different dimensions.
A simple version of my query:
SELECT
NON EMPTY { [Measures].[EP Projected Impressions] } ON COLUMNS,
NON EMPTY { ([EP Hierarchy].[EP Tactic ID].[EP Tactic ID].ALLMEMBERS ) } ON ROWS
FROM [MI_Cube]
This will return:
(EP Tactic ID) (EP Projected Impressions)
1 10
2 20
3 30
4 40
5 50
Now when I try to pull in date information for each tactic from the Time dimension it just gives me a copy of the above results with each time dimension member.
Example query:
SELECT
NON EMPTY { [Measures].[EP Projected Impressions] } ON COLUMNS,
NON EMPTY { ([EP Hierarchy].[EP Tactic ID].[EP Tactic ID].ALLMEMBERS * [Time].[Fiscal Year].[Fiscal Year].ALLMEMBERS ) } ON ROWS
FROM [MI_Cube]
Results:
(EP Tactic ID) (EP Projected Impressions) (Fiscal Year)
1 10 FY2015
1 10 FY2014
1 10 FY2013
1 10 FY2012
1 10 FY2011
2 20 FY2015
2 20 FY2014
2 20 FY2013
2 20 FY2012
2 20 FY2011
etc....
Does this mean that I cannot pull the Time.FiscalYear dimension for each TacticID? Or do I need to restructure my query? EP Hierarchy has lots of dimension members I can pull successfully, but when I try to pull anything from EP Hierarchy and Time my results get multiplied instead of combined.
Thanks for any advice, trying to wrap my head around cubes and mdx queries.
It seems that you are simply missing a relation between the fact table holding the [EP Projected Impressions] member, and the dimension table holding your [Time] dimension.
By adding a relation between a foreign key on the fact table and the primary key on the dimension table, your measures should get correctly filtered by any attributes you slice on the dimension.
Thank you for the responses, it turns out the measure I was using was not connected to the time dimension. Apparently that was an expected behavior, after trying different measures I am getting the results I was expecting.

Mdx - Flag -Actual

I have two dimensions DimFlag and DimPNL and a fact table FactAmount. I am looking to:
When pnl is stat(Is Stat=1) : sum (Actual x FlagId)
For pnl I multiply the amounts by field FlagId basically if it will be so 0 0 X = 0 ...
DimFlag
FlagId FlagLabel
-----------------
1 NotClosed
0 IsClosed
DimPNL
PNLId PNLName Is Stat
1 a 1
2 test 1
3 test2 0
FactAmount
id PNLId FlagId Actual
1 1 1 100
2 2 1 10
3 3 0 120
I tried the following MDX but it didn't work, any idea please ?
Scope (
[Dim PNL].[PNL].members,[Measures].members
);
this = iif([Dim PNL].[PNL].CurrentMember.Properties("Is Stat") =1
,
aggregate([Dim PNL].[PNL].currentmember,[Measures].currentmember)* iif([Dim Flag].[Flag Label].[Flag Label].currentmember = 0, 0, 1),
aggregate([Dim PNL].[PNL].currentmember,[Measures].currentmember)
);
While this type of calculation can be done in MDX, the MDX can get complex and performs bad. I would suggest to explicitly do the calculation e. g. in the DSV or a view on the fact table that you then use instead of the fact table directly in the DSV. The result of the calculation would then be another column on which you can base a standard measure.
To do it in the DSV, assuming you use a relational table as the base for the fact table, add a named calculation to it, define the column name however you like, and use the expression Actual * FlagID. For the other calculation, you may need a subselect, i. e. the expression would be Actual * case when pnlId in(1,2) then 1 else 0 end. You can use any SQL that works as a column expression in the select list as the expression in for a named calculation.
Implementing the same in a view on FactAmount, you could implement the second expression better, as then you could join table DimPNL in the view definition and thus use column IsStat in the calculation. Then you would replace table FactAmout by the view, which has the two additional measure columns.
In either case, just define two measures on the two new columns in the cube, and you are done.
As a rule, calculations that are done on record level in the fact table before any aggregation should be done at data loading time, i. e. as described above.

Dynamic use of MDX AVG function

Anyone have advice on how to build an average measure that is dynamic -- it doesn't specify a particular slice but instead uses your current view? I'm working within a front-end OLAP viewer (Strategy Companion) and I need a "dynamic" implementation based on the dimensions that are currently filtered in the data view.
My fact table looks something like this:
Key AmountA IndicatorA AmountB Other Data
1 5 1 null 25
2 6 1 null 52
3 7 1 2 106
4 null 0 4 108
Now I can specify a simple average for "[Measures].[AmountA]" with "[Measures].[AmountA] / [Measures].[IndicatorA]" which works great - "[IndicatorA]" sums up to the number of non-null values of "[AmountA]". And this also works great no matter what dimensions are selected in the view - it always divides by the count of rows that have been filtered in.
But what about [AmountB]? I don't have a null indicator column. I want to get an average value of [AmountB] for whatever rows have been filtered in for my current view. If I try to use the count of rows as a simple formula (psuedo-code "[Measures].[AmountB] / Count([Measures].[Key])") I get the wrong result, because it is counting all the null rows in the average.
So, I need a way to use the AVG function to specify the average of [AmountB] over the set of "whatever rows I'm currently filtering in, based on whatever dimensions I'm currently using". How do I specify this dynamic set?
I've tried several different uses of the AVG function and they have either returned null or summed up to huge numbers, clearly not the average I'm looking for.
Thanks-
Matt
Sorry, my first suggestion was wrong. If you don't have access to OLAP cube you can't write any mdx-query for this purpose (IMHO). Because, you don't have any detailed data (from your fact table) in this access level and you can use only aggregated data and dimensions from your cube.
Otherwise (if you have access to olap db), you can create this metric (count of not NULL rows) in your measure group and after that use it for AVG calculation (as calculated member in your cube or in section "WITH" in your mdx-query).