MDX Calculating 12 month average unit price and applying to forecast quantity - ssas

I have a requirement to show a report which calculates the average selling unit price of each product and then multiplies this average by the number of units forecasted to sell in the next year (for each product).
At this point my main issue is getting this set up so the totals roll up correctly when viewing at product category level. Using AdventureWorks as an example (and sales orders instead of forecasts) I've got this far...
with
member [Measures].[Sales Order Value]
as sum(descendants([Product].[Product Categories].currentMember,
[Product].[Product Categories].[Product]),
[Measures].[Average Unit Price] * [Measures].[Order Count]),
format_string = "Currency"
select ([Date].[Calendar].[Calendar Year].&[2008],
{[Measures].[Sales Order Value] }) on columns,
[Product].[Product Categories].[Subcategory].members on rows
from [Adventure Works]
I think this is about right, I believe this is going down to product level to apply the calculation between product average unit price and any sales orders for the product.
My issue is that I think the average unit price is being calculated over all data, I need to alter this to pick up an average based on the last 12 months only.

Related

Cumulative Sum | Exclude attribute from calculation

I am trying to do Cumulative Sum/ Running Total using the function below so the user can use any date level, but the requirement is to ignore some of the attributes from the calculation.
Function used:
SUM(NULL:Axis(1).Item(0).Item(Axis(1).Item(0).Count-1).Hierarchy.CurrentMember, [Measures].[ Number of Ticket])
Example:
The table below shows the Cumulative Sum as expected
Example - 1
Here by adding another attribute, Program Remaining, as shown below, its changes the Cumulative behavior, Because Excel will add that attribute to the grouping so it reset the cumulative sum:
Example - 2
Is there a way that I can exclude the Program Remaining attribute from the calculation (I have another 4 attributes that I want to exclude) so that the cumulative can be increased just like the first table even with adding these attribute.
I really appreciate any help
Try using the below sample query
with
member
[Measures].[Internet Sales AmountRunningtotal]
as
case when [Measures].[Internet Sales Amount] = null then null
else
sum({[Product].[Subcategory].firstchild:[Product].[Subcategory].currentmember},[Measures].[Internet Sales Amount])
end
select {[Measures].[Internet Sales Amount],
[Measures].[Internet Sales AmountRunningtotal]
} on columns,
non empty
([Date].[Calendar Year].[Calendar Year],[Date].[Calendar Quarter of Year].[Calendar Quarter of Year],
[Product].[Category].[Category],[Product].[Subcategory].[Subcategory])
on
rows
from
[Adventure Works]
So I explored the sample sent, Your only solutions is to keep the date in the inner most position, your query qouted above will break if you are not using entire members of an attribute.

MDX - How skip 0 values in MIN agregation and how exclude some percent of results?

In my cube I have
Earnings as measure with MIN aggregation
Dimension: [Localization].[Type].&[center]
Dimension: {[Date].[Year].&[2017], [Date].[Year].&[2018]}
My query is:
What are the minimum earnings of the person who decides to buy
apartments in the city center, excluding 5% of the lowest, within
last 2 years?
Now my mdx query looks like that:
SELECT
[Measures].[MinEarnings] ON COLUMNS
FROM [cube]
WHERE
(
BottomCount ([Localization].[Type].&[center], 95, [Measures].[MinEarnings]),
{[Date].[Year].&[2017], [Date].[Year].&[2018]}
)
I have two problems:
Some earnings are 0 - how can I skip them in calculations?
If my query correctly excludes 5% of the lowest earnings?
First of all you should use toppercent not bottomcount. you want the min salary of a person who is not in last 5% not last 5. Toppercent will give you the top 95%.
Secondly to filter 0 you can use the following syntax
toppercent (
filter([Localization].[Type].&[center], [Measures].[MinEarnings]>0)
, 95, [Measures].[MinEarnings])
Even now placing the code in the where clause might not work, however try it. I would suggest that you move the toppercent to rows , then order it, then take the top1
topcount(
order(
toppercent (
filter([Localization].[Type].&[center], [Measures].[MinEarnings]>0)
,95, [Measures].[MinEarnings])
,[Measures].[MinEarnings],asc)
,1)
I have an example which gives the minum sales amount of cities, notice i have replaced nulls with 0 to make it as close as possible to your case
with member [Measures].[Internet Sales Amount2]
as
case when ([Measures].[Internet Sales Amount])=null then 0 else [Measures].[Internet Sales Amount] end
select [Measures].[Internet Sales Amount2]
on columns ,
topcount(order(toppercent(filter([Customer].[City].[City],[Measures].[Internet Sales Amount2]>0),95,[Measures].[Internet Sales Amount2]),[Measures].[Internet Sales Amount2],asc),1)
on rows
from [Adventure Works]
where [Customer].[Country].&[Canada]
in the picture below is the result before topcount 1

