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
priyanath1988
Helper III
Helper III

Last 12 months showing same value

Hi, I am trying to calculate last 12 months of data based on slicer "last day of month" . the code I used is below 

Last 12 months v4 =
VAR MaxDate = MAX( 'Calendar'[Calendar_last_day_of_month] )     -- retrieve latest date
VAR MinDate = EDATE( MaxDate, -12 )   -- move it back 12 months
VAR Result =
     CALCULATE(
          'Actuals_by_Month'[Actuals_Volume],
          FILTER( ALL( 'Calendar' ),      -- return period between
          'Calendar'[Calendar_last_day_of_month] <= MaxDate &&  -- latest date
          'Calendar'[Calendar_last_day_of_month] > MinDate )    -- bigger than year before
     )
RETURN
     Result

  why it just show value for the selected month? why it is not rolling back 12 months?

1 ACCEPTED SOLUTION

@Mikelytics , I was able to make this work but by different method - "USERELATIONSHIP". Here is the code: 

_previous_amount =
var ReferenceDate = MAX('Calendar'[Calendar_last_day_of_month])
VAR PreviousDates =
DATESINPERIOD('Previous Date'[Calendar_last_day_of_month],ReferenceDate, -13, MONTH)
var result =
CALCULATE(sum(ACTUALS_BY_MONTH[AMOUNT]),
REMOVEFILTERS('Calendar'),
KEEPFILTERS(PreviousDates) ,USERELATIONSHIP('Calendar'[Date], 'Previous Date'[Date]))
RETURN
result

*************

Previous Date ='Calendar'

 

Results:

priyanath1988_1-1673386177575.png

Here is Youtube reference that I followed 

https://www.youtube.com/watch?v=d8Rm7dwM6gc 

Thanks for all of your suggestions and time!

View solution in original post

17 REPLIES 17
Mikelytics
Resident Rockstar
Resident Rockstar

@priyanath1988 

Weird. I think I now set it up exactly like you. I added a LastDayOfMonthColum in my data table and adjsuted the formula by refering to the new column and it works

 

Date Table

Mikelytics_2-1673365240931.png

 

Mikelytics_1-1673365232146.png

Formula

 

Last 12 months v4 = 
VAR MaxDate = MAX( Dim_Date[LastDayOfMonth] )     -- retrieve latest date
VAR MinDate = EDATE( MaxDate, -12 )   -- move it back 12 months
VAR Result =
     CALCULATE(
          [Aggregation | Sum],
          FILTER( ALL( Dim_Date ),      -- return period between
          Dim_Date[LastDayOfMonth]  <= MaxDate &&  -- latest date
          Dim_Date[LastDayOfMonth] > MinDate )    -- bigger than year before
     )
RETURN
     Result

 

Result

Mikelytics_0-1673365198593.png

 

What is about your data model. Which Columns are connected between dimension and fact? What is the relation type and what the filter direction? Can you show a screenshpt please?

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

my data model is pretty simple but the fact table is joined with few dimensions wiht one- to- many. however the re;lationship between calendar and fact table is defined by a relationship as screenshot

priyanath1988_0-1673366372239.png

 

Hi @priyanath1988 

 

it looks totally the same on my side. But there is one thing I observed. Please look on the screenshot what I highlighted

Mikelytics_0-1673366797362.png

is this a measure? or a column reference?

normally it should not give something back but is it possible that you need to ^replace it by 

 SUM('Actuals_by_Month'[Actuals_Volume]) ??

 

Can you pleae show the measure definition you have?

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics , Actuals_Volume is a measure defined as below

Actuals_Volume = COALESCE(sum(Actuals_by_Month[AMOUNT]),0)

Have never seen COALESCE 😄

 

Did you try taking it out on only use SUM?

sum(Actuals_by_Month[AMOUNT])

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics yeah, tried just the sum without COALESCE. No luck 😓

 @priyanath1988 

hmm

Is the field you put into the slicer from the calendar table or from the fact table?

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics  from Calendar.

@priyanath1988 

Ok, so slowly I reach the limit of ideas ^^

