Calculate cumulative measure in MDX - mdx

I need to define a CumulativeSales measure in a mondrian catalog definition. So far, I have this:
<CalculatedMember name="CumulativeSales" dimension="Measures" >
<Formula>IIF([Month.Year].CurrentMember.Level.Name = 'Month', Sum([Month.Year].CurrentMember.FirstSibling:[Month.Year].CurrentMember,[Measures].[Sales]), Sum([Month.Year].CurrentMember.Children, [Measures].[Sales]))</Formula>
<CalculatedMemberProperty name="FORMAT_STRING" value="$ #,##0.00;$ #,##0.00;#"/>
</CalculatedMember>
MDX snippet as follows:
IIF
(
[Month.Year].CurrentMember.Level.Name = 'Month'
,Sum
(
[Month.Year].CurrentMember.FirstSibling : [Month.Year].CurrentMember
,[Measures].[Sales]
)
,Sum
(
[Month.Year].CurrentMember.Children
,[Measures].[Sales]
)
)
But sometimes it doesn't work well, particularly when showing cumulatives sales in months with no sales. For example, in this query:
SELECT
{[Measures].[CumulativeSales]} ON COLUMNS
,{[Department].[All]} ON ROWS
FROM [Production]
WHERE
CrossJoin
(
{
[Activity].[Services].[DP]
,[Activity].[Services].[Consulting]
}
,{[Month.Year].[2015].[03]}
);
the result is this:
Measures
Department CumulativeSales
All
The CumulativeSales shows no value. There are still no sales in March 2015, but it should show January+February sales.
Thanks in advance for the help

Related

MDX Query to get Last Year data without filtering based on current month

i have been doing a query to get previous year value in MDX but can't the the desired output. In sql i would do it like this. Question is how do i apply this query in MDX?
select data1, sum(data2) as sumthisyear, sumlastyear
from table1 a
left outer join
( select data1, sum(data2) 'sumlastyear'
from table1
where tableyear = 2017 group by data1
) b on a.data1 = b.data1
where tableyear= '2018' and datafilter = 'SampleFilter'
group by data1, sumlastyear
SSAS Cube Structure Image
I have done some research and did mdx function like parallelperiod but not giving the correct values as data are filtered based on 2018 year.
Here's my MDX query
CREATE MEMBER CURRENTCUBE.Measures.[SPLY Registered Sales] AS
iif( [DimProdDate].[Dates].CurrentMember Is
[DimProdDate].[Dates].[All].LastChild,
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
Tail( NonEmpty( [DimProdDate].[Dates].[Full Date].MEMBERS,
[MEASURES].[Registered Sales]),
1
).Item(0)
),
[MEASURES].[Registered Sales] ),
( PARALLELPERIOD( [DimProdDate].[Dates].[Calendar Year],
1,
[DimProdDate].[Dates].CurrentMember
),
[MEASURES].[Registered Sales] )
);
Any help will be very much appreciated.
Thanks!

MDX queries in SSMS vs calculated membersin cubes performance

I have some MDX queries with calculated members than when I run them in SSMS run very fast.
But when I implement these calculated members in the cube and run them, it's very slowly.
Any ideas of this behavior?
This is the query that runs smoothly in SSMS, but it takes forever when I query the cube from Excel or Tableau (against the same calculated members in the Cube)
with member [Measures].[Sales BUM Avg 3Months]
as
sum(
lastperiods(3, ancestor([Date].[Calendar].currentMember,[Date]. [Calendar].[Calendar Year Month])), [Measures].[Sales Month BUM]
)
/
count(
nonempty(lastperiods(3, ancestor([Date].[Calendar].currentMember,[Date].[Calendar].[Calendar Year Month])), [Measures].[Sales Month BUM])
)
member [Measures].[CoverageSalesBUM]
as
sum([Measures].[Unrestricted BUM])
/
sum([Measures].[Sales BUM Avg 3Months] )
select non empty { [Measures].[Unrestricted BUM] , [Measures].[Sales BUM Avg 3Months] } on 0
,non empty([Date].[Calendar].[Calendar Date] , [Plant].[Plant].[Plant],[Material].[Material].[Material].[149249] ) on 1
from [InventCoverage]
(More of a code review than an answer, as I don't know much about cube scripts so cannot comment on what you might need to do to the cube)
Is you first calculation not a re-invention of the function Avg?
Also in you second calc why do you need to apply Sum()? If this is the aggregation set in the cube then no need to explicitly say Sum. Also apparently Divide is quite a recent introduction and maybe marginally more efficient
WITH
MEMBER [Measures].[Sales BUM Avg 3Months] AS
Avg
(
LastPeriods
(3
,Ancestor
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Calendar Year Month]
)
)
,[Measures].[Sales Month BUM]
)
MEMBER [Measures].[CoverageSalesBUM] AS
Divide
(
[Measures].[Unrestricted BUM]
,[Measures].[Sales BUM Avg 3Months]
)
SELECT
NON EMPTY
{
[Measures].[Unrestricted BUM]
,[Measures].[Sales BUM Avg 3Months]
} ON 0
,NON EMPTY
(
[Date].[Calendar].[Calendar Date]
,[Plant].[Plant].[Plant]
,[Material].[Material].[Material].[149249]
) ON 1
FROM [InventCoverage];

How to calculate average based on distinct counts in MDX

