MDX error in calculated MEASURES - mdx

I have this MDX query and I want to run in sql MDX:
WITH
MEMBER [Measures].[NMESACT1] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+01])
MEMBER [Measures].[NMESACT2] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+02])
MEMBER [Measures].[NMESACT3] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+03])
MEMBER [Measures].[NMESACT4] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+04])
MEMBER [Measures].[NMESACT5] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+05])
MEMBER [Measures].[NMESACT6] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+06])
MEMBER [Measures].[NMESACT7] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+07])
MEMBER [Measures].[NMESACT8] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+08])
MEMBER [Measures].[NMESACT9] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+09])
MEMBER [Measures].[NMESACT10] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+10])
MEMBER [Measures].[NMESACT11] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+11])
MEMBER [Measures].[NMESACT12] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+12])
But I always get
Query (156, 44) Parser: The syntax for ',' is incorrect. (WITH
MEMBER [Measures].[NMESACT1] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[+CStr(2018)+01]) etc...
What am I doing wrong? Regards

Ideally you would just build the dynamic query in whatever tool is executing the query. This approach should perform best as the StrToMember function used below will slow down the query.
MEMBER [Measures].[NMESACT1] AS ([Measures].[ValorBase],[BSC Fact].[Periodo].&[201801])
But if for some reason it needs to be a static query with a #Yr parameter where you pass in "2018" you could do:
MEMBER [Measures].[NMESACT1] AS ([Measures].[ValorBase],StrToMember("[BSC Fact].[Periodo].&["+CStr(#Yr)+"01]",CONSTRAINED))
But if you want your query to work without parameters and without the client tool building the query, then the following should work:
MEMBER [Measures].[NMESACT1] AS ([Measures].[ValorBase],StrToMember("[BSC Fact].[Periodo].&["+CStr(2018)+"01]",CONSTRAINED))

Related

Get the descendants of a member

This request always displays '1' in the column 'nb', but I know that for this patient, the number is '3':
WITH
member pat1 AS [DimTransfusion].[TransfusionPatients].[PatientId].&[{09B7106A-xxxx-4B88-80D6-D1283FE990D6}]--83
set transfusPat AS Descendants(pats1,2)
MEMBER nb AS count(transfusPat)
SELECT {nb} ON 0
FROM [BDD PBM]
but this request displays the correct result:
WITH
member pat1 AS [DimTransfusion].[TransfusionPatients].[PatientId].&[{09B7106A-xxxx-4B88-80D6-D1283FE990D6}]--83
set transfusPat AS Descendants([DimTransfusion].[TransfusionPatients].[PatientId].&[{09B7106A-FB68-4B88-80D6-D1283FE990D6}],2)
MEMBER nb AS count(transfusPat)
SELECT {nb} ON 0
FROM [BDD PBM]
I can't understand why using a member variable instead a real value gives a wrong result.
Here is the dimension usage screenshot:
where VPARCOURS and VTRANSFUSION are fact tables(a Transfusion is always related to one VPARCOURS):
thank you.
The problem was due to pat1: being a member, I suppose the Descendants function returns a member of its first argument is a member, and the same for sets:
so the correct declaration is:
set pat1 AS [DimTransfu...

MDX linkmember equivalent in DAX

Is there any equivalent of linkmemeber from MDX to DAX?
I am trying to migrate the following script from MDX to DAX
with
member TauxdetransformationQuartileRegion as ([Measures].[Taux_de_transformation_Region],[Bon Envoi].[Bon Envoi].&[True])
member Annees as [Date Creation].[Année].currentmember.name
select {
[Measures].[Tx_Real_Meilleur_Region]
,TauxdetransformationQuartileRegion
} on 0
FROM ( select strtoset(#Region) on 0 from test)
WHERE (
linkmember(strtotuple(#DateFin),[Mois Publication].[Mois de publication])
,{strtotuple(#DateDebut):strtotuple(#DateFin)}
,[Perimetre Ebusiness].[Périmètre E-Business].&[O]
,{[MER_Publication].[Detail Type Visiteur].&[Prospect],[MER_Publication].[Detail Type Visiteur].&[Client direct]}
,( - { [Origine_Marketing].[Entree Parcours].&[SiteAgent] } )
)
MDX query parameters are typically strings with member unique names ("[Dim].[Attr].&[Key]") which you must convert to a member with StrToMember(#Param). And if you need to switch hierarchies you the use LinkMember.
DAX parameters are just the values. So the parameter will work against any dimension. Thus LinkMember isn’t needed.
Here is an article about DAX parameters. For Date type parameters I forget if you just say 'Date'[Date] = #DateParam or if you have to say 'Date'[Date] = DATEVALUE(#DateParam). My recollection is that it depends on whether the parameter is Date type (use the first approach) or String type (use the DATEVALUE approach).

icCube - function calling another function results in NULL

Below the MDX statement on icCube. (Note that icCube has a non-native language component, called function).
with function article_list() as topcount([Product].[Product].[Article], [amount], 10)
function benchmark_best_index2(i) as sum(order(topcount([Product].[Product].[Article], [amount], 10), [measures].[amount], desc).(i-1) , [measures].[amount])
// why doesnot the following function work?
function benchmark_best_index(list,i) as sum(order(list, [measures].[amount], desc).(i-1), [measures].[amount])
member [measures].[bm_top_amount_doesnotwork] as benchmark_best_index(article_list(),1)
member [measures].[bm_top_amount_doesnotwork_either] as benchmark_best_index( topcount([Product].[Product].[Article], [amount], 10),1)
member [measures].[bm_top_amount_works] as benchmark_best_index2(1)
select { [measures].[amount],[measures].[bm_top_amount_doesnotwork], [measures].[bm_top_amount_doesnotwork_either], [measures].[bm_top_amount_works]} on 0
,article_list() on 1
from sales
I cannot get the calculated measure [bm_top_amount_doesnotwork] and [bm_top_amount_doesnotwork_either] to work.
My idea is to have 2 generic functions, with the 2nd one calling the first one. The end result is a benchmark value that can be used for charts, calculations and so on.
I see this is not possible form the above stated MDX. But is it possible? And if YES, how?
We've to check in detail what went wrong (issue) in the meantime you can force the type of the parameter so the MDX parser knows it is a set :
sum(order( {list} , [measures].[amount], desc).(i-1), [measures].[amount])

MDX currentMember get me error

This MDX function raise error :
with member measures.x as
(
[Date].[Date].[Date].&[20160101] : [Date].[Date].[Date].currentmember.value
,
[Measures].[current balance]
)
select measures.x on columns,
[Branch].[HeadOffice Branch].[Head Office Code] on rows
from MyCube
Error is :
The CURRENTMEMBER function expects a hierarchy expression for the 1 argument. A member expression was used.
But when I use
[Date].[Date].[Date].&[20160101] : [Date].[Date].[Date].&[20160131]
It works good
why?
This is valid mdx:
[Date].[Date].[Date].&[20160101] : [Date].[Date].currentmember
Although this is not:
WITH member measures.x AS
(
[Date].[Date].[Date].&[20160101] : [Date].[Date].currentmember
,
[Measures].[current balance]
)
It is not valid because round braces (...) signify a tuple but a tuple requires exact co-ordinates so the first argument cannot be a set.
You could add an aggregation such as SUM:
WITH member measures.x AS
SUM
(
[Date].[Date].[Date].&[20160101] : [Date].[Date].currentmember
,
[Measures].[current balance]
)
This is valid mdx but if you do not have [Date].[Date] in context i.e. in the WHERE or SELECT clause then all it is returning is the All member.
Why are you using [Date].[Date].currentmember ?
CURRENTMEMBER works on an attribute hierarchy (in your case [Date].[Date]). So [Date].[Date].CURRENTMEMBER will work.
Also you will need to take out the value selector, as your expression returns a set of dates (member : member returns a set of all the members between those two members).

Mondrian MDX error with CDate and DateDiff usage

How could be solved problem below?
ERR: mondrian.olap.fun.MondrianEvaluationException: mondrian.olap.InvalidArgumentException: Mondrian Error:Invalid parameter. expression parameter of CDate function must be formatted correctly
My MDX request:
WITH
MEMBER
[Measures].[diff] as 'datediff("d",CDate([TimeDim.TimeHir].CurrentMember.Name),now())'
....
Have you tried other member properties:
WITH
MEMBER [Measures].[diff] as
'datediff("d",CDate([TimeDim.TimeHir].CurrentMember.MEMBER_VALUE),now())'
or
WITH
MEMBER [Measures].[diff] as
'datediff("d",CDate([TimeDim.TimeHir].CurrentMember.MEMBER_key),now())'
or
WITH
MEMBER [Measures].[diff] as
'datediff("d",CDate([TimeDim.TimeHir].CURRENTMEMBER.Properties('Key0', Typed)),now())'
Edit
This one works for me against AdvWks :
WITH
MEMBER [Measures].[Date_MEMBER_VALUE] as
[Date].[Calendar].CurrentMember.MEMBER_VALUE
MEMBER [Measures].[Diff_MEMBER_VALUE] as
datediff("D",CDate([Measures].[Date_MEMBER_VALUE]),now())
SELECT
{
[Measures].[Date_MEMBER_VALUE]
,[Measures].[Diff_MEMBER_VALUE]
}
ON 0,
tail([Date].[Calendar].[Date],12) ON 1
FROM [Adventure Works];
Edit
From here it looks like .MEMBER_VALUE does not exist in Mondrian so try using the above code with just .VALUE instead: http://mondrian.pentaho.com/documentation/mdx.php
You can use DateSerial function to convert your value to datetime format which is accepted by CDate