简体   繁体   中英

MDX Calculation in SSAS for Sum of Sales from Start date till today

I am trying to write a query in ssas calculations tab for which should produce the below result. It is like a YTD calculation which i am calculating from 1st Feb 2016 till today. I have written the below query in Management Studio but i need to convert it into SSAS calculations and write it into Calculations tab.

WITH 
  MEMBER [Measures].[ytd Sales Target 2] AS 
    Sum
    (
        StrToMember
        (
          '[Sales Date].[Date].&[' + Format(Now(),'yyyy-') + '02-01T00:00:00]'
        )
      : 
        StrToMember
        (
              '[Sales Date].[Date].&[' + Format(Now(),'yyyy-') + Format(Now(),'MM-')
            + 
              Format
              (
                Now()
               ,'dd'
              )
          + 'T00:00:00]'
        )
     ,[Measures].[sales target]
    ) 
SELECT 
  [Measures].[ytd Sales Target 2] ON 0
FROM [sales];

It should be as simple as:

CREATE MEMBER CURRENTCUBE.[Measures].[ytd Sales Target 2]
AS
Sum
    (
        StrToMember
        (
          '[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]'
        )
      : 
        StrToMember
        (
              '[Sales Date].[Date].&[' + Format(Now(),'yyyy-MM-dd')+'T00:00:00]'
        )
     ,[Measures].[sales target]
    ),
VISIBLE = 1;  

This way won't take your Date into account. But with a little change it can be "time aware". I added a little check for dates before 1st Feb to be ignored.

CREATE MEMBER CURRENTCUBE.[Measures].[ytd Sales Target 2]
AS
iif(
    [Sales Date].[Date].CURRENTMEMBER.MEMBER_KEY < StrToMember('[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]').MEMBER_KEY
    ,NULL
    ,Sum (
        StrToMember
        (
          '[Sales Date].[Date].&[' + Format(Now(),'yyyy') + '-02-01T00:00:00]'
        )
      : 
        StrToMember
        (
              [Sales Date].[Date].CURRENTMEMBER
        )
     ,[Measures].[sales target]
    )

),
VISIBLE = 1;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM