MDX to retrieve year and months with lag on month - mdx

I have a hierarchy Time of [year].[month]
I want to gets rows years and months, going back six periods, but I also want to show the year.
So this I can do, using lag to find a set of months, and then Ascendants.
SELECT
NON EMPTY {[Measures].[modality]} ON COLUMNS,
NON EMPTY {Hierarchize(Generate({[Time.CalYearMonth1].[2016].[1].lag(6):[Time.CalYearMonth1].[2016].[1]},ASCENDANTS([Time.CalYearMonth1].CurrentMember)))}
ON ROWS
FROM [mt_report]
When I do this, I get a row for the year member, which is basically a row I don't want.
In Pentaho, this gives the result:
Axis #0:
{}
Axis #1:
{[Measures].[modality]}
Axis #2:
{[Time.CalYearMonth1].[All Time.CalYearMonth1s]}
{[Time.CalYearMonth1].[2015]}
{[Time.CalYearMonth1].[2015].[7]}
{[Time.CalYearMonth1].[2015].[8]}
{[Time.CalYearMonth1].[2015].[9]}
{[Time.CalYearMonth1].[2015].[10]}
{[Time.CalYearMonth1].[2015].[11]}
{[Time.CalYearMonth1].[2015].[12]}
{[Time.CalYearMonth1].[2016]}
{[Time.CalYearMonth1].[2016].[1]}
Row #0: 0.95
Row #1: 1.11
Row #2: 1.08
Row #3: 1.14
Row #4: 1.13
Row #5: 1.16
Row #6: 1.15
Row #7: 1.15
Row #8: 1.11
Row #9: 1.17
I don't know how to avoid the summary per year.

ASCENDANTS definitions seems to suggest it will return members from all levels above - this will include the All level which it sounds like you'd like to avoid.
You could try Ancestors to get just the Years:
SELECT
NON EMPTY
{[Measures].[modality]} ON COLUMNS
,NON EMPTY
{
Hierarchize
(
{
[Time.CalYearMonth1].[2016].[1].Lag(6) : [Time.CalYearMonth1].[2016].[1]
,Generate
(
{
[Time.CalYearMonth1].[2016].[1].Lag(6) : [Time.CalYearMonth1].[2016].[1]
}
,Ancestors
(
[Time.CalYearMonth1].CurrentMember
,1 //<<<change this to the distnace year is away from month in the levels of the user hierarchy
)
)
}
)
} ON ROWS
FROM [mt_report];
Seems to test ok in AdvWrks:
SELECT
{} ON 0
,Hierarchize
(
{
[Date].[Calendar].[Month].&[2010]&[4].Lag(6)
:
[Date].[Calendar].[Month].&[2010]&[4]
,Generate
(
{
[Date].[Calendar].[Month].&[2010]&[4].Lag(6)
:
[Date].[Calendar].[Month].&[2010]&[4]
} AS S
,Ancestors
(
s.CurrentMember
,3 //<<< distance in advwrks between month and year levels
)
)
}
) ON 1
FROM [Adventure Works];
Returns:

Related

Number or Results and WITH MEMBER Calculation interacting weird

