SSAS Date Measure Max StringFormat when Null - formatting

I have a cube with a measure of type date, performing a max aggregation, where some of the data points have a null value for date. In these cases, the Max value becomes 12/30/1899 which I believe is the earliest expression of a date.
Is there a way to have these appear in the results as blank rather than this bogus date?
I tried to use the measure's StringFormat property with no luck.

Change the NullProcessing property for your measure to Preserve. You can do this in BIDS. Open the project, then the cube, then on the Cube Structure tab expand the measure group and then select the measure. In the properties pane select Source, expand it and you should see the property there. After you process, the measure will be NULL instead of the default 0 which gets assigned to NULLs. The 0 then gets cast to a date and becomes 00/01/1900 in Excel.

Related

Using a date range in calculated member in MDX query for Tabular cube model

I have a requirement where I have to get the values of different measure with each measure having its own date range, the following query works and gets me the data over an year,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Year].&[2019])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
but when I try to pass a date range instead of year, I'm getting an error,
WITH MEMBER [Measures].[Ex1] AS ([Measures].[Average Spending], [Date].[Year].&[2020])
MEMBER [Measures].[Ex2] AS ([Measures].[Average Time Spent], [Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020])
SELECT {[Customer].[Name].[Name]} ON 1, {[Measures].[Ex1],[Measures].[Ex2]} ON 0 FROM [Model];
I'm getting the following error,
Executing the query ... The function expects a string or numeric
expression for the argument. A tuple set expression was used. Run
complete
How do I get the measure for a date range for each calculated member?
If you do not use a tuple (i. e. a combination of single members of the hierarchies like Measures or Year), but a set, then you need to tell MDX what to do with the set elements to get to a single value. I would assume the following will give what you want:
WITH
MEMBER [Measures].[Ex2] AS
Aggregate([Date].[Date].[01-May-2020]:[Date].[Date].[31-May-2020],
[Measures].[Average Time Spent]
)
...
Instead of Aggregate, you could also use Sum, Avg, Variance etc. but normally, I prefer using Aggregate, as this uses whichever aggregation method is defined for the measure in the cube, i. e. it is more universal, and the query does not need to be adapted to changes in the cube calculation scripts.
See also Aggregate in the SSAS documentation

How to find the first value in a range in MDX

I'm trying to create an SSAS calculated measure off of an inventory cube. The underlying data contains an inventory_date, a product code, and some inventory values for the product for that date. I am trying to create a measure which will return the first inventory value for the month, for the product (and overall). I've seen people try to use the head function to no avail. I've also just seen a reference to the openingperiod function, which seems to be exactly what I want, but it also isn't returning the correct value. Both of them, FWIW, are returning the last value in the month rather than the first.
Here is a query that I am issuing:
select
[Measures].[Qty On Hand EOD] on 0,
{openingperiod([Inventory Date].[Calendar].[Month]):
[Inventory Date].[Calendar].[Month].&[2018-06-25T00:00:00]
} on 1
from [Daily Inventory]
This query returns for May 2018 the value of [Qty On Hand EOD] for May 31, rather than May 1, which is what I want. What am I doing wrong?
FWIW, in the cube definition the AggregateFunction property for [Qty On Hand EOD] is set to LastNonEmpty (as opposed to Sum or Avg). Could that play a role?

Create MDX to Divide two measures for each month and then sum the all of the months

I have a multidimensional cube that needs a custom measure that I'm not sure how to build.
That data looks like this:
MemberID-----Date-------EventType
1--------------1/1/2016-------1
2--------------1/1/2016-------2
3--------------2/1/2016-------1
2--------------2/1/2016-------2
4--------------2/1/2016-------2
There is a count measure in the cube, but others can be added if needed. I need to create a measure that will use whatever filters the user applies and then count the EventType (1 and 2 only) by month, divide the resulting counts for EventType 1 into the count for EventType 2 (for each month individually), and finally sum the monthly results. For example 1/1/2016 would be 1/1=1 (count of EventType 1 and count of EventType 2) and 2/1/2016 would be 1/2=0.5 so the resulting measure value for the two months would be 1+0.5=1.5. Any help is greatly appreciated.
Let's assume you have a Date dimension with an attribute called Month. And let's assume you have an EventType dimension. And let's assume you have a count measure in your measure group called Cnt. Here's what else you need to do.
First, go to the DSV and add a new calculated column to the fact table which is called NullInt and is the following expression:
cast(null as int)
Then create a new Sum measure in your measure group off that column and call the measure My Rollup. Under the Source property, change NullHandling to Preserve so that it will start off null.
To explain why we're doing this, a scoped assignment to a physical measure will aggregate up. (If you assign a value to a physical measure at the grain of each month, then it will rollup to the grand total.) But a scoped assignment to a calculated measure doesn't roll up.
Then in your MDX script add the following calculations:
scope([Date].[Month].[Month].Members); //calculate at the month level then rollup
[Measures].[My Rollup] = DIVIDE(
([Event Type].[Event Type].&[1],[Measures].[Cnt]),
([Event Type].[Event Type].&[2],[Measures].[Cnt])
);
end scope;
Note that your version of SSAS probably has the DIVIDE function if it's AS2012 with the latest service pack or newer. But if it doesn't, you can always do division the old fashioned way as IIF(denom=0,null,num/denom).

