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
shds
Advocate I
Advocate I

How to create last twelve months - calculated column/measure based on selected year and month slicer

 I have a report which contains a year slicer and a month slicer so you can select a specific scope for the related charts. Some charts will only look at the selected month in the selected year, but I need other charts to take the selected year and month and specifically look for the twelve months up to the selected year+month.

 

So for example if you select:

  • Year: 2016
  • Month: 10

Then

  • Chart 1: displays sales data with dates in october 2016 (this is easy and this works)
  • Chart 2: should display similar sales data, but within the date range of October 2015 through October 2016.

I can't get Chart 2 to function propertly. The DAX query I'm working with seems to get close, but somehow doesn't do what I require:

 

Sales12M = CALCULATE (
    SUM('data'[Sales]) / 1000;
    DATESBETWEEN (
        'Date'[YearMonthnumber];
        SAMEPERIODLASTYEAR ( NEXTDAY ( LASTDATE ( 'YearMonths'[YearMonthnumber] ) ) );
        LASTDATE ( 'YearMonths'[YearMonthnumber] )
    )
)

I have tried fiddling around with a separate Date dates by day table and another YearMonths table that only shows 2016-01-01, 2016-02-01, etc.

 

What am I missing here?

 

Thanks in advance!

 

 

 

3 REPLIES 3
EAfang
Frequent Visitor

In your DAX formular, you iterated by MONTH with

DATESBETWEEN (
        'Date'[YearMonthnumber];....

 And then used the NEXTDAY() function which pushes by a day. Try using the DAY iteration like this

Sales12M = CALCULATE (
    SUM('data'[Sales]) / 1000;
    DATESBETWEEN (
        'Date'[Date];
        SAMEPERIODLASTYEAR ( NEXTDAY ( LASTDATE ( 'YearMonths'[Date] ) ) );
        LASTDATE ( 'YearMonths'[Date] )
    )
)

I was wondering if you got this to work. I am having the same issue.  I am about to just give up 😞

Vvelarde
Community Champion
Community Champion

@shds

 

Using 2 slicer: Year and Month:

 

Use this Dax Code

 

12M =
CALCULATE (
    Sum(Sales[Value]),
    DATESBETWEEN (
        'Date'[Date],
        DATEADD ( FIRSTDATE ( 'Date'[Date] ), -1; YEAR ),
        LASTDATE ( 'Date'[Date] )
    )
)

 

 




Lima - Peru

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.