mdx get measures for a date range between startDate and endDate - date

I need to know what is the rate of booking of beds (Like in an hotel).
The number of beds (summed month per month) for a range of dates, for the bookings that are in the range (including the partial sum of bookings for the dates that are partially in the range)
I created a "booking" fact table with a StartDate and a EndDate with a measure "countSejoursDate" (count(rows)) and a measure "NbrOfBeds" (sum).
I created 2 "wizard time" dimensions linked as following :
I also created a 3rd "wizard time" dimension called "Date" not linked to any fact.
While trying to get the result, using the MDX below, I'm just able to retrieve the count of rows inside a range of dates... but even with this, the value of the 1srt day of each month is false!
with member nbsejsDate as AGGREGATE(
{NULL:LINKMEMBER([Date].[Calendrier].CURRENTMEMBER,[START_DATE].[Start_Calendrier])}
* {LINKMEMBER([DATE].[Calendrier].CURRENTMEMBER, [END_DATE].[End_Calendrier]):NULL}
, [Measures].[countSejoursDate])
select nbsejsDate
on 0
, [Date].[Calendrier].[Jour].&[2015-03-01]:[Date].[Calendrier].[Jour].&[2015-03-31] on 1
from [Cube]

It's a bit strange as what we've here is an many-to-many relation in the form of a start and end date. Trying to make a correct calculation relying on MDX calculation instead of using a many-to-many relation it's tricky and very,very error prone.
There are different possibilities for solving this :
Use a Range (From - To) link type in the link of time dimension in the Facts.
Use a Javascript view to create an new column that is an array of dates (start/end). The many-to-many relation is created on the fly.
This should make a lot easier any calculation, if I understood the problem correctly.
hope it helps

Related

MDX calculated measures with date comparisons

I'm new of MDX and I'm trying to calculate a new measure based on two different date dimensions.
I have the Creation Date Dimension (with Year, Trimester, Month, Day) and Resolution Date (with Year, Trimester, Month, Day).As measure I have the number of tickets and I want to calculate two new measures in order to know how many tickets that were resolved this month were actually created last month and how many tickets were resolved in the same month as they were created.
I found this interesting post, but I cannot understand how to use properties..
https://bennyaustin.com/2012/06/05/ssas-mdx-calculated-measures-that-require-date-comparison/
Any ideas or suggests?
Thanks for your help.
This question cannot be answered as asked without knowing the exact definition of your time dimensions.
An approach which might be worth considering if your MDX knowledge is little, but you have some SQL knowledge and if your requirements can be fixed to just the month level as you described, could be the following, which does the main calculations already when loading the cube, and not at query time in MDX:
Add a column 'months_from_creation_to_resolution' to your fact table, possibly just add this as a column in the view you may already use on the fact table. This column would be 0 if the ticket was resolved in the same month where it was created, 1 if it was resolved in the month after creation, 2 if it was resolved e. g. in May and created in March, etc. You do this calculation using SQL date functions. You would then create a new dimension in your cube from this table, which would have the new column as the only attribute. SSAS has no problem using a table as base for both a measure group and a dimension.
Then, in MDX, the number of tickets resolved in the month they were created would just be
([Measures].[TicketCount], [Fact].[months from creation to resolution].[0])
and those resolved in the month after creation would be
([Measures].[TicketCount], [Fact].[months from creation to resolution].[1])
As a side effect, the query run time would be faster, as the main logic is pre-calculated when loading the cube.

Double aggregation in Tableau using LOD expressions

I am using Tableau to create a custom google analytics dashboard. I have a custom dimension named author in my google analytics view and I would like to group the date of the first page/view by author and by month having a counter.
I successfully get the date of first page/view using MIN([Date]), but I don't figure out how to use LOD expressions to double aggregate a calculation. I tried the following expression, but tableau shows an error saying that the argument I'm trying to count is already an aggregation and can no longer be aggregated.
{INCLUDE [Author] : COUNT(MIN([Date]))}
What did I miss ?
If your are trying to find the minimum of your [Date] field by your [Author] field, you might want to construct a LOD calculation to find first date, by author like so:
//Creates calculated field for [First Date by author]
{FIXED [Author] : MIN([Date])}
You also have asked about 'double aggregating'. Let's say for example that you wanted find the minimum date, by author, but also by a [Topic]. In this case, you could write:
//Creates calculated field for [First Date by author, by Topic]
{FIXED [Author],[PostTopic] : MIN([Date])}
It is also possible to nest FIXED statements within one another. Let's say for example that you wanted to show maximum [PageViews] for a single month by [Author] and also wanted to limit the time span under consideration to to the first 3 months that an author was publishing. In this case, you could write:
//Creates calculated field for [Most page views in a single month within first 3 months, by Author]
//Example assumes [Date] is a monthly (not daily) data
{ FIXED [Author], [Date] <= DATEADD('month', 3, [First Date by author] ) : MAX({FIXED [Author],[Date]:SUM([PageViews])})}

MDX number of days between shell date dimension and regular date dimension

