How to use date range in IIF function in MDX Query - ssas

Am having 2001,2002 and 2003 years of data in the SSAS server.
Without using where clause and filters I need to get the aggregated data using the IIF function in the MDX query for a particular date range.
I have referred to many sites but I didn't find any solutions using date range in the IIF function.
These are the date values am having, so I need to get aggregated Sales Amount Quota values for a particular date range, for example, 01/07/2002 to 30/11/2002(It is dynamic). I need to use this date range in the IIF function.
My expected result is to be $24,381,800 for the given date range.
Can anyone please guide me to use the date range in the IIF function in the MDX query?

Related

Using a date range in calculated member in MDX query for Tabular cube model

I have a requirement where I have to get the values of different measure with each measure having its own date range, the following query works and gets me the data over an year,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Year].&[2019])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
but when I try to pass a date range instead of year, I'm getting an error,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
I'm getting the following error,
Executing the query ... The function expects a string or numeric
expression for the argument. A tuple set expression was used. Run
complete
How do I get the measure for a date range for each calculated member?
If you do not use a tuple (i. e. a combination of single members of the hierarchies like Measures or Year), but a set, then you need to tell MDX what to do with the set elements to get to a single value. I would assume the following will give what you want:
WITH
MEMBER [Measures].[Ex2] AS
Aggregate([Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020],
[Measures].[Average Time Spent]
)
...
Instead of Aggregate, you could also use Sum, Avg, Variance etc. but normally, I prefer using Aggregate, as this uses whichever aggregation method is defined for the measure in the cube, i. e. it is more universal, and the query does not need to be adapted to changes in the cube calculation scripts.
See also Aggregate in the SSAS documentation

How to calculate day difference using 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:

Select and manipulate SQL data, DISTINCT and SUM?

Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'

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.

Date Range issue in MDX SSAS

I have one MDX query.
select [Measures].[Measure1] on columns,
[Locations].[Location].[LocationKey] on rows
from [MeasureLocation]
where
(
{[ServiceDate].[Date].[Date].[01-01-2013] : [ServiceDate].[Date].[Date].[01-01-2014]}
)
Here I gave date range between 01-01-2013 to 01-01-2014. So ideally SSAS should give record for this date range only. But the actual result is for 01-01-2013 to 31-01-2014. So, it's including whole month[31-01-2014].