Forum Discussion

snandy2011's avatar
snandy2011
Helper IV
7 years ago
Solved

Prior month calculations (specific date range)

Hi all,   I am having trouble comparing current month with specific date range with previous month's same date range. I am able to calculate previous month calculaion by below DAX formula,   Prio...
  • v-frfei-msft's avatar
    7 years ago

    Hi snandy2011,

     

    I made one sample for your reference. You can refer to the steps as below to meet your requirement.

     

    1. Enter the sample data and create a dimtime table.

     

    dimtime = CALENDARAUTO()
    Month = FORMAT(dimtime[Date],"mmm")
    weekinmonth = CONCATENATE(dimtime[Month],"-week"&1 + WEEKNUM ( dimtime[Date] )-WEEKNUM( STARTOFMONTH (dimtime[Date])))

    2. Create a measure as below.

     

    Measure = 
    VAR Year =
        YEAR ( MINX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR month =
        MONTH ( MINX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR day =
        DAY ( MINX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR may =
        YEAR ( MAXX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR mam =
        MONTH ( MAXX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR maxday =
        DAY ( MAXX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ) )
    VAR todayweek =
        CONCATENATE (
            FORMAT ( TODAY (), "mmm" ),
            "-week"
                & 1 + WEEKNUM ( TODAY () )
                    - WEEKNUM ( DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ) )
        )
    RETURN
        IF (
            SELECTEDVALUE ( dimtime[weekinmonth] ) = BLANK ()
                && SELECTEDVALUE ( dimtime[Month] ) = BLANK (),
            BLANK (),
            IF (
                FORMAT ( MAXX ( RELATEDTABLE ( 'Poker Profit' ), 'Poker Profit'[Date] ), "mmm" )
                    = FORMAT ( TODAY (), "mmm" ),
                CALCULATE (
                    SUM ( 'Poker Profit'[Gross Gaming Revenue] ),
                    FILTER (
                        ALL ( dimtime ),
                        dimtime[Date]
                            >= DATE ( Year, month - 1, day )
                            && dimtime[Date]
                                <= DATE ( may, mam - 1, maxday )
                    )
                ),
                CALCULATE (
                    SUM ( 'Poker Profit'[Gross Gaming Revenue] ),
                    DATEADD ( dimtime[Date], -1, MONTH )
                )
            )
        )
    

    Then we can get the result as we excepted.

     

     

    For more details, please check the pbix as attached.

     

    Regards,

    Frank