MDX, Remove Zero-Rows On ROWS - mdx

My MDX-Query looks like this:
WITH
MEMBER [YEAR].[A2014_1-12] AS
([YEAR].[2014], [TYPE].[ACTUAL]),
MEMBER [YEAR].[P2014_1-12] AS
([YEAR].[2014], [TYPE].[PLAN]),
MEMBER [YEAR].[P2015_1-12] AS
([YEAR].[2015], [TYPE].[PLAN]),
MEMBER [YEAR].[A2015_YTD] AS
SUM(
{[YEARPERIOD].[201501]:[YEARPERIOD].[201509]}
,(
[Measures].currentmember
,[TYPE].[ACTUAL]
)
)
SELECT
{
[YEAR].[A2014_1-12],
[YEAR].[P2014_1-12],
[YEAR].[P2015_1-12],
[YEAR].[A2015_YTD]
}
ON COLUMNS,
NON EMPTY
[COST_CENTER]
*[COST_ELEMENT] ON ROWS
FROM [CUBE]
WHERE ([Measures].[COSTS]);
The Query itself is working. However it is also returning a lot of zero-rows and I would like to get rid of these zero-rows. The strange thing is - If I put only one MEMBER (e.g. [YEAR].[A2014_1-12]) on the COLUMNS-axis it is not returning any zero-rows. But as soon as I start to add another MEMBER to the COLUMN-axis it is also many returning zero-rows (It seems like it is ignoring the NON EMPTY in my CROSSJOIN). How can I remove the zero-rows in the above MDX-Query?

Try this as a start
WITH
MEMBER [YEAR].[A2014_1-12] AS
iif(
([YEAR].[2014], [TYPE].[ACTUAL]) = 0
,null
,([YEAR].[2014], [TYPE].[ACTUAL])
)
MEMBER [YEAR].[P2014_1-12] AS
iif(
([YEAR].[2014], [TYPE].[PLAN]) = 0
,null
,([YEAR].[2014], [TYPE].[PLAN])
)
SELECT
NON EMPTY
{
[YEAR].[A2014_1-12],
[YEAR].[P2014_1-12]
}
ON COLUMNS,
NON EMPTY
[COST_CENTER]
*[COST_ELEMENT] ON ROWS
FROM [CUBE]
WHERE ([Measures].[COSTS]);
As an aside you do not need to define the current member of the measures hierarchy in this definition:
MEMBER [YEAR].[A2015_YTD] AS
SUM(
{[YEARPERIOD].[201501]:[YEARPERIOD].[201509]}
,(
[Measures].currentmember
,[TYPE].[ACTUAL]
)
)
This is enough:
MEMBER [YEAR].[A2015_YTD] AS
SUM(
{[YEARPERIOD].[201501]:[YEARPERIOD].[201509]}
,([TYPE].[ACTUAL])
)
Try this instead:
WITH
MEMBER [YEAR].[A2014_1-12] AS
iif(
([YEAR].[2014], [TYPE].[ACTUAL]) = 0
,null
,([YEAR].[2014], [TYPE].[ACTUAL])
)
MEMBER [YEAR].[P2014_1-12] AS
iif(
([YEAR].[2014], [TYPE].[PLAN]) = 0
,null
,([YEAR].[2014], [TYPE].[PLAN])
)
SET [X] AS
NonEmtpy(
[COST_CENTER]*[COST_ELEMENT],
{[YEAR].[A2014_1-12],[YEAR].[P2014_1-12]}
)
SELECT
NON EMPTY
{
[YEAR].[A2014_1-12],
[YEAR].[P2014_1-12]
}
ON 0,
//NON EMPTY
[X] ON 1
FROM [CUBE]
WHERE ([Measures].[COSTS]);

Related

Mondrain MDX for records between date range where start and end date being separate columns

