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
Dan_Wis
Frequent Visitor

sum by rolling 12 months period

Hi All,

 

I have a problem with calculating number od days spent abroad by particular employee within 12m rolling period.

this is what I have:

 

Table 1

 

Employee IDTo Country#DaysFrom CountryEOM_TravelDateKey_2
6154SWITZERLAND5ITALY30/11/20196154-ITALY-SWITZERLAND
6154SWITZERLAND4ITALY31/12/20196154-ITALY-SWITZERLAND
6154NORWAY4ITALY29/02/20206154-ITALY-NORWAY
6154SPAIN4ITALY31/12/20206154-ITALY-SPAIN
6154SWITZERLAND7ITALY31/01/20206154-ITALY-SWITZERLAND

 

I want to show in Table 2 number of days spent in particular country within 12m rolling period:

 

Table 2 = DISTINCT('Table 1'[Key_2])

 

Key_2#Days-12m back
6154-ITALY-SWITZERLAND 
6154-ITALY-NORWAY 
6154-ITALY-SPAIN 

 

#Days-12m back = [Rolling 12 months_Measure]

 

 

Rolling 12 months_Measure =
var CurrentDate = MAX('Table 1'[EOM_TravelDate])
var PreviousDate = DATE(YEAR(CurrentDate),MONTH(CurrentDate)-12,DAY(CurrentDate))
var Result =
CALCULATE(
SUM('Table 1'[#Days]),
FILTER(
'Table 1',
'Table 1'[EOM_TravelDate] > PreviousDate && 'Table 1'[EOM_TravelDate] <= CurrentDate
)
)
return
Result

 

 

Based on that criteria the result it gives me is 16 for 6154-ITALY-SWITZERLAND in Table 2, whereas it should be 7

 

Any ideas what is wrong?

 

Your support would be much appreciated!

 

thanks,

Best regards

3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Don't @Dan_Wis,

Here are the steps you can follow:

1. Create a calculated column.

Rolling 12 months_measure =
var _CurrentDate =CALCULATE(MAX('Table'[EOM_TravelDate]),FILTER('Table','Table'[Key_2]=MAX('Table 2'[Key_2])))
return
CALCULATE(MAX('Table'[#Days]),FILTER('Table','Table'[EOM_TravelDate]=_CurrentDate))

2. Result:

2021.1.26.png

You can download the PBIX file from here.

Best regards

Liu Yang

If this post helps,then consider Accepting it as the solution to help other members find it faster.

amitchandak
Super User
Super User

@Dan_Wis , add the key in filter

 

Rolling 12 months_Measure =
var CurrentDate = MAX('Table 1'[EOM_TravelDate])
var PreviousDate = DATE(YEAR(CurrentDate),MONTH(CurrentDate)-12,DAY(CurrentDate))
var Result =
CALCULATE(
SUM('Table 1'[#Days]),
FILTER(
'Table 1',
'Table 1'[EOM_TravelDate] > PreviousDate && 'Table 1'[EOM_TravelDate] <= CurrentDate
&&  'Table 1'[Key_2] = 'Table 2'[Key_2]
)
)
return
Result

Hi @amitchandak ,

Thanks for support!

Adding the key in filter does not work as I got still 16 instead of 7.

It does work when I input manually PreviousDate: DATE(2019,12,31), however it doesn't make it dynamic for the future reports..

Any other suggestion?

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.