In our MDX Queries, we use a template like the following WITH MEMBER . SELECT SUBSET . FROM (SELECT . FROM .) a lot.
What this particular query should do (and does!) is list all product variants matching a filter, and give the sales amount for it for a month in time. On the same row, we have the sales total of all variants of this product.
We need only a subset of the result (for paging), and the results need to ordered to allow paging of course. Then there is a non empty, so only product variants that were sold in the time period qualify.
This works just fine. Paging also works: putting 30, 60, 90 for Start in SUBSET is gives the correct results.
However, if I put a 1 as Count (SUBSET(..., x, 1)) the result of the "Price all" goes to (null) for each and any row I select with x. If I return two or more rows it works just fine, and all rows have their correct values.
Two results and the correct value in the rightmost column (in case you are wondering how the total of all variants, 67, can be lower than the sales of one particular variant (280): I picked a bad example - there was a return (so a negative sale) for another variant of this product, for 213. The 67 is correct and desired!)
One Row and a (null) result
I do not understand how or why the SUBSET count would interfere with the Tuple I select in the WITH MEMBER. If it does it for 30 rows at a time, why not for one?
*edit
I just tried, what would happen if I filter in a way that only one result matches (and SUBSET paging is set to "working values" of 0, 30): The same as in the 2nd screenshot, for one result row the WITH MEMBER evaluates to (null).
So it is not that SUBSET is the problem, but more general: When there is only one result row!
Could this be a bug in our SQL Server Version? It is SQL 2012 with SP4 and all latest security patches.
WITH
MEMBER [DIM Date].[HI Year_Month_Day].[CURRENT_MONTH] AS
[DIM Date].[HI Year_Month_Day].&[20201215].Parent /* Select Month of December */
MEMBER [MEASURES].[Price all] AS (
(
[DIM Products].[HI Products].Currentmember.Parent
, [DIM Products].[HI Subproduct].[Total]
)
, [MEASURES].[Price]
), FORMAT_STRING = '#,0' /* Get the parent product total next to every subproduct */
SELECT {
([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, ([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price All])
} ON COLUMNS
, {
SUBSET(
ORDER(
NONEMPTY(
([DIM Products].[HI Produt Subproduct].[Subproduct])
, {([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])}
)
, ([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, BDESC
)
, 0
, 30
)
} ON ROWS
FROM (
SELECT
{[DIM StoreLocation].[HI Country].[Country].&[US]}
* {[DIM Productgroup].[HI Productgroup].[Productgroup].&[Bikes]}
ON COLUMNS
FROM [Cube]
)

MDX - transpose rows to columns

I'm trying to display details from two rows into one using MDX. If I execute the MDX below, it returns 2 rows, one containing the 998 Key and one containing the 999 Key
SELECT NON EMPTY {
[Measures].[FactTableCount] } ON COLUMNS,
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS
* ([DimAAAA].[AAAAKey].[AAAAKey],{[DimBBBB].[Key].&[998],[DimBBBB].[Key].&[999]},[DimCCCC].[CCCCKey].[CCCCKey])
) } ON ROWS
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS
FROM [FactTable])
It returns something like this
(columns [DimXXXX].[XXXXKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])
MyValue, MyAAAAKey1, 998, MyCCCCKey1, 1
MyValue, MyAAAAKey2, 999, MyCCCCKey2, 1
However I want to return one row like this
`(columns [DimXXXX].[Key], [DimAAAA].[AAAAKey], [DimAAAA].[AAAAKey], [DimBBBB].[Key], [DimBBBB].[Key], [DimCCCC].[CCCCKey], [DimCCCC].[CCCCKey], [Measures].[FactTableCount])
MyValue, MyAAAAKey1, MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 1
Among other things (such as using SETs, putting the 998/999 logic after the ROWS/COLUMNS, etc) I've tried
SELECT NON EMPTY {
[Measures].[FactTableCount] } ON COLUMNS,
NON EMPTY { ([DimXXXX].[XXXXKey].[XXXXKey].ALLMEMBERS
* ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[998],[DimCCCC].[CCCCKey].[CCCCKey])
* ([DimAAAA].[AAAAKey].[Key],[DimBBBB].[Key].&[999],[DimCCCC].[CCCCKey].[CCCCKey])
) } ON ROWS
FROM ( SELECT ( { [DimXXXX].[XXXXKey].&[MyValue] } ) ON COLUMNS
FROM [FactTable])
...however because the AAAAKey hierarchy is repeated I receive the error message "The AAAAKey hierarchy is used more than once in the Crossjoin function"
Is there a way to do do this ?
Based on your comment below I have a sample.Let me know if it works.
I think I can see what you're saying however the measures are one
thing, but the dimension values are another - say Record1:MyValue,
MyAAAAKey1, 998, MyCCCCKey1, 2 and Record2: MyValue, MyAAAAKey2, 999,
MyCCCCKey2, 5 - I would like to output MyValue, MyAAAAKey1,
MyAAAAKey2, 998, 999, MyCCCCKey1, MyCCCCKey2, 2, 5
So in the query below I am trying to simulate your problem.
select
{[Measures].[Internet Sales Amount]}
on columns,
non empty
([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]},[Product].[Subcategory].[Subcategory])
on rows
from [Adventure Works]
Result
Now onw way is to take the changing values to columns, "{[Product].[Category].&1,[Product].[Category].&2}" in my case and "{[DimBBBB].[Key].&[998],[DimBBBB].[Key].&[999]}" in your case
select
{
({[Product].[Category].&[1],[Product].[Category].&[3]},[Measures].[Internet Sales Amount]),
([Product].[Category].defaultmember,[Measures].[Internet Order Quantity])
}
on columns,
non empty
([Customer].[City].[City],[Product].[Subcategory].[Subcategory])
on rows
from [Adventure Works]
Result:
Note how the values are only repeated for the relevent column. This does add an extra column but you rows are now half the orignal count.
Edit: to handle the requirement based on the comment
1st row of the grid would be Ballard, Bikes, Mountain Bikes, Road
Bikes. The 2nd: Ballard, Clothing, Caps, Gloves. The 3rd: Barstow,
Bikes, Road Bikes, null. I want to merge/list the actual dimension
values
So to achieve above we have two options. But in either case some manipulation on UI would be required.
1)First option
with member
measures.t
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name
member measures.t1
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name
select
{measures.t,measures.t1}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows
from [Adventure Works]
2)The second option,
with member
measures.t1
as
[Customer].[City].currentmember.name
member measures.t2
as
[Product].[Category].currentmember.name
member measures.t3
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(0).item(2).name
member measures.t4
as (nonempty(existing([Customer].[City].currentmember,[Product].[Category].currentmember,[Product].[Subcategory].[Subcategory].members),[Measures].[Internet Sales Amount])).item(1).item(2).name
select
{measures.t1,measures.t2,measures.t3,measures.t4}
on columns,
nonempty(([Customer].[City].[City],{[Product].[Category].&[1],[Product].[Category].&[3]}),[Measures].[Internet Sales Amount])
on rows
from [Adventure Works]

MDX: Linkmember > Descendants offsets with currentmember context

I'm self taught in MDX and am struggling with a problem.
I need to be able to isolate a set of products according to a window around their release date and then calculate their sales by the current rows date context. The 'window' is a set of streetdates where member is between or equal to currentmember -27 and currentmember + 7 (as we sometimes see early sales/preshipping).
Final output should look something like (forward days blank):
NR Sales
2018-03-25 117.12
2018-03-26 130.01
2018-03-27 2,827.63
2018-03-28
2018-03-29
2018-03-30
2018-03-31
My cube has a street/release date hierarchy which is a mirror of my fiscal date hierarchy. It pulls correct values with a slightly altered query looking at a single day in the where clause.
The below query returns values, but is slightly incorrect as i think the descendants call isn't working properly.
WITH
SET [StreetDateSet] AS
LinkMember
(
Descendants
(
[Date].[Yr-Qtr-Mo-Wk-Day].CurrentMember
,[Date].[Yr-Qtr-Mo-Wk-Day].[Day]
).Item(0)
,[Street Date].[StreetDate-Yr-Mo-Wk-Dy]
).Lag(27)
:
LinkMember
(
Descendants
(
[Date].[Yr-Qtr-Mo-Wk-Day].CurrentMember
,[Date].[Yr-Qtr-Mo-Wk-Day].[Day]
).Item(0)
,[Street Date].[StreetDate-Yr-Mo-Wk-Dy]
).Lag(-7)
MEMBER [NR Sales] AS
Sum
(
[StreetDateSet]
,[Measures].[Sales Value]
)
SELECT
NON EMPTY
[NR Sales] ON 0
,[Date].[Date].Children ON 1
FROM [CUBE]
WHERE
(
[Date].[Yr-Qtr-Mo-Wk-Day].[Week].&[2018]&[2]&[4]
,[Item].[New Used].[New]
,{
[Item].[Category Code].[220]
,[Item].[Category Code].[230]
,[Item].[Category Code].[210]
}
);
I suspect there's a few problems here but am not sure how to address them. Descendants is likely impacted by the date hierarchy in the where clause. I've tried to get it into the 1 axis, but i get 'The Yr-Qtr-Mo-Wk-Day hierarchy already appears in the Axis1 axis.' and am unsure how to address.
Thanks for any advice/guidance on where i'm going wrong.
Turns out that linkmember wasn't required and ended up finding a solution via MSDN that uses a filter/Properties function:
Set iteration in calculation
Solution involves a member which iterates a NULL * CurrentMember defined set and I slightly adopted the statement to generate a dynamic set which matches off the complementary hierarchies.
WITH
MEMBER [Measures].[SameDay] AS
NULL * Count([Date].[Yr-Mo-Wk-Day].CurrentMember AS MySet)
+
Sum
(
{
Filter
(
[Street Date].[StreetDate-Yr-Mo-Wk-Dy].MEMBERS AS MySet2
,
MySet2.Current.Properties("Key") = MySet.Item(0).Properties("Key")
).Item(0).Lag(28)
:
Filter
(
[Street Date].[StreetDate-Yr-Mo-Wk-Dy].MEMBERS AS MySet2
,
MySet2.Current.Properties("Key") = MySet.Item(0).Properties("Key")
).Item(0).Lead(7)
}
,[Measures].[Sales Value]
)

Mdx request get the first element of a tuple with datetime

My MDX request gives to me this result:
For my entities, I've multiple years for one value, I need just get the first year for the entity (and others dimensions). I've tried to use the function .FirstChild in the year dimension this return only value with the year '2014' (first year in my dimension). The function .Item() returns only empty values:
SELECT
NON EMPTY
{[Measures].[Value]} ON COLUMNS
,NON EMPTY
{
[EntiteFederal].[EntiteCode].[EntiteCode].ALLMEMBERS*
[T].[Year].[Year].ALLMEMBERS*
[T].[YearDate].[YearDate].ALLMEMBERS
}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM [Mycube];
Hopefully this is working - tricky for me to test:
WITH
SET [EntYr] AS
Generate
(
[EntiteFederal].[EntiteCode].[EntiteCode].MEMBERS AS X
,
X.CurrentMember
*
Head
(
NonEmpty
(
[T].[Year].[Year].ALLMEMBERS * [T].[YearDate].[YearDate].ALLMEMBERS
,X.CurrentMember
)
)
)
SELECT
NON EMPTY
[Measures].[Value] ON COLUMNS
,NON EMPTY
[EntYr] ON ROWS
FROM [Mycube];

Get the last three months dynamically

I have the following MDX query
SELECT
Hierarchize
(
{
[PERIOD].[Year Month].&[2014 / 10]
,[PERIOD].[Year Month].&[2014 / 11]
,[PERIOD].[Year Month].&[2014 / 12]
}
) ON COLUMNS
,{
[Measures].[Amount]
,[Measures].[Total Cost]
} ON ROWS
FROM [Asset];
Is there a way to get the last three months dynamically ?
Exactly as George says except in your context I don't think you need EXISTING:
SELECT
Hierarchize
(
Tail([PERIOD].[Year Month].members,3)
) ON COLUMNS
,{
[Measures].[Amount]
,[Measures].[Total Cost]
} ON ROWS
FROM [Asset];
Last N values from any axis could be retrieved by using Tail function:
Tail(EXISTING [PERIOD].[Year Month],3)
Retrieves a set of last 3 items of Month level for Period dimension