Calculating the difference between two dates
This recipe shows how to calculate the difference between two dates. We're going to use promotions as an example, and calculate the time span of a promotion, from the start date to the end date.
Getting ready
Start SQL Server Management Studio and connect to your SSAS 2012 instance. Click on the New Query button and check that the target database is Adventure Works DW 2012. Then execute this query:
SELECT { [Measures].[Reseller Order Count] } ON 0, { [Promotion].[Start Date].[Start Date].MEMBERS * [Promotion].[End Date].[End Date].MEMBERS } ON 1 FROM [Adventure Works] WHERE ( [Promotion].[Promotion Type].&[Discontinued Product] )
The query shows that the Discontinued Product
promotion appeared twice with various time spans. Our task is to calculate how many days it lasted each time.
How to do it...
Follow these steps to calculate the difference between two dates:
Add the
WITH
part of the query.Define two calculated measures...