Sunday, August 3, 2014

RANGE

The Range functionality allows you to define a set of member based on the first and last member of the range. You do this by using the colon “:”.
Fixed Range:
SELECT
[Date].[Calendar Year].[CY 2011]:[Date].[Calendar Year].[CY 2014] // FIXED RANGE
ON COLUMNS,
[Measures].[Sales Amount] ON ROWS
FROM [Adventure Works]
image
Fixed End Range
The first member of the range is replaced by NULL
SELECT
NULL:[Date].[Calendar Year].[CY 2012] //RANGE
ON COLUMNS,
[Measures].[Sales Amount] ON ROWS
FROM [Adventure Works]
image
Fixed Start Range
The last member of the range is replaced by NULL
SELECT
[Date].[Calendar Year].[CY 2012]:NULL //RANGE
ON COLUMNS,
[Measures].[Sales Amount] ON ROWS
FROM [Adventure Works]
image
Dynamic Ended Range
Using the StrToMember function we give a dynamic ending to the range:
SELECT
    {
        [Date].[Calendar Year].[CY 2013]
        :
        StrToMember("[Date].[Calendar Year].[CY "+cstr(Year(now()))+"]")
        } //RANGE
        on COLUMNS
    ,
    [Measures].[Sales Amount]
    ON ROWS
from[Adventure Works]
image
Full Dynamic Range
Of course we can combine this for a full dynamic range:
SELECT
    {
        strtomember("[Date].[Calendar Year].[CY "+cstr(Year(DATEADD("YYYY",-2,now())))+"]")
        :
        strtomember("[Date].[Calendar Year].[CY "+cstr(Year(now()))+"]")
        } //RANGE
        on COLUMNS
    ,
    [Measures].[Sales Amount]
    ON ROWS
from[Adventure Works]
image
Have Fun And Till Next Time

No comments:

Post a Comment