I have a shell date dimension, and a sale date dimension. Having trouble setting up a calculated measure with the difference in days between the 2 dates.
I have tried a number of things, and the calculation always seems to return an error.
mdx example is:
WITH
MEMBER [Measures].[TimeDate] AS [Date].[Day].currentmember
MEMBER [Measures].[DSODate] AS [DSO Date].[Day].currentmember
MEMBER [Measures].[DaysSinceSale] AS
DateDiff(
"d"
, [Measures].DSODate.MemberValue
, [Measures].TimeDate.MemberValue
)
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[Day].members} ON ROWS
from [Receivables];
I have tried using DateDiff, and tried just subtracting the 2 dates.
Assuming it may have something to do with the 2 date dimensions being of different hierarchies, but i am not really sure how to handle that.
MDX Results
Date conversions can be tricky in mdx so maybe initially try the following simple approach:
WITH
MEMBER [Measures].[TimeDate] AS [Date].[Day].currentmember
MEMBER [Measures].[DSODate] AS [DSO Date].[Day].currentmember
MEMBER [Measures].[DaysSinceSale] AS
DateDiff(
"d"
, VBA!CDate([Measures].DSODate.MemberValue)
, VBA!CDate([Measures].TimeDate.MemberValue)
)
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[Day].members} ON ROWS
from [Receivables];
Otherwise you might need to use the key and an approach similar to this:
MDX - Converting Member Value to CDate
I found a way to get this to work ...
Main issue was that i didn't have a crossjoin, like whytheq mentioned.
I also didn't need the custom Measures declared at the top.
The other adjustment i made was to utilize the DateKey in the date calculation. That seemed to work in all my tests, and improved performance quite a bit.
WITH
MEMBER [Measures].[DaysSinceSale] AS
[Date].[DateKey].CurrentMember.MemberValue - [DSO Date].[DateKey].CurrentMember.MemberValue
Select
{[Measures].[DaysSinceSale]} ON COLUMNS,
{[Date].[DateKey].Members * [DSO Date].[DateKey].members} ON ROWS
from [Receivables];
If you see any issues that may arise with using DateKey let me know. For what i am doing that seemed to pull back the correct date value, and allowed me to find the difference between dates without using a datediff function.

Tableau: How to calculate number of days

I have custom SQL query in tableau that displays the data in the following format:
Start Date | App
6/21/16 app1
6/22/16 app2
6/23/16 app3
In this case, the end date would be '6/23/16'. So, app1 has been "live" for 2 days, app2 for 1 and so on.
I am trying to find the the number of days an app has been live. I can try using the DateDiff function but I would need to hardcode the values in that case and I want it to be dynamic.
The challenge is to have a calculated field that would find the max date in the entire column and subtract it from the individual app's date. This would give me the 'number of live days' for an app.
I am new to tableau and do not know how to proceed. Any help is appreciated.
Here is one solution.
datediff('day', [Start Date], { fixed : max([Start Date]) } )
Note the expression in Curley braces. That is a level of Detail (LOD) calculation -- basically a separate subquery at a potential different level of detail. So you can compare values for each row with values computed based on the whole table.
Depending on how and where you want to use this calculation, you might want to alter that LOD calculation to be fixed for certain dimensions or include or exclude certain dimensions. The online help should explain.
Just use"DATEDIFF('day',[Order Date], [Ship Date])",
order date and ship date are example dimensions from superstore data.xlxs

MDX - calculate one date dimension from another date dimension

I have a fact table that has 2 dates Invoice Date and Accounting Current Date. In order to get requested Revenue value I need to use combination of these two dates. For example, if I need YTD Revenue I need to select it like this:
(Note: I am writing SQL query because I am more familiar with it)
SELECT Revenue
FROM
Fact_Revenue
WHERE
Invoice_Date <= '2011-10-22'
and AccountingCurrent >= '2011-01'
and AccountingCurrent <= '2011-10'
Besides Revenue, this fact tables has other information that I also need, but for calculating this other data I don't need Accounting Current Date. So my idea is to use only 1 date (Invoice Date) in main MDX query (so that I can grab as many data with 1 query as I can) and for calculating Revenue I would like to use Calculated Member and in there I would like to associate Accounting Current Date with selected Invoice Date.
For example
SELECT {[Measure].[RevenueYTD],
[Measure].[RevenueMTD],
[Measure].[NumberOfInvoices],
[Measure].[NumberOfPolicies]}
ON COLUMNS,
{[People].Members} ON ROWS
FROM [Cube]
WHERE
[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]
In this case, [Measure].[RevenueYTD] and [Measure].[RevenueMTD] need to be limited by Accounting Current Date and Invoice Date must be lower than the date from the query. On the other hand, I need [Measure].[NumberOfInvoices] and [Measure].[NumberOfPolicies] for particual Invoice Date (or MTD Date, whatever), but without involvemenet of Accounting Current Date
Calculated member query should do something like this (this is more like algorithm):
ROUND(
SUM(
YTD([Accounting Current Date].[Date Hierarchy].CurrentMember),
[Measures].[Revenue]
),
2)
WHERE [Invoice Current Date].[Date Hierarchy] < [Invoice Current Date].[Date Hierarchy].CurrentMember
Navigating from one dimension to another is not something trivial in MDX. In theory dimensions are independent so standard language is missing functions for doing this. You can use StrToMember MDX function but it's slow and a bit strange.
For your filters, let's start with the first one :
Invoice_Date <= '2011-10-22'
In MDX we'll have to create a set with the members matching the expression. This can be done using the Range set operator :
NULL:[Invoice Date].[Date Hierarchy].[Date].&[2011-10-22]
The other filter is easy to guess :
AccountingCurrent >= '2011-01' and AccountingCurrent <= '2011-10'
MDX version :
[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]
It's also possible using Filter MDX function if your need different type of filters.
Now we need to take the pieces and build the query. One possible solution is using a set slicer and overwritting the values when you don't want the filter to be applied :
WITH
// here we're changing the 'selection' from the where clause
MEMBER [Measure].[NumberOfInvoices II] AS ([Accounting Date].[Date Hierarchy].defaultmember,[Measure].[NumberOfInvoices])
SELECT
.. axis here [Measure].[RevenueYTD] will be applying the filters defined in the where clause
FROM MyCube
WHERE {[Accounting Date].[Date Hierarchy].[Date].&[2011-01-31]:[Accounting Date].[Date Hierarchy].[Date].&[2011-10-30]}