Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Help with comparting data dates

Hi,

 

I would like to do some calculations with the below data.

YTD - which I know I could use the TOTALYTD + Filter

 

TotalYTD - Actual = TOTALYTD(Sum('Table'[GM]),'Table'[Full Date],FILTER('Table','Table'[Measure Version]="Actual"))

 

 

But how do I do a calculation with the same date range ie m January -  September 2018 for the Plan and RF?

Also the total of last year for the same date range January -  September 2017?

 

Measure VersionGMFull Date
Actual$810988387.8501/01/2017 00:00
Actual$337791028.1301/02/2017 00:00
Actual$550435475.7601/03/2017 00:00
Actual$402794574.6601/04/2017 00:00
Actual$467615215.9801/05/2017 00:00
Actual$540032362.8301/06/2017 00:00
Actual$431805160.2501/07/2017 00:00
Actual$463675935.8301/08/2017 00:00
Actual$501191739.4101/09/2017 00:00
Actual$438198301.6801/11/2017 00:00
Actual$396214086.6001/12/2017 00:00
Actual$259308342.4901/01/2018 00:00
Actual$328621377.2701/02/2018 00:00
Actual$446070845.3701/03/2018 00:00
Actual$357567501.3601/04/2018 00:00
Actual$456199138.7601/05/2018 00:00
Actual$421862533.3701/06/2018 00:00
Actual$392615815.8201/07/2018 00:00
Actual$465390953.8301/08/2018 00:00
Actual$442974962.6501/09/2018 00:00
Plan$913421956.5701/01/2017 00:00
Plan$412362821.4501/02/2017 00:00
Plan$518063594.2301/03/2017 00:00
Plan$556249173.6801/04/2017 00:00
Plan$524580575.3801/05/2017 00:00
Plan$555159147.8501/06/2017 00:00
Plan$526988132.0401/07/2017 00:00
Plan$552664759.9701/08/2017 00:00
Plan$605712510.4401/09/2017 00:00
Plan$587320990.5901/11/2017 00:00
Plan$532713634.6201/12/2017 00:00
Plan$937890299.6001/01/2018 00:00
Plan$336840287.0601/02/2018 00:00
Plan$467714233.5501/03/2018 00:00
Plan$456025669.1601/04/2018 00:00
Plan$472688772.2801/05/2018 00:00
Plan$550095419.8601/06/2018 00:00
Plan$551625717.8901/07/2018 00:00
Plan$574777728.5901/08/2018 00:00
Plan$653631927.1301/09/2018 00:00
Plan$650477750.5001/11/2018 00:00
Plan$549065649.0201/12/2018 00:00
RF$767205034.6501/01/2018 00:00
RF$328621377.2701/02/2018 00:00
RF$446070845.3701/03/2018 00:00
RF$357567501.3601/04/2018 00:00
RF$456199138.7601/05/2018 00:00
RF$421862533.3701/06/2018 00:00
RF$392615815.8201/07/2018 00:00
RF$465390953.8301/08/2018 00:00
RF$607816648.6201/09/2018 00:00
RF$541901652.9201/11/2018 00:00
RF$514735218.3401/12/2018 00:00

 

Thank you 

  • Hi Anonymous

    The formula in your lastest post is correct to solve this problem.

    So far, it is a useful workaround for your problem.

     

    The penultimate one shows a incorrect formula.

    MinDate =
    CALCULATE (
    MIN ( 'Table'[Full Date] ),
    FILTER ( 'Table', 'Table'[Measure Version] = "Actual" ),
    FILTER ( 'Table', 'Table'[Full Date] = YEAR ( 2018 ) ) //incorrect
    )

    Please see reference how to use "calculate" with "filter"

    https://www.sqlbi.com/articles/filter-arguments-in-calculate/

     

    If the formula is used in a measure, you could use the following instead.

    YTD Plan =
    VAR TableMaxDate =
        CALCULATE (
            MAX ( 'Table'[Full Date] ),
            FILTER ( ALL ( 'Table' ), 'Table'[Measure Version] = "Actual" )
        )
    VAR MinDate =
        CALCULATE (
            MIN ( 'Table'[Full Date] ),
            FILTER (
                ALL ( 'Table' ),
                'Table'[Measure Version] = "Actual"
                    && YEAR ( 'Table'[Full Date] ) = 2018  //from the information, it seems it is no 
    need to add this part, if so,
    you could delete this part
    ) ) RETURN CALCULATE ( SUM ( 'Table'[GM] ), FILTER ( 'Table', 'Table'[Measure Version] = "Plan" ), DATESBETWEEN ( DIM_Date[Date], MinDate, TableMaxDate ) )

    Best Regards

    Maggie

22 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Jeltex,

       

      I have just tried this;

       

       

      Last Year = 
      CALCULATE (
          [TotalYTD - Actual],
          SAMEPERIODLASTYEAR ( 'Table'[Full Date] )
      )

       

       

      But i get this error when i bring in other columns;

       

      MdxScript(Model) (12, 5) Calculation error in measure 'Table'[Last Year]: Function 'SAMEPERIODLASTYEAR' expects a contiguous selection when the date column is not unique, has gaps or it contains time portion. 

       

      I am guessing that this is due to me not having contiguous date data, but i have not really used a date table before.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Try creating a simple date dimension by using add table and then the following code:

        DIM_Date = CALENDARAUTO()

        Then set up a relationship between 'DIM_Date'[Date] and 'Table'[Full Date].

         

        Then change your code to:

        Last Year = 
        CALCULATE (
            [TotalYTD - Actual],
            SAMEPERIODLASTYEAR ( 'DIM_Date'[Date] )
        )