How to get previous month data in MDX - reporting-services

I am trying to get previous month data for my SSRS report.But i am not getting any result.Could you please give any help.Below is my query.
SELECT
NON EMPTY
{[Measures].[Trade Net Amount]} ON COLUMNS
,NON EMPTY
{
[Fund].[Fund Name].[Fund Name].ALLMEMBERS*
[Transaction Type].[Transaction Description].[Transaction Description].ALLMEMBERS*
[Calendar Day].[Year].[Year].ALLMEMBERS*
[Calendar Day].[Month].[Month].ALLMEMBERS
}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM
(
--FROM ( SELECT ( STRTOSET(#CalendarDayMonth, CONSTRAINED) ) ON COLUMNS
--FROM ( SELECT ( STRTOSET(#CalendarDayYear, CONSTRAINED) ) ON COLUMNS
SELECT
StrToMember
(
"[Calendar Day].[Calender Hierarchy].[Month].[" + Format(Now(),"yyyy")
+
Format
(
Now()
,"MMM"
)
+ "].PrevMember"
) ON COLUMNS
FROM [Escher_Hybrid]
);

Related

Order by dimension mdx

I have this MDX:
WITH MEMBER [Measures].[InventarioSobreVentas] AS [Measures].[InventarioDirecto_Monto]/[Measures].[VentasInventarioDirecto_Ventas]*100 SELECT NON EMPTY {
[Measures].[InventarioDirecto_Monto],
[Measures].[VentasInventarioDirecto_Ventas], [Measures].[InventarioSobreVentas] } ON COLUMNS, NON EMPTY { (
[Dim_Tiempo_].[Anio].[Anio].ALLMEMBERS
*[Dim_Tiempo_].[NombreMesAbreviado].[NombreMesAbreviado].ALLMEMBERS
* [Dim_Tiempo_].[Mes].[Mes].ALLMEMBERS
* [Dim_Tiempo_].[Dim_Tiempo_].[Dim_Tiempo_].ALLMEMBERS
* [Dim_PlantaConsolidada_].[Grupo].[Grupo].ALLMEMBERS
* [Dim_PlantaConsolidada_].[IdGrupo].[IdGrupo].ALLMEMBERS
* [Dim_PlantaConsolidada_].[Unidad].[Unidad].ALLMEMBERS
* [Dim_PlantaConsolidada_].[IdUnidad].[IdUnidad].ALLMEMBERS
* [Dim_PlantaConsolidada_].[sNombrePlantaConsolidada].[sNombrePlantaConsolidada].ALLMEMBERS
* [Dim_PlantaConsolidada_].[keyIdPlantaConsolidada].[keyIdPlantaConsolidada].ALLMEMBERS
* [Dim_ConceptosInventario_].[Dim_ConceptosInventario_].[Dim_ConceptosInventario_]
)}
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_VALUE, MEMBER_UNIQUE_NAME ON ROWS
--FROM ( SELECT ( {[Dim_PlantaConsolidada_].[keyIdPlantaConsolidada].[All] } ) ON COLUMNS
FROM ( SELECT ( STRTOSET(#DimPlantaConsolidadaSNombrePlanta, CONSTRAINED) ) ON COLUMNS
FROM ( SELECT ( STRTOSET(#DimPlantaConsolidadaIdUnidad, CONSTRAINED) ) ON COLUMNS
FROM ( SELECT ( STRTOSET(#DimTiempoAnio, CONSTRAINED) ) ON COLUMNS FROM [BSC]
)))
--))) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
As you can see I have column called:
* [Dim_Tiempo_].[Mes].[Mes].ALLMEMBERS
and result come as:
Values 1 to 12, problem is I need to order it ascending like, 1,2,3,4,5,6,7,8,9,10,11,12 but mdx is so complicated, I try to use ORDER filter but I always get error. Is not possible to order by Dimension?
You can put ORDER around all the rows set:
...
NON EMPTY
ORDER(
[Dim_Tiempo_].[Anio].[Anio].ALLMEMBERS
*[Dim_Tiempo_].[NombreMesAbreviado].[NombreMesAbreviado].ALLMEMBERS
*[Dim_Tiempo_].[Mes].[Mes].ALLMEMBERS
*[Dim_Tiempo_].[Dim_Tiempo_].[Dim_Tiempo_].ALLMEMBERS
*[Dim_PlantaConsolidada_].[Grupo].[Grupo].ALLMEMBERS
*[Dim_PlantaConsolidada_].[IdGrupo].[IdGrupo].ALLMEMBERS
*[Dim_PlantaConsolidada_].[Unidad].[Unidad].ALLMEMBERS
*[Dim_PlantaConsolidada_].[IdUnidad].[IdUnidad].ALLMEMBERS
*[Dim_PlantaConsolidada_].[sNombrePlantaConsolidada].[sNombrePlantaConsolidada].ALLMEMBERS
*[Dim_PlantaConsolidada_].[keyIdPlantaConsolidada].[keyIdPlantaConsolidada].ALLMEMBERS
*[Dim_ConceptosInventario_].[Dim_ConceptosInventario_].[Dim_ConceptosInventario_]
,[Dim_Tiempo_].[Mes].CURRENTMEMBER.MEMBER_CAPTION //<<OR [Dim_Tiempo_].[Mes].CURRENTMEMBER.MEMBERVALUE
,BASC
)
ON ROWS
...

mdx select data from period

For report based on OLAP I need to exctract data for period of current year and parallel of previous year.
If today is 2016-05-26,
Then I need these period of dates: 2016-01-01 - 2016-05-26 and 2015-01-01 - 2015-05-26
SELECT NON EMPTY
{ [Measures].[Sell In Return] } ON COLUMNS,
NON EMPTY { (
[Date].[Date].[Date].ALLMEMBERS *
[Product].[Prod Name].[Prod Name].ALLMEMBERS *
[Product].[Product].[Product].ALLMEMBERS *
[Date].[Month Calendar].[Month Calendar].ALLMEMBERS
) }
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS
FROM
[Table]
Here is a start for finding Today in the cube. I've moved Measures to the slicer and Today onto columns:
WITH
MEMBER [Measures].[Key for Today] AS
Format
(
Now()
,'yyyyMMdd'
)
MEMBER [Measures].[Today string] AS
'[Date].[Month Calendar].[Calendar Day].&[' + [Measures].[Key for Today]
+ ']'
SET [Today] AS
StrToMember
(
[Measures].[Today string]
,constrained
)
SET [TodayLstYr] AS
ParallelPeriod
(
[Date].[Date - Calendar Month].[Calendar Year]
,1
,[Today]
)
SET [ThisYear] AS
{
Ancestor
(
[Today]
,[Date].[Month Calendar].[Calendar Day]
).Item(0)
:
[Today]
}
MEMBER [Date].[Date - Calendar Month].[All].[ThisYear] AS
Aggregate([ThisYear])
SET [LastYear] AS
{
Ancestor
(
[TodayLstYr]
,[Date].[Month Calendar].[Calendar Day]
).Item(0)
:
[TodayLstYr]
}
MEMBER [Date].[Date - Calendar Month].[All].[LastYear] AS
Aggregate([LastYear])
SELECT
NON EMPTY
{
[Today]
,[Date].[Date - Calendar Month].[All].[ThisYear]
,[Date].[Date - Calendar Month].[All].[LastYear]
} ON COLUMNS
,NON EMPTY
[Product].[Prod Name].[Prod Name].ALLMEMBERS
*
[Product].[Product].[Product].ALLMEMBERS ON ROWS
FROM [Table]
WHERE
[Measures].[Sell In Return];
Here's one more approach of getting at it:
with set Today as //today's date
filter([Date].[Month Calendar].[Date].members,
Format([Date].[Month Calendar].currentmember.member_name, "Short Date") =
format(now(), "Short Date"))
set MonthBegToToday as //range of dates from current month's beginning till today
{today.item(0).firstsibling : today.item(0)}
set LastYearMonthBegToToday as //range of dates from current month's beginning till today (using parallelperiod function to get the "same date last year")
PARALLELPERIOD([Date].[Month Calendar].[Year], 1,
today.item(0).firstsibling)
:
PARALLELPERIOD([Date].[Month Calendar].[Year], 1,
today.item(0))
member Measures.SalesMonthBegToToday as //total sales by using SUM()
sum(MonthBegToToday, [Measures].[Sell In Return])
member Measures.SalesLastYearMonthBegToToday as
sum(LastYearMonthBegToToday, [Measures].[Sell In Return])
Thanks for help
I define 2 periods with member and add range onto columns
WITH
MEMBER [Measures].[Key for Today] AS Format (Now() ,'yyyyMMdd' )
member [measures].[CurrentYear] as Format (Now() ,'yyyy')
MEMBER [Measures].[TodayPrevYear] AS Format (DateAdd("YYYY",-1,Now()) ,'yyyyMMdd')
Member [measures].[PrevYear] as Format ( dateadd("YYYY",-1,Now()) ,'yyyy')
SELECT
NON EMPTY
{
StrToMember ( '[Date].[Date Key].[Date Key].&[' +[measures].[PrevYear]+'0101'+ ']',constrained )
:StrToMember ( '[Date].[Date Key].[Date Key].&[' + [Measures].[TodayPrevYear] + ']',constrained )
,
StrToMember ( '[Date].[Date Key].[Date Key].&[' +[measures].[CurrentYear]+'0101'+ ']',constrained )
:StrToMember ( '[Date].[Date Key].[Date Key].&[' + [Measures].[Key for Today] + ']',constrained )
} ON COLUMNS
,NON EMPTY
[Product].[Product].[Product].ALLMEMBERS
* [Product].[SKUs].[SKU].ALLMEMBERS
ON ROWS
FROM [Table]
WHERE
[Measures].[Sell In Return];

Cross tab (Pivot) query in SQL

I am using a pivot query in SQL as below:
SELECT classification, [BSL] AS BSL, [AQ] AS AQ, [KYN] AS KYN, ([BSL] + [AQ] +
[KYN]) AS CR_TTL
FROM
(
SELECT classification, Shed
FROM PunctualityMain WHERE Date >= '4/1/2012' AND Date <= '6/30/2012'
) x
PIVOT
(
COUNT(Shed)
FOR Shed IN ([BSL], [AQ], [KYN])) p ">
In result i am getting 0 in null cells, is it possible to show null in place of 0 in result page?
the simplest solution would be to use the NULLIF shortcut, like so:
SELECT classification
, NULLIF([BSL],0) AS BSL
, NULLIF([AQ],0) AS AQ
, NULLIF([KYN],0) AS KYN
, NULLIF([BSL] + [AQ] + [KYN],0) AS CR_TTL
FROM
(
SELECT classification, Shed
FROM PunctualityMain WHERE Date >= '4/1/2012' AND Date <= '6/30/2012'
) x
PIVOT
(
COUNT(Shed)
FOR Shed IN ([BSL], [AQ], [KYN])) p

Parameterized MDX query in SSRS 2008 with Multiple Rank Columns

I'm struggling to figure this one out and was hoping someone out there might be able to help.
I have an SSRS (2008) report that connects directly to an SSAS OLAP Cube. I have 5 different columns, each showing a different measure, broken out by the "Manager" dimension. I also have parametrized dropdowns in SSRS that filter the data based on various things.
Here's the code that got me to this point:
SELECT
NON EMPTY{
[Measures].[Metric A]
,[Measures].[Metric B]
,[Measures].[TMetric C]
,[Measures].[Metric D]
,[Measures].[Metric E]
} ON COLUMNS
,NON EMPTY{
([Ops Hierarchy].[Manager].[Manager].ALLMEMBERS )
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM
(
SELECT
(
STRTOSET(#CompleteDateCalendar, CONSTRAINED) ) ON COLUMNS
FROM
(
SELECT
(
STRTOSET(#City, CONSTRAINED) ) ON COLUMNS
FROM
(
SELECT
(
STRTOSET(#Region, CONSTRAINED) ) ON COLUMNS
FROM
(
SELECT
(
STRTOSET(#Country, CONSTRAINED) ) ON COLUMNS
FROM
[CUBE]
)
)
)
)
WHERE
(
IIF( STRTOSET(#Country, CONSTRAINED).Count = 1, STRTOSET(#Country, CONSTRAINED)
, [Ops Hierarchy].[Division Name].currentmember )
, IIF( STRTOSET(#Region, CONSTRAINED).Count = 1, STRTOSET(#Region, CONSTRAINED)
, [Ops Hierarchy].[Region Name].currentmember )
, IIF( STRTOSET(#City, CONSTRAINED).Count = 1, STRTOSET(#City, CONSTRAINED)
, [Ops Hierarchy].[System Name].currentmember )
, IIF( STRTOSET(#CompleteDateCalendar, CONSTRAINED).Count = 1, STRTOSET(#CompleteDateCalendar, CONSTRAINED)
, [CompleteDate].[Calendar].currentmember )
)
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
And of course, SSRS automagically created the corresponding dropdowns for each #parameter.
Now, here's where I need help:
I have a new requirement to add a "Rank" column for each metric. So, for example, next to the "Metric A" column, there will be another column called "Metric A Rank" that ranks the manager on that row against the other managers that are showing on the report (based on the parameters selected in the dropdowns).
Now, I know that I can add:
WITH
SET [OrderedSet1] AS ORDER(FILTER([Ops Hierarchy].[Manager].MEMBERS,[Measures].[Metric A] <> NULL),[Measures].[Metric A],BASC)
MEMBER [Measures].[Metric A Rank] AS RANK([Ops Hierarchy].[Manager].CurrentMember,[OrderedSet1])
SET [OrderedSet2] AS ORDER(FILTER([Ops Hierarchy].[Manager].MEMBERS,[Measures].[Metric B] <> NULL),[Measures].[Metric B],BASC)
MEMBER [Measures].[Metric B Rank] AS RANK([Ops Hierarchy].[Manager].CurrentMember,[OrderedSet2])
etc, to the top of the MDX query, and then reference [Measures].[Metric A Rank] and [Measures].[Metric B Rank] in my select statement.
What I don't know how to do is filter the sets with the SSRS dropdowns so that I'm ranking against only what is showing on the report.
So for example, if someone has Country="USA", Region = "South", City = "Atlanta" and CompleteDate = "Jan 2012" selected in SSRS, and I rank Metric A against OrderedSet1, then I only want to show how each manager ranks against other managers in Atlanta for Jan 2012.
How do I "parameterize" the sets in the WITH clause using the SSRS dropdowns so that I'm only ranking against the subset that the user has selected?
Thanks in advance for your help!
in Set declaration you can use
SET [OrderedSet1] AS ORDER(FILTER([Ops Hierarchy].[Manager].MEMBERS*
[Ops Hierarchy].[System Name].currentmember
,[Measures].[Metric A] <> NULL),[Measures].[Metric A],BASC)

Passing MultiValue Parameter in MDX

How to pass MultiSelect parameter from SSRS to MDX?
I tried as below which is not working as expected:
WHERE ({IIF( STRTOSET(#Name, CONSTRAINED).Count = 1,
STRTOSET(#Name, CONSTRAINED), [Name].currentmember )})
You can use directly :
WHERE ( STRTOSET(#Name, CONSTRAINED) )
or (not sure about this ) :
WHERE ( IIF( STRTOSET(#Name, CONSTRAINED).Count = 1,
STRTOSET(#Name, CONSTRAINED),
STRTOMEMBER(#Name, CONSTRAINED) ) )
However SSAS and set slicers are not always good friends. If possible use MDX Subselects instead :
WHERE ( SELECT STRTOSET(#Name, CONSTRAINED) ON 0 FROM .. )