How to calculate average based on distinct counts in MDX - ssas

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];

Related

MDX Calculation for summing the product of two measures and multiplying that by the last children of two other metrics

Here is a snip of my cube's dimension usage:
In "TD Measures" I have
[A] "Billable Client Hours Current".
In "Personnel Measures" I have
[B] "FTE Count" (an employee's amount they are working like .5 for
people who work 20 hours per week)
[C] "Forecast FY End" Forecast for the end of the fiscal year
[D] "Forecast FYTD" Forecast for the current period to date in the fiscal year.
The calculation I need to do at a leaf level is:
[A][B][C]/[D]
The issue is that [A] has more dimensionality that [B], [C], and [D].
So If this is my data:
The calc I need to do is:
((15*.05)+(5*1)) //sum of hours in a period multiplied by that month's FTE
X 2000/300 //last child of FCFYE divided by last child of FCFYTD in the currently selected set.
This calc is likely to change a little over the next week or two, but this is the main concept of what they are looking to do. Any help in writing the MDX for this to create a calculated member in my cube is greatly appreciated. Sorry if I've left out any key info--I'm quite the MDX noob.
Through digging through Chris Webb's and Mosha's posts as well as working with some guys on the MSDN forums I have worked out a solution to this below. Ultimately, I worked out with the business that we didn't need to interrogate the underlying rows to discern if they were active or not since the addition of the root functionality eliminated the need as long as they filtered on it from DimMiscellaneous. But for the sake of helping someone with a similar problem here is the full MDX below.
//Only evaluate if they are active
CREATE HIDDEN UtilizedFTESummator;
[Measures].[UtilizedFTESummator] = Iif([Measures].[Is Active For Utilization Value] > 0,[Measures].[Period FTE],NULL);
NON_EMPTY_BEHAVIOR([Measures].[UtilizedFTESummator]) = [Measures].[Is Active For Utilization Value];
//only include this measure if the underlying employee has values in their underlying data for active in utilization
CREATE MEMBER CURRENTCUBE.[Measures].[FTE Active Utilization]
AS
SUM
(
EXISTING [Historical Personnel].[Employee Id].[Employee Id],
[Measures].[UtilizedFTESummator]
),VISIBLE=0;
//Show weighted FTE by workdays
CREATE MEMBER CURRENTCUBE.[Measures].[FTE MTD Active Utilization]
AS SUM
(
DESCENDANTS([Historical Personnel].[Employee Id].CURRENTMEMBER,[Historical Personnel].[Employee Id].[Employee Id]),
(
DIVIDE
(
SUM
(
DESCENDANTS([Period].[Fiscal Period].CURRENTMEMBER,[Period].[Fiscal Period].[Fiscal Period]),
[Measures].[FTE Active Utilization]*[Measures].[Work Days In Month]
)
,SUM(ROOT([Historical Personnel].[employee id].currentmember),[Measures].[Work Days In Month])
,0
)
)
);
//Use Weighted FTE for calculating the weighted value for each person (all periods aggregated)
//Forecast Billable End Of Year has default aggregation of last child
CREATE MEMBER CURRENTCUBE.[Measures].[Annualized CBH Projected]
AS DIVIDE
(
SUM
(
DESCENDANTS([Historical Personnel].[Employee Id].CURRENTMEMBER,[Historical Personnel].[Employee Id].[Employee Id]),
[Measures].[Billable Client Hours Current] *
(
DIVIDE
(
[Measures].[Forecast Billable End Of Year]
,
[Measures].[Forecast Billable FTE]
,0
)
)
*[Measures].[FTE MTD Active Utilization]
)
,[Measures].[FTE MTD Active Utilization]
,0
);
Simplified answer with the users filtering on the Is Active For Utilization flag is as follows:
//Weight FTE by employee calculating FTE * workdays for each period and then dividing by the sum of days irrespective of filters on historical personnel or miscellaneous
CREATE MEMBER CURRENTCUBE.[Measures].[FTE MTD]
AS SUM
(
DESCENDANTS([Historical Personnel].[Employee Id].CURRENTMEMBER,[Historical Personnel].[Employee Id].[Employee Id]),
(
DIVIDE
(
SUM
(
DESCENDANTS([Period].[Fiscal Period].CURRENTMEMBER,[Period].[Fiscal Period].[Fiscal Period]),[Measures].[Period FTE]*[Measures].[Work Days In Month]
)
,
SUM((ROOT([Historical Personnel].[employee id].currentmember),ROOT([Miscellaneous]),[Measures].[Work Days In Month]))
,0
)
)
);
//Weight by FTE with default agg for Forecast EOY being last child.
CREATE MEMBER CURRENTCUBE.[Measures].[Annualized CBH Projected]
AS DIVIDE
(
SUM
(
DESCENDANTS([Historical Personnel].[Employee Id].CURRENTMEMBER,[Historical Personnel].[Employee Id].[Employee Id]),
[Measures].[Billable Client Hours Current] *
(
DIVIDE
(
[Measures].[Forecast Billable End Of Year]
,
[Measures].[Forecast Billable FTE]
,0
)
)
*[Measures].[FTE MTD]
)
,[Measures].[FTE MTD]
,0
);

Count Working Days MDX

