How to calculate day difference using mdx? - mdx

Tell me how to calculate the day difference between two date columns in pentaho schema workbench.
Thanks in advance.

Just to add some dynamic functionality to Harsha's answer you can use:
the CURRENTMEMBER function to get hold of dates used on ROWS
you can use VBA date functions to get hold of today's date
So if I run this:
WITH
MEMBER Measures.DD AS
Datediff
('d'
,Cdate([Date].[Calendar].CurrentMember.Member_Caption)
,Cdate(VBAMDX.Now())
)
SELECT
{Measures.DD} ON COLUMNS
,[Date].[Calendar].[Date] ON ROWS
FROM [Adventure Works];
It results in this:

Related

Calculated measure to find the datediff using timedimension

I need to find out the number of days in Month based on Time dimension. Time dimension is Month when Jan is selected it has to return 31 as value.
If you have Time dimension and Time hierarchy, this should work:
WITH MEMBER measures.NumOfDays AS
Count
(
Descendants
(
[Time].[Time].CurrentMember,
,LEAVES
)
)
SELECT Measures.NumOfDays ON 0,
[Time].[Time].Month on 1
FROM [MyCube]
The below sample shows how to get the count.
Please note the below query only show the idea how to do this. Your cube will note have these attributes you you need to replace them
with member
measures.t as Count(([Date].[Month of Year].&[1],[Date].[Day of Month].[Day of Month].members))
select {measures.t}
on columns
from [Adventure Works]

MDX: return last value for selected items in Power BI

This is a question regarding SSAS Cubes, MDX formulas and Power BI.
I have a measure with the active members per each month. So when I select for example 2018 it shouldn´t aggregate but return the last available month with active members, and if I break down by month it should give the active members for each month.
So I have this formula which works almost fine if querying in MS Management Studio:
with member [Measures].[Last existing SOCIOS] AS
Max(
EXISTING [DIM FECHA].[Jerarquía].[MES NOMBRE].members,
iif([Measures].[ACTIVOS] = 0,null,
[Measures].[ACTIVOS])
)
select {[Measures].[Last existing SOCIOS]} on columns,
[DIM FECHA].[MES NOMBRE].members on rows
from [cubo_Compromisos]
where [DIM FECHA].[AÑO].&[2018]
I would prefer to have the november value returned at the 'All' level. But this is not my main problem. The real issue is that when I use this measure in Power BI it behaves differently: when selecting multiple months it ignores the selected values and just returns the last value for the whole year.
In the screenshot below I have added the value returned by the KPI Card because that is the value that I want returned:
If I select items like this it does it right, but I need it to select all months, and not just one because I am using this measure along others:
Does anyone know the right MDX function to use or an alternative?
Edited: 23-11-2018
It does the same in a Pivot Table connected to a SSAS Cube.When I add the date dimension to the table it works fine. But when using the date dimension and filtering it without the dimension added as rows it returns the value for the whole year.
The function you are looking at is LastChild. Last Child on the upper level of the hierarchy will return the value you are looking at.
I think that function can be used in the Cube design in SSAS - then this will be the standard behavior. If you want to do it with a query you need to do something like:
SELECT [Date].[Fiscal].[Fiscal Quarter].[Q1 FY 2002].LastChild ON 0
FROM [Adventure Works]
To get the last month of the 1st quater (I used example from microsoft and another post on the subject )

Need date values which are 90 days or more from current date in MDX

I have a dimension [Date].[Last Met].
I need to pull out values which are more than 90 days from current date using MDX.
Please suggest the best way.
You can filter like this:
FILTER
(
[Date].[Last Met].MEMBERS,
Datediff("d",[Date].[Last Met].CurrentMember.Name, Format(Now(),'yyyyMMdd') <=90
)
A much more elegant option would be to create a calculated column in DSV called RollingLast90Days, and using SQL datediff logic to assign it 1/0. Once in place, you would need to just have a slicer :
...WHERE ([Time].[[RollingLast90Days].&[1])
Above is based on asumption you would process cube daily. If not apply the same logic in a calculated measure.
IIF(
Datediff("d",[Date].[Last Met].CurrentMember.Name, Format(Now(),'yyyyMMdd') <=90,
1,
null)
and then using this slicer on HAVING or in WHERE clause.

How to filter time hierarchy based on anothr attribute's value in MDX

I want to create a calculated member. I have a time dimension hierarchy" YQM", which has Year/Quarter/Month. The calculated member should find YTD till the current month. However, the value of current month will come from database.
I have created a attribute, "CP" which returns the current month value.
Please tell me how to create the calculated member. Please note "CP" is not in the same hierarchy YQM.
I don't understand why you are using Aggregate. So, I will assume that it is there for some purpose, which you have't clarified. Now, if the members of [YQM].[Month] and [Dim].[CP] are of the same format, then you can try the code below:
Aggregate(
PeriodsToDate([Dim].[YQM].[Month].MEMBERS,
FILTER([Dim].[YQM].[Month].MEMBERS,
[YQM].[Month].CURRENTMEMBER.member_value = [Dim].[CP].FirstChild.member_value)
)
)
Why not try playing with the function YTD. The following should give you the monthly amount in the first column of data and then the YTD amount in the next column:
WITH MEMBER [Measures].[YearToDate] AS
AGGREGATE(
YTD()
,[Measures].[someMeasureInCube]
)
SELECT
{
[Measures].[someMeasureInCube],
[Measures].[YearToDate]
} ON 0,
[Dim].[YQM].[Month].MEMBERS ON 1
FROM [yourCube]

How can I write an mdx query that slices by both a date range and dimension member value

I need to write an mdx query that limits its results by the value of a dimension but also by a date range. I know how to do one or the other but I can't figure out how to do both at once.
This works for the date range:
SELECT {[Measures].[Hours]} ON COLUMNS, [Time Type].[Type].Members ON ROWS
FROM [cube]
WHERE {[Date].[Date ISO].[2013-01-26]:[Date].[Date ISO].[2013-06-25]}
And this works for the member slicer:
SELECT {[Measures].[Hours]} ON COLUMNS, [Time Type].[Type].Members ON ROWS
FROM [cube]
WHERE [Time Type].[Allocation Type].[Direct]
How do I constrain the results by both of these WHERE clause values at the same time? I've tried putting them both in the same WHERE like so:
SELECT {[Measures].[Hours]} ON COLUMNS, [Time Type].[Type].Members ON ROWS
FROM [cube]
WHERE ([Time Type].[Allocation Type].[Direct],
{[Date].[Date ISO].[2013-01-26]:[Date].[Date ISO].[2013-06-25]})
but Mondrian replies with: No function matches signature (, ).
Note that Mondrian does not support subqueries or I would do it that way.
I think I might need to use a filter function on my rows for my member constraint but I need to filter on something that I don't want to display, which I am not sure how to do.
I think crossjoin is the answer like so:
SELECT {[Measures].[Hours]} ON COLUMNS, [Time Type].[Type].Members ON ROWS
FROM [cube]
WHERE CROSSJOIN([Time Type].[Allocation Type].[Direct],
{[Date].[Date ISO].[2013-01-26]:[Date].[Date ISO].[2013-06-25]})
CROSSJOIN creates all the combinations of the 'Direct" member and the dates in my range as tuples for my WHERE slicer. I think this is the right answer.