How to Calculate Sum untill start of month of a given date in DAX

I would like to calculate Sum(QTY) until the start date of the month for a given date.
Basically I can calculate Sum(QTY) until given date in my measure like:
SumQTYTillDate:=CALCULATE(SUM([QTY]);FILTER(ALL(DimDateView[Date]);DimDateView[Date]<=MIN(DimDateView[Date])))
But I also would like to calculate Sum(QTY) for dates before 10/1/2015 - which is the first date of selected Date's month. I have changed above measure and used STARTOFMONTH function to find first day of the month for a given date like;
.......DimDateView[Date]<=STARTOFMONTH(MIN(DimDateView[Date]))))
but not avail, it gives me
"A function ‘MIN’ has been used in a True/False expression that is
used as a table filter expression. This is not allowed."
What am I missing? How can I use STARTOFMONTH function in my measure?
Thanks.
STARTOFMONTH() must take a reference to a column of type Date/Time. MIN() is a scalar value, not a column reference. Additionally, your measure wouldn't work, because STARTOFMONTH() is evaluated in the row context of your FILTER(). The upshot of all this is that you would get a measure which just sums [QTY] across the first of every month in your data.
The built in time intelligence functions tend to be unintuitive at best. I always suggest using your model and an appropriate FILTER() to get to what you want.
In your case, I'm not entirely sure what you're looking for, but I think you want the sum of [QTY] for all time before the start of the month that the date you've selected falls in. In this case it's really easy to do. Add a field to your date dimension, [MonthStartDate], which holds, for every date in the table, the date of the start of the month it falls in. Now you can write a measure as follows:
SumQTY=SUM(FactQTY[QTY])
SumQTYTilStartOfMonth:=
CALCULATE(
[SumQTY]
;FILTER(
ALL(DimDateView)
;DimDateView[Date] < MIN(DimDateView[MonthStartDate])
)
)

mdx datediff error

select measures.name on 0,
datediff("d", [Fecha].[Date].currentmember.member_value, [Dim Date].[Date].currentmember.member_value) on 1
from cube
Error: Execution of the managed stored procedure datediff failed with
the following error: Exception has been thrown by the target of an
invocation.Argument 'Date1' cannot be converted to type 'Date'
Is there any requirements to do datediff in mdx?
In the dimension these member are defined as datetime, not sure if this influence in anyway the result...
Update: I solved the problem by making the calculation at the datasource view and at the cube I added a measure that I could use in MDX to create the indicator that I needed. Of course this is all using SSAS for testing the result and SSDT for creating the members. I hope this approach helps a lot of people even though i don't know if this is the best case scenario. Happy MDX ;)
By default, the axis will show, not the member_value, but the member name, which is always a string. The Member_Value is a measure.
With Member MyDateDiff as
datediff("d", [Fecha].[Date].currentmember.member_value, [Dim Date].[Date].currentmember.member_value)
Select
(Measures.MyDateDiff, Measures.Name) on 0
(Fecha.Date.Members, [Dim Date].[Date].Members) on 1
From cube
EDIT
I'm still new to this too, but after your comments, I thought I should elaborate on the process I went through to get similar queries right.
First, make sure you are getting the data you think you are. Get those Member_Value measures into the Columns dimension:
With
Member FechaDate as
[Fecha].[Date].currentmember.member_value
Member MyDate as
[Dim Date].[Date].currentmember.member_value
Select
(Measures.FechaDate, Measures.MyDate) on 0
(Fecha.Date.Members, [Dim Date].[Date].Members) on 1
From cube
Are you getting date values? If yes, try out your datediff function. If not, check the dimension definitions. I wouldn't be surprised to find the the ValueColumn of one of your attributes was set to the key field instead of the date value field, or some such thing.
You may need two steps, but the date add method should be fine. The Member approach from Bill is good, but it returns it as a string, not a date object. If it's in the correct format, you should be able to do:
Member MyDate as
DATEDADD("d", 1, VBAMDX!CDate([Dim Date].[Date].currentmember.member_value))
Depending on how your date string ends up formatted, you may have a little more work to do.