Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Vanchy_Liao
Advocate II
Advocate II

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

 

 

 

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

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:

 

VahidDM_0-1648093765744.png

 

 

 

 

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/

 

 

View solution in original post

2 REPLIES 2
VahidDM
Super User
Super User

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:

 

VahidDM_0-1648093765744.png

 

 

 

 

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/

 

 

amitchandak
Super User
Super User

@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

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.