Can the function "TOPN" be used as a filter?

atkach123

New Member
Joined
Mar 23, 2016
Messages
8
Hi,

I'm currently trying to use TOPN as a filter context in a formula. Essentially, without going into too much detail, I'm trying to get a driver's (worker's etc.) overtime rate to be applied to his hours for the week. The driver's overtime rate for the week should be the rate on the last day of the week. In the simplified example below, that rate should be $16.

However, with a full and much larger data set, my thoughts to pull that overtime rate were something along the lines of: =CALCULATE([Overtime rate], filter by Driver, filter by week, TOPN(1,table, [Date])). With the TOPN function, I'm trying to get the formula to filter for the maximum (i.e. last) date of that week so that it can pull in that respective overtime wage.


Date Driver Overtime rate
8/31/2015 A 14
9/1/2015 A 14
9/2/2015 A 14.5
9/3/2015 A 14.5
9/4/2015 A 15
9/5/2015 A 16


Thanks!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Date Driver Overtime rate
8/31/2015 A 14
9/1/2015 A 14
9/2/2015 A 14.5
9/3/2015 A 14.5
9/4/2015 A 15
9/5/2015 A 16
 
Upvote 0
As far as I know, CALCULATE() and TOPN() are not native functions?

This will give you the rate on Saturday - the last date in your sample...
[Table="width:, class:grid"][tr][td] [/td][td]
F​
[/td][td]
G​
[/td][td]
H​
[/td][td]
I​
[/td][/tr]
[tr][td]
1​
[/td][td]Date[/td][td]Driver[/td][td]OT Rate[/td][td][/td][/tr]

[tr][td]
2​
[/td][td]8/31/2015[/td][td]A[/td][td]
14​
[/td][td][/td][/tr]

[tr][td]
3​
[/td][td]9/1/2015[/td][td]A[/td][td]
14​
[/td][td][/td][/tr]

[tr][td]
4​
[/td][td]9/2/2015[/td][td]A[/td][td]
14.5​
[/td][td][/td][/tr]

[tr][td]
5​
[/td][td]9/3/2015[/td][td]A[/td][td]
14.5​
[/td][td][/td][/tr]

[tr][td]
6​
[/td][td]9/4/2015[/td][td]A[/td][td]
15​
[/td][td][/td][/tr]

[tr][td]
7​
[/td][td]9/5/2015[/td][td]A[/td][td]
16​
[/td][td]
16​
[/td][/tr]
[/table]

I2=IF(WEEKDAY(F2,2)=6,H2,"")
copied down
 
Upvote 0
Hey guys,

Thanks for the response. Yes, sorry, should have mentioned this will be done in power pivot. The example above is just a simplified version of the huge amount of data that I have.


Thanks!
 
Upvote 0
Hi there,

Yes, since TOPN returns a table, it can be used to generate a 'maximum' filter argument for CALCULATE.
There are other options: the most concise (for a maximum date) would be LASTDATE, and more generally you can use LASTNONBLANK.

So the options would be:
Code:
=CALCULATE( [B][Your measure][/B]<your measure="">,..., LASTDATE( table[Date] ) )
=CALCULATE( <your measure="">[B][Your measure][/B],..., TOPN( 1, table table[Date] ) )
=CALCULATE( <your measure="">[B][Your measure][/B],..., LASTNONBLANK( VALUES( table[Date] ) ,1 ) )

How you handle the Driver/Week filters would depend on how you are using this measure in your model.
Also, if you need this measure to work when the filter context is on a date other than the last day of the week you would need to handle that e.g. by clearing the Date filter but retaining the Week filter.

Ozeroth :)</your></your></your>
 
Last edited:
Upvote 0
LASTDATE embedded in CALCULATE worked like a charm - thanks Ozeroth and everyone else who responded!


Cheers,

Adam
 
Upvote 0

Forum statistics

Threads
1,224,157
Messages
6,176,744
Members
452,741
Latest member
Muhammad Nasir Mahmood

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top