I want to search date between two columns of same dimension. Columns name is start date and end date. I tried every thing, following query give me this error : Mondrian Error:Internal error: Cannot deduce type of call to function ':'
MDX query :
WITH
SET [~ROWS] AS
Hierarchize
(
{
{[Location_Cluster.default].[All Location_Cluster.defaults]}
,{[Location_Cluster.default].[Location_Cluster].MEMBERS}
}
)
MEMBER [Measures].[QTY Percent] AS
[Measures].[Total_Quantity]
/
(
[Measures].[Total_Quantity]
,[Location_Cluster.default].[All Location_Cluster.defaults]
)
,format_string = '0.00%'
MEMBER [Measures].[Revenue Percent] AS
[Measures].[Total_Revenue]
/
(
[Measures].[Total_Revenue]
,[Location_Cluster.default].[All Location_Cluster.defaults]
)
,format_string = '0.00%'
MEMBER [Measures].[Margin Percent] AS
[Measures].[Total_Margin]
/
(
[Measures].[Total_Margin]
,[Location_Cluster.default].[All Location_Cluster.defaults]
)
,format_string = '0.00%'
SELECT
NON EMPTY
{
[Measures].[Sku_Count]
,[Measures].[Total_Quantity]
,[Measures].[QTY Percent]
,[Measures].[Total_Revenue]
,[Measures].[Revenue Percent]
,[Measures].[Total_Margin]
,[Measures].[Margin Percent]
} ON COLUMNS
,NON EMPTY
[~ROWS] ON ROWS
FROM [APCS_SALES_CUBE]
WHERE
(
{[Date].[Start_Date].&[2017-01-01] : NULL}
,{NULL : [Date].[End_Date].&[2017-03-01]}
,[Cluster.Cluster_Id].[2]
,[Taxonomy.default].[Taxonomy_ID].[3]
,[Company.Company_Name].[Compnay_Name].[1]
);
I followed following links to resolve this issue. But don't know how to apply specify function in Mondrian MDX
https://ask.sqlservercentral.com/questions/95478/mdx-for-records-between-date-range-where-start-and.html#answer-142811
https://www.purplefrogsystems.com/blog/2013/04/mdx-between-start-date-and-end-date/#comment-1981
Your query is not well formed. You need to make set out of the range you want to slice. Here is an example:
WITH SET [~ROWS] AS
(${dateFromParameter} : ${dateToParameter})
MEMBER Measures.[number] as
FORMAT([date].[godina].CurrentMember.PROPERTIES("datum"), "dd.mm.yyyy")
SET [ROOT] AS
FILTER([ORG].[org_id].Members, [ORG].[org_id].currentmember.parent.parent=[ORG].[org_id].currentmember.parent)
SELECT
NON EMPTY {[Measures].[total], Measures.[number]} ON COLUMNS,
NON EMPTY [ROOT] * [~ROWS] ON ROWS
FROM [svcs]

Mdx Query Show Year Level Data for all years except This Year

SELECT
NON EMPTY
{[Measures].[AMOUNTCUR]} ON 0
,NON EMPTY
{
NULL : [PRX_UE_DATE].[Calender].[Year].&[2015]
,[PRX_UE_DATE].[Calender].[Year].&[2017]
,Descendants
(
[PRX_UE_DATE].[Calender].[Year].&[2016]
,[PRX_UE_DATE].[Calender].[Month]
)
} ON 1
FROM [PRX_SalesDataModel];
I have a query like above. It's working It returns 2016 on month based others years are year based.
Now I have to add more dimensions on this mdx but when i try it to like
SELECT
NON EMPTY
{[Measures].[AMOUNTCUR]} ON 0
,NON EMPTY
{
NULL : [PRX_UE_DATE].[Calender].[Year].&[2015]
,[PRX_UE_DATE].[Calender].[Year].&[2017]
,
Descendants
(
[PRX_UE_DATE].[Calender].[Year].&[2016]
,[PRX_UE_DATE].[Calender].[Month]
)
*
[PRX UE CLIENT].[SEGMENTID].ALLMEMBERS
} ON 1
FROM [PRX_SalesDataModel];
I get error Members, tuples or sets must use the same hierarchies in the function.
You just need to keep all the date dimension stuff in set braces {...}:
SELECT
NON EMPTY
{[Measures].[AMOUNTCUR]} ON 0
,NON EMPTY
{
NULL : [PRX_UE_DATE].[Calender].[Year].&[2015]
,[PRX_UE_DATE].[Calender].[Year].&[2017]
,
Descendants
(
[PRX_UE_DATE].[Calender].[Year].&[2016]
,[PRX_UE_DATE].[Calender].[Month]
)
}
* [PRX UE CLIENT].[SEGMENTID].ALLMEMBERS
ON 1
FROM [PRX_SalesDataModel];
You could even make it maybe more readable by moving the date stuff into a named set via a WITH clause:
WITH SET [DatesSet] AS
{
NULL : [PRX_UE_DATE].[Calender].[Year].&[2015]
,[PRX_UE_DATE].[Calender].[Year].&[2017]
,
Descendants
(
[PRX_UE_DATE].[Calender].[Year].&[2016]
,[PRX_UE_DATE].[Calender].[Month]
)
}
SELECT
NON EMPTY
{[Measures].[AMOUNTCUR]} ON 0
,NON EMPTY
[DatesSet]
* [PRX UE CLIENT].[SEGMENTID].ALLMEMBERS
ON 1
FROM [PRX_SalesDataModel];
You've got your answer from #whytheq on why the error cropped up. There is just one part which I felt is not handled yet. You are building the set of years by hand. Instead of that, it can be made dynamic like below -
with set AllYearsExceptCurrent as
filter(
[PRX_UE_DATE].[Calender].[Year].members as yr,
yr.current.item(0).member_value <> Format(Now(), "yyyy")
)

How to build a dynamic MDX formula for a calculated member?