My SSAS cube has the following fact and dimensions with the columns as shown below
FactActivity
DateKey, UserKey, ActivityKey, ActivityCount
DimDate
DateKey, Date, Week, Year
DimUser
UserKey, UserName, Gender
DimActivity
ActivityKey, ActivityName
I have created the distinct count measures of users and dates as follows
[Distinct Users]
COUNT(NONEMPTY([DimUser].[UserKey].[UserKey].Members, [Measures].[ActivityCount])
[Distinct Dates]
COUNT(NONEMPTY([DimDate].[DateKey].[DateKey].Members, [Measures].[ActivityCount])
Both these measures are working correctly as expected when I slice/pivot by ActivityName.
Now I wanted to calculate average days per user, so I created the metric as follows
[Avg Days Per User]
AVG([DimUser].[UserKey].[UserKey].Members, [Measures].[Distinct Dates])
But this is giving me wrong results.! i also tried
DIVIDE([Measures].[Distinct Days], [Measures].[Distinct Users])
Still I get wrong results...what i'm doing wrong?
Maybe just adding in EXISTING will help?
AVG(
EXISTING [DimUser].[UserKey].[UserKey].Members,
[Measures].[Distinct Dates]
)
Although trying to recreate something similar in AdvWks I seem top get a valid return without EXISTING:
WITH
MEMBER [Measures].[Avg Count Per Reseller] AS
Avg
( [Reseller].[Reseller].[Reseller].MEMBERS
,[Measures].[Reseller Order Count]
), format_string = "0.0000"
SELECT
{[Measures].[Reseller Order Count],[Measures].[Avg Count Per Reseller]} ON 0
,{[Promotion].[Promotion].[All Promotions].Children} ON 1
FROM [Adventure Works]
WHERE [Date].[Calendar Year].&[2005];

SQL:Pivot table which includes sum and percentage total

I'm trying to recreate a view in Tableau as a view in SQL. It requires me pivoting a table based on month and not only summing the amount but I also need to sum by margin and also create a Margin % row.The desired output is
BUSINESS_UNIT CLASS JANUARY FEBRUARY MARCH
202 Cost of Sales 100 (null) 60
202 Revenue 200 80 (null)
202 Margin x xx xxx
202 Margin % x% xx% xxx%
I can pivot based on Month but how do perform twos sums in one pivot table and how would I go about including a percenatge row also?
Code so far
SELECT
*
FROM
(SELECT
[Business_Unit]
,[Class]
,Month as Period
,[Amount]
--,Margin
FROM [sample_table]
where [Class] in ('Revenue','Cost of Sales') )AS T
PIVOT(SUM(Amount)
FOR Period IN ([January],[February],[March])) as Pvt
I have included my code so far http://www.sqlfiddle.com/#!3/06bafc/6
Not the prettiest SQL I've done. but this seems to work...
http://www.sqlfiddle.com/#!3/06bafc/60/0
What it does is build on what you've done by generating a margin line and adding a total column
Using this line and total we can then calculate the % of margin. Grouping SETS allowed me to generate the multiple rows, subtotals and totals, Since I knew the only additional line generated would have a null class, I was able to set the Name of the class to margin when null.
WITH CTE AS (
SELECT
Business_Unit
,case when class is NULL then 'Margin' else class end as Class
,Sum(January) as January
,Sum(February) as February
,Sum(March) as march
,Sum(coalesce(January,0)+coalesce(February,0)+coalesce(March,0)) as Total
FROM (
SELECT
*
FROM
(SELECT
[Business_Unit]
,[Class]
,Month as Period
,[Amount]
--,Margin
FROM [sample_table]
where [Class] in ('Revenue','Cost of Sales') )AS T
PIVOT(SUM(Amount)
FOR Period IN ([January],[February],[March])) as Pvt
) as Base
GROUP BY Grouping sets
((Business_Unit,Class,January,February,March,
coalesce(January,0)+coalesce(February,0)+coalesce(March,0))
,(Business_Unit)
))
SELECT *
FROM CTE UNION
SELECT Business_Unit
,'Margin %'
,January*100.00/Total
,February*100.00/Total
,March*100.00/Total
,Total*100.00/Total
FROM CTE
WHERE CLASS='Margin'

MDX Count over time (years - not within a year)

I'd like to be able to rollup the count of commitments to a product over years -
The data for new commitments in each year looks like this:
Year | Count of new commitments | (What I'd like - count of new commitments to date)
1986 4 4
1987 22 26
1988 14 40
1989 1 41
I know that within a year you can do year to date, month to date etc, but I need to do it over multiple years.
the mdx that gives me the first 2 columns is (really simple - but I don't know where to go from here):
select [Measures].[Commitment Count] on 0
, [Date Dim].[CY Hierarchy].[Calendar Year] on 1
from [Cube]
Any help would be great
In MDX something along the line:
with member [x] as sum(
[Date Dim].[CY Hierarchy].[Calendar Year].members(0) : [Date Dim].[CY Hierarchy].currentMember,
[Measures].[Commitment Count]
)
select [x] on 0, [Date Dim].[CY Hierarchy].[Calendar Year] on 1 from [Cube]
Use a common table expression:
with sums (year,sumThisYear,cumulativeSum)
as (
select year
, sum(commitments) as sumThisYear
, sum(commitments) as cumulativeSum
from theTable
where year = (select min(year) from theTable)
group by year
union all
select child.year
, sum(child.commitments) as sumThisYear
, sum(child.commitments) + parent.cumulativeSum as cumulativeSum
from sums par
JOIN thetable Child on par.year = child.year - 1
group by child.year,parent.cumulativeSum
)
select * from sums
There's a bit of a "trick" in there grouping on parent.cumulativeSum. We know that this will be the same value for all rows, and we need to add it to sum(child.commitments), so we group on it so SQL Server will let us refer to it. That can probably be cleaned up to remove what might be called a "smell", but it will work.
Warning: 11:15pm where I am, written off the top of my head, may need a tweak or two.
EDIT: forgot the group by in the anchor clause, added that in