Dynamic use of MDX AVG function - mdx

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).

Related

Conditional styles on a calculated value in Cognos

I have a report (Pivot table) for which I have a value everyday. My goal is to calculate the variance every day and highlight with conditional styles.
1 jan. 23
2 jan. 23
3 jan. 23
5
25
42
I have a query who calculates the variance between days in % (which I dont want to put in the report).
My conditional style rule is [Table].[Pourcentage variation D-1] between 0,05 and 10.
Since my request is not in displayed my report, I have this error :
RSV-VAL-0032 The following expression is not valid. If the item exists
in a query but is not referenced in the layout, add it to a property
list.
The problem is that I don't have the "Proprety list" in my pivot table like in can have in a Data table.
How can I highlight my variance based on a request that I dont display in my Pivot table ?
Thanks for helping
Add the calculated field to the pivot table (I know you do not want this in the layout, this is just to test if that removes the error). See if the conditional style works the way you expect.
If so, change the column for the percentage data item to property, set box type to none. This way it is still something that can be referred to, but is hidden from the final result

How to calculate dynamic % of grand total as a measure on Power BI?

I have the below table connected into Power BI and I am looking for ways to create a formula calculating % of grand total of the Rating column and further subtracting with targets for each rating. For example, the % of grand total for Rating 1 is 3 divided by 7 (42.86%). The most important part of the formula is the denominator which has to remain at a total level and dynamic for any filters applied to either Grade or BU columns. For example, denominator at a total level would be 7 and when filtered down to Academy BU should be 3.
Sample Data Table:
Rating Target Table:
I want the end result to look like this,
I have used the following formula to achieve this,
Measure created: % of total calc = DIVIDE(COUNT('Table'[Rating]),CALCULATE(SUM('Table'[Count]),'Table'[Rating]))
To make the above formula work I had to add an extra column and include ones in it (see below)
I want to know if there are other ways of achieving this outcome?
ALLEXCEPT will produce such result to exclude used dimensions and include mandatory filters such as date with one condition, rating, date, any dimension must be in the same table.

SSAS Cube - Excel Drill through not filtered as required

I have a SSAS DSV similar to following structure:
Id Type Special
1 A 1
2 B Null
3 A Null
4 C 1
5 C Null
I built a dimension for this DSV including one attribute for Type.
Then I have in my cube three measures
Measure1: Count of rows
Measure2A: Sum of Special
Measure2B: Count of non-empty values for Special
Finally in Excel, I display data as following:
Rows --> Type attribute
Values --> Measure1 / Measure2A / Measure2B
When I look at the results, everything is correct.
For instance, I get a count of 1 for measure2A and measure 2B for row = C
BUT when I attempt to drill through for related cells, instead of getting 1 row, I get 2 (the ones where type = C without considering the value of Special)
I guess I am doing something wrong in my design of the cube but cannot understand what.
When determining what rows to show in drillthrough SSAS only considers the dimension context not which detail rows have a non null measure value.
You could add a new dimension on the Special column and add that dimension as a filter to your PivotTable.
Or you could install ASSP and construct a custom rowset action that fires an MDX query which does a NON EMPTY on your measure.
http://asstoredprocedures.codeplex.com/wikipage?title=Drillthrough&referringTitle=Home

No sum() with null values

I'm looking for a solution to create sums of +- 10 scores and targets of a product over 6 different dimensions. There are some more i won't bother you with. Of every dimension I need a total. For example
SalesPeriod. Product: Bikes. Dimensions: bmx, size, colours, with bars etc. Targets: 1,2,3,4,5. Scores:1,2,3,4,5.
So 10 totals for bmx bikes with size x, colour red and bars, and 10 totals for bmx bikes, size x, colour red etc etc.
However, every score needs to be calculated only when none of the underlying values is a null. For example score 1 contains a null then no calculation, but score 2 does not contain a null thus should be calculated.
At this point the calculation is done via a case statement which basically checks the values of within each column/score and only calculates the total when the count of scores is equal to the expected rows.
The calculation requires a lot of cpu and with a larger dataset this is very inefficient and it simply takes too long.
I'm looking for a solution that will be much more effecient. What could be my best option to try?
You can filter (or first group by) the products with Non Null values only first by using your same count method. I don't think there is any other method.
SELECT columnid, SUM(column1)
FROM table
GROUP BY columnid
HAVING COUNT(column1)=COUNT(*);
Then you can join it on columnid with another similar query on another columnN as well.
(I'm not sure if understood your problem completely, but you basically want an efficient query with sum(scores) and sum(targets) only when they are not null? or only when they are both not null? or only scores? or only targets?)

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