Forum Discussion

WaelTalaat79's avatar
WaelTalaat79
Helper I
4 years ago

Hi dear

I am trying to calculate a if condition for previousmonth and I got this message 

MdxScript(Model) (13, 76) Calculation error as attached photo 

6 Replies

  • WaelTalaat79 

    Modify your meusre as follows: 

    = IF( MONTH( PREVIOUSMONTH( 'Dates'[Date].[Date] ) ) = 12 , 0 , 1 )



    • WaelTalaat79's avatar
      WaelTalaat79
      Helper I

      error message MdxScript(Model) (13, 72)

       

      = 12 mean December so should I select MonthNo instead of date

      • Fowmy's avatar
        Fowmy
        Super User

        WaelTalaat79 

        Where/what are you using this measure? Please share screenshot of the calcualtion 

  • v-luwang-msft's avatar
    v-luwang-msft
    Community Support

    Hi WaelTalaat79 ,

    You have to read the fine print here to understand how PREVIOUSMONTH (and similar functions) work:

    https://support.office.com/en-US/article/PREVIOUSMONTH-Function-DAX-72fb8dda-6cd6-49e6-b9b4-8a5f6324...

     

    "This function returns all dates from the previous month, using the first date in the column used as input. For example, if the first date in the dates argument refers to June 10, 2009, this function returns all dates for the month of May, 2009."

     

    So, mainly you have to use this in a "context aware way". Here is the example from the page:

     

    Example

    The following sample formula creates a calculated field that calculates the 'previous month sales' for the Internet sales.

    To see how this works, create a PivotTable and add the fields, CalendarYear and MonthNumberOfYear, to the Row Labels area of the PivotTable. Then add a calculated field, named Previous Month Sales, using the formula defined in the code section, to the Values area of the PivotTable.

    =CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]), PREVIOUSMONTH('DateTime'[DateKey]))

     

    And you could use the measure to get last month data:

     

    lastmonth =
    IF (
        MONTH ( MAX ( 'Date'[Date] ) ) = 1,
        DATE ( YEAR ( MAX ( 'Date'[Date] ) ) - 1, 12, DAY ( MAX ( 'Date'[Date] ) ) ),
        DATE ( YEAR ( MAX ( 'Date'[Date] ) ), MONTH ( MAX ( 'Date'[Date] ) ), DAY ( MAX ( 'Date'[Date] ) ) )
    )

     

     

     

     

    lastmon2 = IF (
        MONTH ( MAX ( 'Date'[Date] ) ) = 1,12,0
        
       
    )

     

     

    And you could refer:

     
     

    Did I answer your question? Mark my post as a solution!


    Best Regards

    Lucien