I would like to count the working days of a spesific time range and then find the Average Daily Dispatches.Currently the time range is at WHERE statement.
I believe that I have include the date range in the 1st Member but I can't figure how to count the dates in a month range.
Any suggestments?
WITH MEMBER [Measures].[Working Days] AS
COUNT(Date.[Working Date].&[1])--Doesn't work
MEMBER [Measures].[Average Daily Dispatches] AS
[Measures].[Total Dispatches]/[Measures].[Working Days]
SELECT [Measures].[Average Daily Dispatches] ON 0
FROM [cube]
WHERE (
[Date].[Month].&[2015-01-01T00:00:00]:[Date].[Month].&[2015-08-01T00:00:00]
);
Use the NonEmpty function to yield those working dates on which there was a dispatch. Then use COUNT on top of this.
WITH MEMBER [Measures].[Working Days] AS
COUNT
(
NonEmpty
(
[Date].[Working Date].MEMBERS,
[Measures].[Total Dispatches]
)
)
Some sort of alternative seems to exist using Exists/Existing (I think 1 is Monday in the AdvWrks cube!)
WITH
MEMBER [Measures].[MondayCnt] AS
Count
(
Exists
(
(EXISTING
[Date].[Calendar].MEMBERS)
,[Date].[Day of Week].[1]
)
)
MEMBER [Measures].[Averge] AS
[Measures].[Internet Sales Amount] / [Measures].[MondayCnt]
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[MondayCnt]
,[Measures].[Averge]
} ON 0
,
[Date].[Calendar].[Month].&[2006]&[6]
:
[Date].[Calendar].[Month].&[2007]&[6] ON 1
FROM [Adventure Works];
The above results in the following:

How to count YTD days in my MDX query

I've below MDX query which gives me YTD amount for each my device perfectly, but I want count of those YTD days taken in consideration while calculating YTD amount.
How can I achieve those YTD days counts in my query?
WITH
MEMBER [Settlement Date].[Calendar].[CalendarYTD] AS
Aggregate
(
YTD
(
[Settlement Date].[Calendar].[Settlement Calendar Month].&[201412]
)
)
SELECT
[Settlement Date].[Calendar].[CalendarYTD] ON COLUMNS
,NON EMPTY
[Device].[DeviceID].Children ON ROWS
FROM [cube1]
WHERE
[Measures].[amount];
OR
you can use below AW MDX query while making changes which give same results as my above query:
WITH
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([Date].[Calendar].[Month].[March 2015]))
SELECT
[Date].[Calendar].[CalendarYTD] ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Measures].[Order Quantity];
this is a little swapped around but hopefully heading in the direction you require:
WITH
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([Date].[Calendar].[Month].[March 2007]))
MEMBER [Measures].[DaysCompleteCurrYear] AS
Descendants
(
YTD([Date].[Calendar].[Month].[March 2007])
,[Date].[Calendar].[Date]
,SELF
).Count
SELECT
{
[Measures].[DaysCompleteCurrYear]
,[Measures].[Order Quantity]
} ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[CalendarYTD];
If we put the target month into a single member set then the family relationships are preserved and we can use that set in the custom measures:
WITH
SET [targMth] AS
{[Date].[Calendar].[Month].[March 2007]}
MEMBER [Date].[Calendar].[CalendarYTD] AS
Aggregate(YTD([targMth].Item(0).Item(0)))
MEMBER [Measures].[DaysCompleteCurrYear] AS
Descendants
(
YTD([targMth].Item(0).Item(0))
,[Date].[Calendar].[Date]
,SELF
).Count
SELECT
{
[Measures].[DaysCompleteCurrYear]
,[Measures].[Order Quantity]
} ON COLUMNS
,[Product].[Category].Children ON ROWS
FROM [Adventure Works]
WHERE
[Date].[Calendar].[CalendarYTD];

Calculate cumulative measure in 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

MDX PeriodsToDate Function

I am unable to get periodsToDate to work in MDX. I am getting the sum value when i am setting the periods to year level, but i am unable to get the sum values for quarters, months and days level.
This is my query for the year level (which is working fine).
With MEMBER [Measures].[Cumul Claim Cost] AS
'Sum
(
PERIODSTODATE
(
[Valuation Date].[Year].[All].Level,
[Valuation Date].[Year].CurrentMember
)
,[Measures].[Cost]
)'
select
{[Measures].[Cumul Claim Cost], [Measures].[Cost]} on columns,
{[Valuation Date].[Date].Members} on rows
from [WVWC DATA CUBE FROI SROI]
And this is my query for days level which is not working.
With MEMBER [Measures].[Cumul Claim Cost] AS
'Sum
(
PERIODSTODATE
(
[Valuation Date].[Year].[All].Level,
[Valuation Date].[Year].[Month-Year].[Quarter- Year].[Date].CurrentMember
)
,[Measures].[Cost]
)'
select
{[Measures].[Cumul Claim Cost], [Measures].[Cost]} on columns,
{[Valuation Date].[Date].Members} on rows
from [WVWC DATA CUBE FROI SROI]
Please help.
Thanks.
Nevermin guys, I was able to get the correct query after tons of hit and trials.
with MEMBER [Measures].[Cumul Claim Cost] AS 'Sum ( PERIODSTODATE (
[Valuation Date].[All].level,
[Valuation Date].CurrentMember ) ,[Measures].[Cost] )'
select {[Measures].[Cumul Claim Cost], [Measures].[Cost]} on
columns, NON EMPTY {[Valuation Date].[Quarter- Year]} on rows from
[WVWC DATA CUBE FROI SROI]