Forum Discussion

Vanchy_Liao's avatar
Vanchy_Liao
Icon for Advocate IV rankAdvocate IV
4 years ago
Solved

DAX - Measure is wrong when using Date for analyze Calculate and DATESINPERIOD

Hi Everyone,

 

    I create two measures:

    1. SUM_QTY:

        (1) Goal: To sum all QTY in the data source

        (2) DAX: SUM(Sheet1[QTY])

 

    2. SUM_QTY_Last12M: 

        (1) Goal: To sum the QTY in the Last 12 Months

        (2) DAX: CALCULATE([SUM_QTY], DATESINPERIOD('Dim_MTH'[MTH], DATE(YEAR(NOW()), MONTH(NOW()), 01), -1, YEAR))

 

    The first one is working well, but the last one is weird.

    Why SUM_QTY_Last12M always shows 431? Although 431 is the correct anwser...

MonthSUM_QTYSUM_QTY_Last12MWhat I Expect
2021-01-0198431 
2021-02-014431 
2021-03-013343133
2021-04-016943169
2021-05-018143181
2021-06-013343133
2021-07-0164316
2021-08-015143151
2021-09-013343133
2021-10-0194319
2021-11-012143121
2021-12-013343133
2022-01-0164316
2022-02-015143151
2022-03-013843138
Total566431431

 

 

 

  • Hi Vanchy_Liao 

     

    Try this:

     

    SUM_QTY_Last12 = 
    VAR _A =
        CALCULATE(
            [SUM_QTY],
            DATESINPERIOD( Sheet1[Month], DATE( 2022, 3, 1 ), -12, MONTH )
        )
    VAR _B =
        CALCULATE(
            [SUM_QTY],
            FILTER(
                Sheet1,
                Sheet1[Month]
                    IN DATESINPERIOD( Sheet1[Month], DATE( 2022, 3, 1 ), -12, MONTH )
            )
        )
    RETURN
        IF( ISFILTERED( Sheet1[Month] ), _B, _A )
    

     

    Output:

     

     

     

     

     

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
    Appreciate your Kudos!!
    LinkedIn: 
    www.linkedin.com/in/vahid-dm/

     

     

2 Replies

  • Hi Vanchy_Liao 

     

    Try this:

     

    SUM_QTY_Last12 = 
    VAR _A =
        CALCULATE(
            [SUM_QTY],
            DATESINPERIOD( Sheet1[Month], DATE( 2022, 3, 1 ), -12, MONTH )
        )
    VAR _B =
        CALCULATE(
            [SUM_QTY],
            FILTER(
                Sheet1,
                Sheet1[Month]
                    IN DATESINPERIOD( Sheet1[Month], DATE( 2022, 3, 1 ), -12, MONTH )
            )
        )
    RETURN
        IF( ISFILTERED( Sheet1[Month] ), _B, _A )
    

     

    Output:

     

     

     

     

     

    If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
    Appreciate your Kudos!!
    LinkedIn: 
    www.linkedin.com/in/vahid-dm/

     

     

  • Vanchy_Liao , to me last column seem same as the first one

     

    for rolling

    Rolling 12 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-12,MONTH))

     

    You can change no of months as per need

    Abstract Thesis Part 60: Rolling Months Formula: https://youtu.be/GS5O4G81fww