mdx. Filter values from fact table before aggregate - mdx

I have the simple cube with 2 dimensions: Dim1, Dim2, and one measure - value (aggregator = sum)
I need to get cross table via mdx:
select non empty [Dim2].members on 0, non empty [Measures].value on 1
from [cube]
=>
el1 el2 el3 el4
value 12 14 45 64
this values is aggregate by dim1. ok.
Next i create calculate measure:
with [Measures].value_filter as iif([Measures].value> 15, [Measures].value, null)
select non empty [Dim2].members on 0,
non empty [Measures].value_filter on 1
from [cube]
=>
we get the correct result:
el3 el4
value 45 64
But, how can i filter cells by non aggregate values of [Measures].value, ie real values from database?

You can only filter by members that are contained in a dimension. If you want to filter on something, it has to be available in the cube. hence you may potentially have to add some columns as attributes to one of your dimensions.
You would do filtering as follows, assuming you want to use Member1 of hierarchy/attribute Hier2 of dimension Dim2 as the filter:
select non empty [Dim2].members on 0,
[Measures].value on 1
from [cube]
where ([Dim2].[Hier2].[Member1])

Related

MDX - Sum one measure at leaf lvl where another measure exists at leaf lvl

I need to work out Stockturn at any lvl in an item hierarchy.
So far the calc is SUM the last 3 months [COGS] * 4 divided by [SOH] (SOH Value)
The below is embedded in a BI tool and works. It is in the Item grid and filters for every member in the Item dimension at the level display:
((SUM({.lag(3):.lag(1)}, ([Type].[Type].[Actual], [Measures].[Cogs]))) * 4) /
([Type].[Type].[Actual], [Measures].[SOH], )
What it needs changed/added is only returning [SOH] for Items where the [COGS] measure has values.
So if I have items that do not have COGS then do not include them:
Item
SOH
COGS
A
10
20
B
15
40
C
20
Do not include C as it will throw the calc.
The simplest approach - assuming you are using SSAS 2012 or newer - would be to use Divide instead of the division operator /:
Divide( ((SUM({.lag(3):.lag(1)}, ([Type].[Type].[Actual], [Measures].[Cogs]))) * 4),
([Type].[Type].[Actual], [Measures].[SOH], )
)
Divide returns NULL (empty value) instead of infinity when dividing by zero or null.
Edit: I see you want to avoid the calculation of the numerator is null or 0, not the denominator. In this case, you would use Iif:
IIF((SUM({.lag(3):.lag(1)}, ([Type].[Type].[Actual], [Measures].[Cogs])) <> 0,
((SUM({.lag(3):.lag(1)}, ([Type].[Type].[Actual], [Measures].[Cogs]))) * 4)
/
([Type].[Type].[Actual], [Measures].[SOH], )
, NULL)
Iif is a function with three arguments, a condition, the value to use if the condition is true, and the condition to use if the condition is false.

MDX Query - Non Empty on 1 Axis

I am trying to eliminate a null value from a result set. I am querying only first level dimension data to add to my selection box.
There is a #null value that gets returned with the query.
select {} on columns,
NON EMPTY{[Markets].[All Markets].Children}) on rows
from [SteelWheelsSales]
And the above query does not work since its not against any measure. But i want only the list Markets in the first level to be displayed in my selection box.
Rather than NON EMPTY try NonEmpty:
SELECT
{} ON 0,
NonEmpty([Markets].[All Markets].Children) ON 1
FROM [SteelWheelsSales];
Try filtering instead,
SELECT
{} ON COLUMNS,
Filter( [Markets].[All Markets].Children, not IsEmpty ( [Measures].[Sales]) ) ON ROWS
FROM [SteelWheelsSales];

DAX formula for - MAX of COUNT

I have the below dataset:
using the measure:
BalanceCount := COUNT(Balances[Balance])
which gives me the result:
However, I want the Grand Total to show the maximum amount of the BalanceCount, which is 2.
NewMeasure:=
MAXX(
SUMMARIZE(
FactTable
,FactTable[Account]
,FactTable[MonthEnd]
)
,[BalanceCount]
)
SUMMARIZE() groups by the columns specified, and MAXX() iterates through the table specified, returning the maximum of the expression in the second argument evaluated for each row in its input table.
Since the filter context will limit the rows of the fact table, we'll only have the appropriate subsets in each column/row grand total.
I found a solution that works for this particular case. It will not work if columns other than Account and MonthEnd are included in the filter context.
MaxBalanceCount:=
MAXX ( SUMMARIZE (
Balances,
Balances[Account],
Balances[MonthEnd]
),
CALCULATE ( COUNTROWS ( Balances ) )
)

How can I get MIN and MAX values for dimension members in a set that uses NON EMPTY

I have a problem where I need to get the high and low values for a set from the dimension members (not the measures) for a specific intersection (one customer and time period). We need to reference these for report parameters downstream. I can only find examples for pulling the min and max measures. I need the actual dimension values.
Any ideas?
Assuming the KEY0 is a numerical value, you could do something like :
select
topCount( [Rent Range].[Rent Range Floor].members, 1, [Rent Range].[Rent Range Floor].currentMember.properties( 'KEY0', TYPED ) )
+ bottomCount( [Rent Range].[Rent Range Floor].members, 1, [Rent Range].[Rent Range Floor].currentMember.properties( 'KEY0', TYPED ) )
on 0
from [Sales]
Otherwise any other numerical property would be fine.
I had a similar requirement, see if the following helps you out.
SELECT TOPCOUNT(NONEMPTY(DESCENDANTS([Hit Time].[Date], 1, LEAVES)),1) ON 0
FROM [cube]
For the max you case use TopCount(your set, 1, your measure)
For the min you case use BottomCount(your set, 1, your measure)

Adding an extra row to an MDX result set

I have a very simple MDX query that retuns the contents of a dimension.
I would like to inject one more row to the result set as part of the MDX.
Is this possible?
You can create a calculated member of the dimension. Suppose I have 4 members of region built into my cube: East, West South and Central.
SELECT
{[Profit].[Sales]} ON COLUMNS,
NON EMPTY [Market].Generations(2).Members ON ROWS
FROM [Basic]
will give me the sales over the 4 regions.
If I add a dummy calculated member to the region, I can get one extra row of results.
WITH
MEMBER [Market].[Dummy] AS
'0',
SOLVE_ORDER = 0
SELECT
{[Margin].[Sales]} ON COLUMNS,
NON EMPTY Union(
[Market].Generations(2).Members,
{[Market].[Dummy]}) ON ROWS
FROM [Basic]
no, it has to be a member of some dimension