Wrong percentage calculation in Hierarchy

I have a hierarchy Customer Group->Customer Name->Document No. I am calculating percent of Total for top 20 customers.
Problem here is that percentage is calculated correct till the Customer Name Level but fails for document No level.
[Measures].[Sales LCY]
/
Sum(
[DimCustLedgerEntry].[Customer].[All DimCustLedgerEntry]
,[Measures].[Sales LCY]
) ,FORMAT_STRING="PERCENT"

Period on Period growth for a dynamic measure

I wrote an mdx script showing period on period growth for Internet Sales Amount, and it all works fine.
We are using an interface, where you can place a slicer so that user can choose what dimension of date.Calendar he is interested in, i.e.whatever dimension of Date.Calendar the user decides to choose (Quarter, Month, Year) it will correctly look at previous member. Now, im trying to obtain the same for Measure - i.e. i want it to be flexible dependant on what measure the user will choose in the slicer ( i.e. Internet tax Amount).
I cant think of a way of creating a previous member with measure being flexible...
WITH
MEMBER [Measures].[Internet Sales Amount PP] AS
(
[Date].[Calendar].CurrentMember.Prevmember,
[Measures].[Internet Sales Amount]
)
MEMBER [Measures].[Period on Period Grwth %] AS
IIF (
[Measures].[Internet Sales Amount PP] = 0,
'N/A',
([Measures].[Internet Sales Amount]-[Measures].[Internet Sales Amount PP]) /[Measures]. [Internet Sales Amount PP]
)
,FORMAT_STRING = "percent"
SELECT
[Date].[Calendar].[Month] ON ROWS,
{[Measures].[Internet Sales Amount],
[Measures].[Internet Sales Amount PP],
[Measures].[Period on Period Grwth %]} ON COLUMNS
FROM [Adventure Works]
If you happen to use SSRS, you can add a parameter to absorb the measure selected in the drop down from front end. We host SSRS in SharePoint environment and this is how we do it.
WITH
MEMBER [Measures].[Measure Growth PP] AS
(
[Date].[Calendar].CurrentMember.Prevmember,
strtomember('[Measures].'+ #MeasureSelected)
)
MEMBER [Measures].[Period on Period Grwth %] AS
IIF (
[Measures].[Measure Growth PP] = 0,
'N/A',
(strtomember('[Measures].'+ #MeasureSelected)-[Measures].[Measure Growth PP]) /
[Measures].[Measure Growth PP]
)
,FORMAT_STRING = "percent"
SELECT
[Date].[Calendar].[Month] ON ROWS,
{strtomember('[Measures].'+ #MeasureSelected),
[Measures].[Measure Growth PP],
[Measures].[Period on Period Grwth %]} ON COLUMNS
FROM [Adventure Works]

Count maximum sequel of null values - mdx query

I want to create a member based on this problem
I have a Product A being sold
I want to find the largest range of consecutive days without sale
example:
days 1,2,3 the product not sale, after that,it sold for 15 consecutive days, at 19th day it didnt sell for 2 days and after that it sold every day until the end of the month
so my maximum days without sale was 3
The following query delivers in the Microsoft sample cube Adventure Works what you want:
WITH Member Measures.[days without sales] AS
IIf( [Measures].[Internet Sales Amount] > 0
, 0
,(Measures.[days without sales], [Date].[Calendar].CurrentMember.PrevMember) + 1
)
Member Measures.[Max days without sales] AS
Max( [Date].[Calendar].[Date].Members
,Measures.[days without sales]
)
SELECT { [Measures].[Max days without sales] }
ON COLUMNS
FROM [Adventure Works]
WHERE [Product].[Product].&[486]
The measure days without sales is defined recursively, and returns how many days up to and including the current member of the [Date].[Calendar] hierarchy there was no sales. You may need to adapt the criteria for "without sale", bearing in mind that in MDX, numerical comparisons treat NULL as 0 - which is different from SQL.
This measure only works correctly if there is a member in this hierarchy for each day, i. e. there are no gaps in this hierarchy. And actually, the definition is more general than just working for days: If you use months for the [Date].[Calendar].CurrentMember, it would give you the number of months without sales, etc. It works with each level of the hierarchy.
The measure Max days without sales does not contain the product in its definition, it delivers the maximum days for whatever is in context (in this case the product in the WHERE clause).
Please note that - as actually there is a loop over all days in the [Date].[Calendar] hierarchy when calculating Measures.[Max days without sales], and within that the recursion again iterates along the previous days, and all this for each cell in the result set - this may be slow for large reports.