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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Sylvine_Wyz
Helper IV
Helper IV

DAX YTD N-1 EMONTH

Hello everybody, 

 

I want to know the total of quantity about the last year until the last month.
Ex : if we are the 15th april 2021, i want the total of quantity from 01/01/2020 to 31/03/2020
If we are the 1st may 2021, i want the total of the quantity from 01/01/2020 to 30/04/2020.

Currently, i use this formul ant it s correct : 

formul =
var _max = eomonth(date(year(today())-1,month(today()),day(today())),-1)
return
CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),DATESYTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)
//CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)
 
But if in my report, i choose the segment "February", i want to appair the quantity of february only. Currently it's the sum of january and february. 
 
Sylvine_Wyz_0-1619776499577.png

 

Thanks in advance for your help

 

Have a nice day

 
1 ACCEPTED SOLUTION
v-luwang-msft
Community Support
Community Support

Hi @Sylvine_Wyz ,

If slicer is date,you could try the following measure:

formul =
VAR _max =
    EOMONTH (
        DATE ( YEAR ( TODAY () ) - 1, MONTH ( SELECTEDVALUE ( 'date'[Date] ) ), DAY ( SELECTEDVALUE ( 'date'[Date] ) ) ),
        -1
    )
VAR _min =
    DATE ( "2020", "1", "1" )
VAR TEST =
    CALCULATE (
        SUM ( 'wyz_reporting bi_order'[quantity_order_line] ),
        FILTER (
            'wyz_reporting bi_order',
            'wyz_reporting bi_order'[Date] >= _min
                && 'wyz_reporting bi_order'[Date] <= _max
        )
    )
RETURN
    TEST
//CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)))))))))))

 

If slicer is month,you could try the following measure:

formul2 =
VAR YEAR =
    SELECTEDVALUE ( 'date'[Year] )
VAR mon =
    SELECTEDVALUE ( 'date'[mon] )
VAR _max =
    EOMONTH ( DATE ( YEAR - 1, mon, "01" ), -1 )
VAR _min =
    DATE ( "2020", "1", "1" )
VAR TEST =
    CALCULATE (
        SUM ( 'wyz_reporting bi_order'[quantity_order_line] ),
        FILTER (
            'wyz_reporting bi_order',
            'wyz_reporting bi_order'[Date] >= _min
                && 'wyz_reporting bi_order'[Date] <= _max
        )
    )
RETURN
    TEST

 

base data:

v-luwang-msft_0-1620287415186.png

And final result:

v-luwang-msft_1-1620287444547.png

You could download my pbix file if you need!

Wish it is helpful for you!

 

Best Regards

Lucien

View solution in original post

3 REPLIES 3
v-luwang-msft
Community Support
Community Support

Hi @Sylvine_Wyz ,

If slicer is date,you could try the following measure:

formul =
VAR _max =
    EOMONTH (
        DATE ( YEAR ( TODAY () ) - 1, MONTH ( SELECTEDVALUE ( 'date'[Date] ) ), DAY ( SELECTEDVALUE ( 'date'[Date] ) ) ),
        -1
    )
VAR _min =
    DATE ( "2020", "1", "1" )
VAR TEST =
    CALCULATE (
        SUM ( 'wyz_reporting bi_order'[quantity_order_line] ),
        FILTER (
            'wyz_reporting bi_order',
            'wyz_reporting bi_order'[Date] >= _min
                && 'wyz_reporting bi_order'[Date] <= _max
        )
    )
RETURN
    TEST
//CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)))))))))))

 

If slicer is month,you could try the following measure:

formul2 =
VAR YEAR =
    SELECTEDVALUE ( 'date'[Year] )
VAR mon =
    SELECTEDVALUE ( 'date'[mon] )
VAR _max =
    EOMONTH ( DATE ( YEAR - 1, mon, "01" ), -1 )
VAR _min =
    DATE ( "2020", "1", "1" )
VAR TEST =
    CALCULATE (
        SUM ( 'wyz_reporting bi_order'[quantity_order_line] ),
        FILTER (
            'wyz_reporting bi_order',
            'wyz_reporting bi_order'[Date] >= _min
                && 'wyz_reporting bi_order'[Date] <= _max
        )
    )
RETURN
    TEST

 

base data:

v-luwang-msft_0-1620287415186.png

And final result:

v-luwang-msft_1-1620287444547.png

You could download my pbix file if you need!

Wish it is helpful for you!

 

Best Regards

Lucien

Sylvine_Wyz
Helper IV
Helper IV

@amitchandak Hello Thanks for your return.
I tried the mesure.
If a month is seleted it's correct but if no month is selected, the quantity is the total of 2020 and not until 31/03/2020

amitchandak
Super User
Super User

@Sylvine_Wyz , You can try measure M1 and M3

 

M1 = CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),dateadd('Date'[Date],-1,year),'Date'[Date]<=_max)

 

M2= CALCULATE(Sum('wyz_reporting bi_order'[quantity_order_line]),DATESYTD(dateadd('Date'[Date],-1,year)),'Date'[Date]<=_max)

 

M3 = if(isfiltered(Date[Month]) , [M1], [M2])

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.