I use SSAS and Adventure Works DW 2008.
What is the MDX to get this?:
Measure: Reseller Sales Amount
Day : 2014/03/05
and
Month = 2014/03 ( Sum(Day 01-05) )
and
Year 2014 (Sum(Mount 01 and 02) + Sum(Day 01-05))
With
set Serial_Month as
[Date].[Calendar].Currentmember.parent.FIRSTSIBLING
:
[Date].[Calendar].Currentmember.parent
set Serial_Day as
[Date].[Calendar].Currentmember.FIRSTSIBLING
:
[Date].[Calendar].Currentmember
Select
non empty
{
[Date].[Calendar].[Date],
Serial_Day,
Serial_Month
} on columns ,
non empty {[Measures].[Reseller Sales Amount]} on rows
From [Adventure Works]
The following specifies some specific dates and then creates a calculated member.
I'm not 100% sure what you require but is the following heading in the right direction?
note: I don't have the same date ranges in my Adventure works as you.
WITH
SET [SpecificDate] AS
[Date].[Calendar].[Date].&[20080401]
SET [SpecificMonths] AS
{ [Date].[Calendar].[Month].&[2008]&[3]:
[Date].[Calendar].[Month].&[2008]&[7] }
MEMBER [Date].[Calendar].[AggregatedMonths] AS
(
AGGREGATE([SpecificMonths])
)
SET [SpecificYear] AS
[Date].[Calendar].[Calendar Year].&[2008]
MEMBER [Date].[Calendar].[CalcMember] AS
(
[Date].[Calendar].[Date].&[20080401] +
[Date].[Calendar].[Month].&[2008]&[3] +
[Date].[Calendar].[Calendar Year].&[2008]
)
Select
{
[Measures].[Reseller Sales Amount]
} ON COLUMNS,
{
[SpecificDate],
[SpecificMonths],
[Date].[Calendar].[AggregatedMonths],
[SpecificYear],
[Date].[Calendar].[CalcMember]
} ON ROWS
FROM [Adventure Works]
If you'd like all dates (with data) on rows and then various measures on the columns, such as mtd and ytd then you can do something like the following:
With
MEMBER [Measures].[CurrentDay] AS
AGGREGATE(
[Date].[Calendar].Currentmember,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMonth] AS
AGGREGATE(
[Date].[Calendar].Currentmember.parent,
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentMTD] AS
AGGREGATE(
MTD([Date].[Calendar].CURRENTMEMBER),
[Measures].[Reseller Sales Amount]
)
MEMBER [Measures].[CurrentYTD] AS
AGGREGATE(
YTD([Date].[Calendar].CURRENTMEMBER),
[Measures].[Reseller Sales Amount]
)
Select
non empty
{
[Measures].[CurrentDay],
[Measures].[CurrentMonth],
[Measures].[CurrentMTD],
[Measures].[CurrentYTD]
}
on columns,
non empty
{
[Date].[Calendar].[Date]
}
having [Measures].[CurrentDay]<>null
on rows
From [Adventure Works]
Related
I have this MDX Query:
WITH
MEMBER [COUNT_RANK] AS
RANK(([TKT].[SP].CURRENTMEMBER,
[TKT].[SA].CURRENTMEMBER) ,
[TKT].[SP].CURRENTMEMBER
*[TKT].[SA].[SA]
)
SELECT {
[COUNT_RANK],
[Measures].[TKT Count],
[Measures].[Est Hours]
} ON 0,
ORDER ({[TKT].[SP].[SP]} * {[TKT].[SA].[SA]}, [Measures].[TKT Count], DESC)
ON 1
FROM Ops
the issue I have is that While the COUNT_RANK works and provides a 1-to-n value of ranking per SP for each SA, I need the order of the rank based on TKT Count desc. Meaning for rank = 1, then that SP*SA must have the highest number of TKTs.
Right now the result is random TKT Counts for the RANKING. how do I make the RANK go based on TKT Count DESC?
This is for SQL Server 2016 SSAS. Thanks.
From the MSDN Docs, you should create an ordered set before you declare the RANK() member, and use RANK function on the ordered set.
Here's their example:
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
SELECT {[Measures].[City Rank],[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]
I have the following MDX query:
SELECT
NON EMPTY
Measures.[Enrolments] ON COLUMNS,
NON EMPTY
( STRTOMEMBER(#FromISOYear, CONSTRAINED) :
STRTOMEMBER(#ToISOYear, CONSTRAINED) ) ON ROWS
FROM (
SELECT
Filter
(
[Term Start Date].[ISO Year].Children *
[Term Record Creation].[ISO Year].Children *
[Term Record Creation].[ISO Week Number Of Year].Children
,
Cint([Term Record Creation].[ISO Week Number Of Year].CurrentMember.Member_Key) <= Cint( STRTOMEMBER(#ToISOWeekNumberOfYear, CONSTRAINED) )
OR
Cint([Term Record Creation].[ISO Year].CurrentMember.Member_key) < Cint([Term Start Date].[ISO Year].CurrentMember.Member_key)
) ON COLUMNS
FROM [Enrolments]
);
Basically, the filter expression works when ISO Week Number of Year is not a parameter e.g. if I change the filter expression to say [ISO Week Number of Year] <= 7 it delivers correct results. When I pass the same value from SSRS as a parameter, I get incorrect results - the filter doesn't appear to be applied.
Could someone please explain why this is and provide a fix?
I notice you are missing Member_Key here. Change the following:
Cint( STRTOMEMBER(#ToISOWeekNumberOfYear, CONSTRAINED) )
To:
Cint( STRTOMEMBER(#ToISOWeekNumberOfYear, CONSTRAINED).Member_Key )
Here is my code guys i want to be able to return an total column of 2016,2015,2014,2013 but when i do this
Sum([2016]+[2015]+[2014]+[2013]) as Revenue like this its returns NULLS.
or this
Sum(2016+2015+2014+2013) as Revenue like this returns a different number than if you go take a calculator and add the numbers in 2013 2014 2015 and 2016
The numbers doesn't add up in the total column.
SELECT AirCarrierName,
SUM([2016]) AS [2016],
SUM([2015]) AS [2015],
SUM([2014]) AS [2014],
SUM([2013]) AS [2013]
FROM Sum_Orders
PIVOT
(
SUM(Sum_Orders.Sum_SellPrice)
FOR Sum_Orders.OrderperiodYear IN ([2016],[2015],[2014],[2013])
)AS pvt
WHERE OrderStatus IN ('INVOICED','OPEN') AND AirCarrierName
NOT LIKE 'NULL'
and AirCarrierName = 'CATHAY PACIFIC AIRWAYS LTD.'
GROUP BY AirCarrierName
Order by [2016] desc
this code returns the following
when i add up the lines for creating the total column this is what i get,
instead of having a nice sum of 2013 to 2016 in total column
Sum([2016]+[2015]+[2014]+[2013]) as total
this is the query with the total column included which is returning Nulls
SELECT AirCarrierName,
SUM(Sum_BuyPrice) AS Buy_price,
SUM(Sum_SellerMargin) AS Margin, sum(Sum_GrossWeightkg/1000)as Kilos_total,
SUM([2016]) AS [2016],
SUM([2015]) AS [2015], SUM([2014]) AS [2014], SUM([2013])
AS [2013],
SUM([2016]+[2015]+[2014]+[2013])as [total]
FROM Sum_Orders
PIVOT
(
SUM(Sum_Orders.Sum_SellPrice)
FOR Sum_Orders.OrderperiodYear IN ([2016],[2015],[2014],[2013])
)AS pvt
WHERE OrderStatus IN ('INVOICED','OPEN') AND AirCarrierName NOT LIKE 'NULL'
GROUP BY AirCarrierName
I have an example where we prepared query in sql for fetching appropriate results
SQL Query-
select partnerid,BrandDesc,ActualRetailValue
from
(
select DENSE_RANK() over (partition by partnerid order by sum(ActualRetailValue) desc) as rnk,
partnerid,BrandDesc,sum(ActualRetailValue) as ActualRetailValue
from JDASales
where partnerid in (693,77)
group by partnerid,BrandDesc
) as A
where rnk <=5
order by partnerid,rnk
Output -
I want this result with mdx query.Even tryout with this code
SELECT
NON EMPTY
{[Measures].[Actual Retail Value]} ON COLUMNS
,NON EMPTY
[DimBrands].[Brand].[Brand].ALLMEMBERS
*
TopCount
(
[DimPartners].[Partner].[Partner].ALLMEMBERS
*
[DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
,5
,[Measures].[Actual Retail Value]
) ON ROWS
FROM
(
SELECT
{[DimPartners].[Partner].&[1275]} ON COLUMNS
FROM
(
SELECT
{[Dim Date].[Fiscal Year].&[2014-01-01T00:00:00]} ON COLUMNS
FROM [SALES]
)
)
WHERE
[Dim Date].[Fiscal Year].&[2014-01-01T00:00:00];
You can amend the rows snippet to use the GENERATE function:
SELECT
NON EMPTY
{[Measures].[Actual Retail Value]} ON 0
,NON EMPTY
GENERATE(
[DimBrands].[Brand].[Brand].ALLMEMBERS AS B
,
TopCount(
B.CURRENTMEMBER
*[DimPartners].[Partner].[Partner].ALLMEMBERS
*[DimSKU].[XXX Desc].[XXX Desc].ALLMEMBERS
,5
,[Measures].[Actual Retail Value]
)
) ON ROWS
...
...
This functions usage is detailed here: https://msdn.microsoft.com/en-us/library/ms145526.aspx
I want to create table like this:
[January] [February] ...other months... [Total for Year]
item1
item2
item3
It's easy to create 2 different queries, for months and total, like this:
SELECT
[Time].[Month].[Month] ON COLUMNS,
TOPCOUNT([Items], 5, [Count]) ON ROWS
FROM [Cube]
WHERE([Time].[Year].[Year].&[2015-01-01T00:00:00])
and
WITH
MEMBER [Total] AS SUM([Count], [Time].[Year].[Year].&[2015-01-01T00:00:00])
SELECT
[Total] ON COLUMNS,
TOPCOUNT([Items], 5, [Count]) ON ROWS
FROM [Cube]
but how to concatenate them or write single one?
You could expand the WITH statement like this:
WITH
MEMBER [Time].[Month].[All].[Total] AS --<<THIS IS HOSTED IN SAME HIERARCHY AS THE SET THAT FOLLOWS
Sum
(
[Count]
,[Time].[Year].[Year].&[2015-01-01T00:00:00]
)
SET [mths] AS
Exists
(
[Time].[Month].[Month].MEMBERS
,[Time].[Year].[Year].&[2015-01-01T00:00:00]
)
SET [concatenatet_set] AS
{
--<<THE FOLLOWING CAN BE BROUGHT TOGETHER AS THEY HAVE THE SAME "DIMENSIONALITY" I.E. FROM THE SAME HIERARCHY
[mths]
,[Time].[Month].[All].[Total]
}
SELECT
[concatenatet_set] ON COLUMNS
,TopCount
(
[Items]
,5
,[Count]
) ON ROWS
FROM [Cube];
Here is the script I have used to test the above idea against AdvWrks:
WITH
MEMBER [Date].[Calendar].[All].[Total] AS
Sum
(
[Measures].[Internet Sales Amount]
,(
[Date].[Calendar].[All Periods]
,[Date].[Calendar Year].&[2007]
,[Date].[Calendar Quarter of Year].&[CY Q1]
)
)
SET [mths] AS
Exists
(
[Date].[Calendar].[Month]
,(
[Date].[Calendar Year].&[2007]
,[Date].[Calendar Quarter of Year].&[CY Q1]
)
)
SET [concatenatet_set] AS
{
[mths]
,[Date].[Calendar].[All].[Total]
}
SELECT
[concatenatet_set] ON COLUMNS
,TopCount
(
NonEmpty
(
[Product].[Subcategory].[Subcategory]
,(
[Date].[Calendar Year].&[2007]
,[Date].[Calendar Quarter of Year].&[CY Q1]
)
)
,5
,[Measures].[Internet Sales Amount]
) ON ROWS
FROM [Adventure Works]
WHERE
[Measures].[Internet Sales Amount];
It results in the following which seems reasonable:
Try just changing your first query to:
SELECT
[Time].[Month].Members ON COLUMNS,
TOPCOUNT([Items], 5, [Count]) ON ROWS
FROM [Cube]
WHERE([Time].[Year].[Year].&[2015-01-01T00:00:00])