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
Anonymous
Not applicable

Measure to start from a specific date

Hi Expert

 

I am trying re create the following Sumproduct formula as shown in the table image below in Power BI. I want the measure to Shart at Feb-20 and always get the Sumproduct of the previous 6 periods.

 

table.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

See atatched smaple PBIX

https://www.dropbox.com/s/ii28rhnl5gqawej/Test.pbix?dl=0 

1 ACCEPTED SOLUTION

So going back to 20-25 is harder because your months are not continuous so you need to calculate the previous month then use that when filtering the months for the topn 

 

Measure Back 1 =
var _currPeriod = max('Sample'[MonthKey])
var _prevPeriod = CALCULATE(max('Sample'[MonthKey]), 'Sample'[MonthKey] < _currPeriod)
var _last6 = topn(6, Filter(ALL('Sample'[MonthKey]), 'Sample'[MonthKey] < _prevPeriod), 'Sample'[MonthKey])
var _last6Cnt = COUNTROWS(_last6)
var _numerator = CALCULATE(SUMX( 'Sample', 'Sample'[Amount] * 'Sample'[Rate]), _last6, all('Sample'))
var _denominator = CALCULATE(sum('Sample'[Amount]), _last6, all('Sample'))
return if(_last6Cnt >= 6, DIVIDE( _numerator , _denominator))

 

Including the current month is easier as you just change the filter condition from < to <= 
 
Measure Forward 1 =
var _currPeriod = max('Sample'[MonthKey])
var _last6 = topn(6, Filter(ALL('Sample'[MonthKey]), 'Sample'[MonthKey] <= _currPeriod), 'Sample'[MonthKey])
var _last6Cnt = COUNTROWS(_last6)
var _numerator = CALCULATE(SUMX( 'Sample', 'Sample'[Amount] * 'Sample'[Rate]), _last6, all('Sample'))
var _denominator = CALCULATE(sum('Sample'[Amount]), _last6, all('Sample'))
return if(_last6Cnt >= 6, DIVIDE( _numerator , _denominator))

View solution in original post

7 REPLIES 7
d_gosbell
Super User
Super User

You cannot do what you want with your sample data because you only have a string version of the month name. So all the Aprils sort first then the Augusts, etc.

 

So I created the following calculated column so that the months can be sorted correctly

 

MonthKey = DATEVALUE("1-" & [Month])
 
Then you can create a measure like the following
 
Measure =
var _currPeriod = max('Sample'[MonthKey])
var _last6 = topn(6, Filter(ALL('Sample'[MonthKey]), 'Sample'[MonthKey] < _currPeriod), 'Sample'[MonthKey])
var _last6Cnt = COUNTROWS(_last6)
var _numerator = CALCULATE(SUMX( 'Sample', 'Sample'[Amount] * 'Sample'[Rate]), _last6, all('Sample'))
var _denominator = CALCULATE(sum('Sample'[Amount]), _last6, all('Sample'))
return if(_last6Cnt >= 6, DIVIDE( _numerator , _denominator))
 
Anonymous
Not applicable

Hi - d_gosbell - Firstly thanks excellent feedback. Just one slight issues the - the final result is off by one i.e i need to move the end result up by one.....i.e. shift up one row..... 


@Anonymous wrote:

Hi - d_gosbell - Firstly thanks excellent feedback. Just one slight issues the - the final result is off by one i.e i need to move the end result up by one.....i.e. shift up one row..... 


So I matched your expected output exactly Mar-20 is 1.26 in both and every month after that is the same also:

d_gosbell_0-1625651487622.png

 

Are you saying you want something different to the screen shot you provided for the expected output? Does that mean that the formula at row 27 should be summing rows 20 to 25? Or do you want to include the current period, so row 27 should actually be summing rows 22 to 27?

Anonymous
Not applicable

Hi d_gosbell.....Lets assume both the options you have given
Does that mean that the formula at row 27 should be summing rows 20 to 25? Or do you want to include the current period, so row 27 should actually be summing rows 22 to 27?

 

For me to learn how would i amend the measure in both cases...

So going back to 20-25 is harder because your months are not continuous so you need to calculate the previous month then use that when filtering the months for the topn 

 

Measure Back 1 =
var _currPeriod = max('Sample'[MonthKey])
var _prevPeriod = CALCULATE(max('Sample'[MonthKey]), 'Sample'[MonthKey] < _currPeriod)
var _last6 = topn(6, Filter(ALL('Sample'[MonthKey]), 'Sample'[MonthKey] < _prevPeriod), 'Sample'[MonthKey])
var _last6Cnt = COUNTROWS(_last6)
var _numerator = CALCULATE(SUMX( 'Sample', 'Sample'[Amount] * 'Sample'[Rate]), _last6, all('Sample'))
var _denominator = CALCULATE(sum('Sample'[Amount]), _last6, all('Sample'))
return if(_last6Cnt >= 6, DIVIDE( _numerator , _denominator))

 

Including the current month is easier as you just change the filter condition from < to <= 
 
Measure Forward 1 =
var _currPeriod = max('Sample'[MonthKey])
var _last6 = topn(6, Filter(ALL('Sample'[MonthKey]), 'Sample'[MonthKey] <= _currPeriod), 'Sample'[MonthKey])
var _last6Cnt = COUNTROWS(_last6)
var _numerator = CALCULATE(SUMX( 'Sample', 'Sample'[Amount] * 'Sample'[Rate]), _last6, all('Sample'))
var _denominator = CALCULATE(sum('Sample'[Amount]), _last6, all('Sample'))
return if(_last6Cnt >= 6, DIVIDE( _numerator , _denominator))
Anonymous
Not applicable

Anonymous
Not applicable

Excellent sir thank you.. God Bless amazing feedback - Option 2 worked

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.