MDX Grand Total specifically - mdx

I am new to MDX and find it hard to search for someone with the same issue. The problem is how the grand total of the column 'Difference' is calculated. Currently this is the situation:
Item
Current Sales
Forecast Sales
Difference
A
200.000
150.000
50.000
B
100.000
110.000
10.000
Tot
300.000
260.000
40.000 (should be 60.000)
The formula for Difference is the absolute (ABS) of 'Current Sales' - 'Forecast Sales' (so negative values will be changed to positive.
But this effects the grand total. The grand total should always be a SUM of the total of all differences per item, even when collapsed.
So in this situation, it looks like the difference between Forecast and Current sales is 0, but actually it should be 200.000:
Item
Current Sales
Forecast Sales
Difference
A
200.000
100.000
100.000
B
100.000
200.000
100.000
Tot
300.000
300.000
0 (should be 200.000)
Is there a way in MDX to make this happen?
The actual current code in MDX:
CREATE MEMBER CURRENTCUBE.[Measures].[Difference] AS Abs (
[Measures].[Current Sales] - ([Measures].[Forecast Sales],
[Sales Forecast Filter].[Type].[2]))
The filter only makes sure the right values are added.

Related

MDX - Calculations of Daily Stock (Financial Instrument) Values

How do I calculate the daily values of a Persons Stock (Financial Instrument) using MDX.
A person buys Microsoft Shares/Stock like in the example below. I already have a SSAS MD Cube with this Information (DimDate, DimShare, FactInvestment, DimClient).Fund Units
Can easily calculate the Daily Balance of Units (Unit Balance) using MDX (sum(NULL:[Date].[Calendar].CurrentMember,[Measures].[Units]) to give me daily Balance of Units.[Daily Stock Value][2]
I have a Daily Share price Measure Group with a "Share Price" with Aggregation Usage Last non-empty value. This will give me the daily Stock price.[Daily Stock Price]. The Measure group DOES NOT have a Client dimension.[Daily Stock Price][3]
Would like to calculate the daily value of stock [Unit Balance]*[Share Price] = [Share Values] for each clientDaily Stock Values. See manual calculations in yellow on Pivot Table.

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.

SSAS Calculated Member - how to do percent of total based on another measure

I am currently trying to create a calculated measure for an SSAS 2008 R2 cube. In a financial cube dealing with accounts receivable data, I have a "Gross Balance" measure, and a "Days Since DOS" measure. The "Days Since DOS" measure is invisible to the user because it is only used in combination with a couple others to gain an average.
I would like the new calculated measure to show the percent of the total gross balance that has a Days Since DOS value > 90.
For example, if the total gross balance were $1000, the total gross balance for records with days since DOS > 90 being $500, the Percent Over 90 Days calculated measure would show 50%.
Would it be possible to do this with my current setup, and if so, how would I go about writing the expression?
I found out that it is in fact possible.
First, create a new named calculation in the DSV using a case statement (for example, call it [Gross Bal Over 90]):
CASE
WHEN [Days Since DOS] > 90 THEN [Gross Balance]
ELSE 0
END
Then, the calculated measure would simply be:
Sum([Gross Bal Over 90])/Sum([Gross Balance])
You can then make [Gross Bal Over 90] invisible to the user, keeping a cleaner look.

SSAS MDX Calculated Measure Over Time

I have a calculated measure that needs to cross join Customer and Product dimension then cross join a total sales measure to get a percentage for a specific customer sale.
[Measures].[Sale Value] / [Measures].[Total Sales]
each measure has a link to the time dimension, and are set to last non empty.
The problem is that as I look at more information over longer periods (days, months, years etc) it gets slower and slower and slower. I am assuming this is because the calculated measure does its processing on the fly and there is no caching.
Is this correct? I have about 2000 customers and 50 products.
Please please help! any information about how to speed this up would be great.
The answer to this was to set a many to many relationship between Customer/Prodcut and the [Measures].[Total Sales] measure group.

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

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.