MDX divide a set by a measure - mdx

I have a set lets just say:
set [A] as {
([Measures].[X],[somedimension].[A])
[Measures].[Y],[somedimension].[A])
[Measures].[Z],[somedimension].[A])
}
What I need to do is I have to divide this set with a specific value say: [Measures].[P]
Is it possible to do something like this in MDX? If yes then how. Because if I use a normal divide operation it fives an error saying "The Divide function expects a string or numeric expression for the 1 argument. A tuple set expression was used"

SET usually is just a list of items from a dimension. Use FILTER with needed condition to get items which will satisfy it.
WITH
SET [A] AS {Your Set Members}
SET [A WITH P Over 100] AS FILTER([A], [Measures].[P] > 100)
SET [All Others] AS [A] - [A WITH P Over 100] -- Just for example
SELECT { [P] } ON COLUMNS,
{[A WITH P Over 100]} ON ROWS
FROM [Your Cube]
WHERE ([P] < 1000)

Related

How to set outgoing nodes in Cypher?

I have a simple query: given a node A, count the number of nodes that node A has outgoing links, then set that number as a property of A. However I just can't do it.
Attempt 1
MATCH (n)-->(m)
SET n.out=count(m)
RETURN n.name,n.out
This yields the error:
Invalid use of aggregating function count(...) in this context (line 2, column 11 (offset: 28))
"set n.out=count(m)"
^
Attempt 2
MATCH (n)-->(m)
WITH count(m) AS o
SET n.out=o
RETURN n.name,n.out
This yields the error:
Variable `n` not defined (line 3, column 5 (offset: 43))
"SET n.out=o"
^
In both times the errors are in the SET clause. But reading the documentation for SET I cannot identify why these happen.
I cannot count the links because for one pair of n, m there maybe several link types.
You can achieve the desired outcome with the following query:
MATCH (n)
WITH size((n)-->()) as out, n
SET n.out = out
RETURN n.name, n.out

How can I use arrayExists function when the array contains a null value?

I have a nullable array column in my table: Array(Nullable(UInt16)). I want to be able to query this column using arrayExists (or arrayAll) to check if it contains a value above a certain threshold but I'm getting an exception when the array contains a null value:
Exception: Expression for function arrayExists must return UInt8, found Nullable(UInt8)
My query is below where distance is the array column:
SELECT * from TracabEvents_ArrayTest
where arrayExists(x -> x > 9, distance);
I've tried updating the comparison in the lambda to "(isNotNull(x) and x > 9)" but I'm still getting the error. Is there any way of handling nulls in these expressions or are they not supported yet?
Add a condition to filter rows with empty list using notEmpty and assumeNotNull for x in arrayExists.
SELECT * FROM TracabEvents_ArrayTest WHERE notEmpty(distance) AND arrayExists(x -> assumeNotNull(x) > 9, distance)

Filter Set by another Set

I want to filter a set by another set, which is returned by the event #{date}.
The following code only works if the #{date} event returns a single member (e.g. [TIME_DIMENSION].[YEAR].[2010]). Any suggestions how to filter if the event returns more than one member?
WITH SET [A] AS
Filter(
TopPercent(
[PRODUCTS].members - [PRODUCTS].[all],
80,
[Measures].[Sales]
),
#{date}
)
Maybe you could use the NonEmpty function? It would return the set of non empty tuples from the set.
WITH SET [A] AS
NonEmpty(
TopPercent(
[PRODUCTS].members - [PRODUCTS].[all],
80,
[Measures].[Sales]
)
,#{date} * [Measures].[Sales]
)

The Axis0 function expects a tuple set expression for the argument

I just want to get the invoices distinct count of sales on March. I got this MDX query.
My MDX Query:
select
distinctcount([Measures].[Sales #]) on columns,
--[Measures].[Sales #] on columns,
non empty ([Customer].[Store Group].[Store Group]) on rows
from
(
select
{
[Calendar].[Month].[March 2015]
}
ON columns
from [F1_SalesBI])
But I got the following error, whenever I tried executing this.
Executing the query ...
The Axis0 function expects a tuple set expression for the argument. A string or numeric expression was used.
Execution complete
Here is the definition from msdn:
https://msdn.microsoft.com/en-us/library/ms145519.aspx
Syntax:
DistinctCount(Set_Expression)
You're doing this when you use Measures as the argument:
DistinctCount(Numeric_Expression)
Something like the following should hopefully run ok:
WITH
SET [mySet] AS
[Customer].[Store Group].[Store Group] * {[Measures].[Sales #]}
MEMBER [Measures].[setDistCount] AS
DistinctCount([mySet])
SELECT
{
[Measures].[setDistCount]
,[Measures].[Sales #]
} ON 0
,NON EMPTY
[Customer].[Store Group].[Store Group] ON 1
FROM
(
SELECT
{[Calendar].[Month].[March 2015]} ON 0
FROM [F1_SalesBI]
);
To return the number of invoices for the month try the following:
WITH
SET [mySet] AS
{[Calendar].[Month].[March 2015]} * {[Measures].[Sales #]}
MEMBER [Measures].[setDistCount] AS
DistinctCount([mySet])
SELECT
{
[Measures].[setDistCount]
} ON 0
FROM [F1_SalesBI];
If the above returns 1 then try inspecting the set via this script:
SELECT
{} on 0,
{[Calendar].[Month].[March 2015]} * {[Measures].[Sales #]} ON 0
FROM [F1_SalesBI];
Does this just return 1 member?
How about this (without the set braces around the measure)?:
SELECT
{} on 0,
{[Calendar].[Month].[March 2015]} * [Measures].[Sales #] ON 0
FROM [F1_SalesBI];

SQL to MDX conversion

I have this where clause in sql language:
where (cond1=1 or cond2=1) and cond3=1
How can I get this result in MDX with the slicing(condition into the where)?
{[cond1].&[1],[cond2].&[1]} /*and*/ {[cond3].&[1]}
Thanks
Try to use a subcube:
Select
-- YOUR SELECTED MEASURES AND DIMENSIONS
From
(
Select
{[cond1].&[1],[cond2].&[1]} on 0
,{[cond3].&[1]} on 1
-- ,{more slices} on x
From [CubeName]
)
Hope this help!
You can use subcube expression as stated above, but this is not the only option. If you use subcube, you would increase query performance greatly (assuming the fact you don't perform crossjoins in it).
You can also use general WHERE keyword after last expression that returns cube:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where ([Dim2].[Y].&[Y1])
Or:
select
{ .. } on 0,
{ .. } on 1
from (select { [Dim1].[X].allmembers } on 0)
where {[DimTime].[Time].[Year].&[2001] : [DimTime].[Time].[Year].&[2015]}
This is applied at the end of execution, which means performance may decrease. However, if you need to apply external filter to all axis, this is the option you need.
Another way to filter member values is using tuple expressions:
with member LastDateSale as ( (Tail(EXISTING [DimTime].[Time].[Dates].members,1), [Measures].[ActualSales]) )
This will take your DimTime axis, apply external filter, get the last element from it and calculate [ActualSales] for it, if possible.