I am trying to create a Calculated Member to get a sum up to last week.
No problem to get the week value (I need it to be two digits i.e. '05', so adding 100-1 )
with
Member [Measures].Week as
'right(str(int(99+datepart ( ''ww'', Now()))),2)'
-- That works as expected
member [Measures].SalesUpToWeek as
'strtomember(
"aggregate(periodstodate([Dim].[2015],[Dim].[2015].[" + ([Measures].Week) + "]),[Measures].[Sales])")'
I get the literal value
aggregate(
periodstodate([Dim].[2015],[Dim].[2015].[25])
,[Measures].[Sales]
)
What I need is the value of this MDX calculation.
All other attempts end up with a syntax error. Just as an example
member [Measures].SumToWeek as
'aggregate(
periodstodate(
[Dim].[2015],[Dim].[2015].[' + strtomember([Measures].Week) + '])
,[Measures].[Sales])'
Error
Lexical error at line 2, column 0. Encountered: after : "[\n"
Any idea?
[Measures].Week is already a string entity. You don't need to put StrToMember around it. Instead you should have it outside the string you dynamically defined.
with
Member [Measures].Week as
"right(str(int(99+datepart ( ''ww'', Now()))),2)"
member [Measures].SumToWeek as
aggregate(
periodstodate(
[Dim].[2015],
StrToMember("[Dim].[2015].[" + [Measures].Week + "]")
)
,
[Measures].[Sales]
)
This is your error:
strtomember([Measures].Week)
Let us say that [Measures].Week is equal to 15 then you are trying to do this:
strtomember(15)
So there are two errors in the above:
You're feeding a numeric into a functions that converts Strings to Memebers
You need to feed in the full string representation of the member i.e."[Dim].[2015].[15]"
Maybe try putting the strToMember function around the string that is the representation of the member:
MEMBER[Measures].SumToWeek AS
'aggregate(
periodstodate(
[Dim].[2015],
strtomember('[Dim].[2015].[' + [Measures].Week + ']', constrained)
)
,[Measures].[Sales])'
Here is the MSDN reference for the function strtomember:
https://msdn.microsoft.com/en-us/library/ms146022.aspx?f=255&MSPPError=-2147217396
Edit
Looking at this previous post: (StrToMember does not accept calculated measure (mdx))
...you need to create the member before feeding it into the new measure, so:
WITH
MEMBER [Measures].[Week] AS
Right
(
Str(Int(99 + Datepart('ww',Now())))
,2
)
MEMBER [Dim].[2015].[TargetWeek] AS
StrToMember
(
'[Dim].[2015].[' + [Measures].Week + ']'
,constrained
)
MEMBER [Measures].SumToWeek AS
Aggregate
(
PeriodsToDate
(
[Dim].[2015]
,[Dim].[2015].[TargetWeek]
)
,[Measures].[Sales]
)
Edit2
Ok if you wish to use PeriodsToDate then we need to use StrToSet and then use the member in this set inside the function. This is because custom members lose their family ties and are therefore useless inside some mdx functions.
Here is a working script in AdvWrks illustrating the approach I'm suggesting:
WITH
MEMBER [Measures].[Wk] AS
Right
(
Str(Int(99 + Datepart('ww',Now())))
,2
)
SET [TargetWeek] AS
StrToSet
(
'[Date].[Calendar Weeks].[Calendar Week].[Week ' + cstr([Measures].[Wk]) + ' CY 2007]'
)
MEMBER [Measures].[SumToWeek] AS
Aggregate
(
PeriodsToDate
(
[Date].[Calendar Weeks].[Calendar Year]
,[TargetWeek].item(0).item(0)
)
,([Measures].[Internet Sales Amount])
)
SELECT
{[Measures].[SumToWeek]} ON 0,
[Product].[Product Categories].[All] ON 1
FROM [Adventure Works];
I got an interesting suggestion on Pentaho forums that pointed me to a nice blog post which allowed to write an elegant solution:
http://diethardsteiner.blogspot.com.es/2009/10/current-date-function-on-mondrian.html
with
member [Measures].sm as aggregate(
periodstodate(
currentdatemember([Dim],'["Dim"]\.[yyyy]'), -- current year
currentdatemember([Dim],'["Dim"]\.[yyyy]\.[ww]').lag(1) -- previous week
),
[Measures].[Sales])
Thks

MDX with IIF parameter

I'm tyring to discern an amount based on a parameter. I currently have the #ClientType defaulted to 'C'.
WITH
MEMBER
[Measures].[Amount] AS
IIF(
#ClientType ='C',
[Measures].[Total Client Amount] ,
-[Measures].[Total Client Amount]
)
Regardless of changing the #ClientType I'm still only returning the FALSE.
Try adding in these two measures inspect their values:
WITH
MEMBER [Measures].[ClntTypParam] AS #ClientType
MEMBER [Measures].[ClntTypParamC] AS
IIF (
#ClientType = "c"
,"equalToC"
,"notC"
)

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];