You mentioned you have other dimensions connected to the fact table. 

so question 1) is there maybe another filter which is active from another dimension which causes the issue?

questions 2) is there ,maybe by accident, antother relation from another table to the calendar table, maybe auto-connected by Power BI? Can you please show the overview of relations as wel as if any of this table have an active filter? if there is any try to turn them of in the report.

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics , ok let me try to remove stuffs and just keep the calendar and fact table. it might take 10 or 15 mins. I will get back to you . thanks so much!

@Mikelytics I created a new sample pbix project and kept just the fact table and calendar table. calendar(Date) joined to fact table(last day of the month). My visual looks like this

priyanath1988_0-1673373000694.png

I changed my measure like below

Last 12 months v4 =
VAR MaxDate = MAX( 'Calendar'[Calendar_last_day_of_month] ) -- retrieve latest date
VAR MinDate = EDATE( MaxDate, -12 ) -- move it back 12 months
VAR Result =
CALCULATE(
sum(Actuals_by_Month[AMOUNT]),0)
FILTER( ALL( 'Calendar' ), -- return period between
'Calendar'[Calendar_last_day_of_month] <= MaxDate && -- latest date
'Calendar'[Calendar_last_day_of_month] > MinDate ) -- bigger than year before
)
RETURN
Result

 

Not sure why it is still showing like this. just to confirm the "calendar last day of month" should be pulled from calendar for both canvas and slicer. correct?

 

@Mikelytics , tried ,, but no luck 😞 

This is so wird because it worked properly on my side as I showed ouy in the beginning. I really hate to say it but I have a lack of ideas left. @amitchandak Any idea? 😄

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics , I was able to make this work but by different method - "USERELATIONSHIP". Here is the code: 

_previous_amount =
var ReferenceDate = MAX('Calendar'[Calendar_last_day_of_month])
VAR PreviousDates =
DATESINPERIOD('Previous Date'[Calendar_last_day_of_month],ReferenceDate, -13, MONTH)
var result =
CALCULATE(sum(ACTUALS_BY_MONTH[AMOUNT]),
REMOVEFILTERS('Calendar'),
KEEPFILTERS(PreviousDates) ,USERELATIONSHIP('Calendar'[Date], 'Previous Date'[Date]))
RETURN
result

*************

Previous Date ='Calendar'

 

Results:

priyanath1988_1-1673386177575.png

Here is Youtube reference that I followed 

https://www.youtube.com/watch?v=d8Rm7dwM6gc 

Thanks for all of your suggestions and time!

Hi @priyanath1988 

 

so joining Calendar[Date] with 1:n relation to FactTable['[Calendar_last_day_of_month] is good.

 

Can you please try to refer to the calendar key column in the measure instead?

Last 12 months v4 =
VAR MaxDate = MAX( 'Calendar'[Date] ) -- retrieve latest date
VAR MinDate = EDATE( MaxDate, -12 ) -- move it back 12 months
VAR Result =
CALCULATE(
sum(Actuals_by_Month[AMOUNT]),0)
FILTER( ALL( 'Calendar' ), -- return period between
'Calendar'[Date] <= MaxDate && -- latest date
'Calendar'[Date] > MinDate ) -- bigger than year before
)
RETURN
Result

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!
Mikelytics
Resident Rockstar
Resident Rockstar

Hi @priyanath1988 

This is really weird. I tried out your measure and it worked on my side. Can please show your data model and how you set up the canvas? Especially how looks your calendar table like?

Mikelytics_0-1673342142566.png

Mikelytics_1-1673342161079.png

 

Best regards
Michael
-----------------------------------------------------
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Appreciate your thumbs up!
@ me in replies or I'll lose your thread.

 

 

 

 

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics I am generating Calendar like below 

Calendar = CALENDARAUTO()
please see the sreenshot of calendar table
priyanath1988_2-1673364472608.png

 


 

 and this is how canvas is been set up. "CY Actuals Through" is "calendar_last_day_of_month" from 'Calendar'.
 priyanath1988_1-1673364313294.png

 

Please let me know if you need any other information.

thanks!

 

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.