In Contour, how do I filter down to the top N by values in a group?

1.3k Views Asked by At

In an analysis where each row contains e.g. a flight number, I'd like to filter down my active dataset to keep only the three most-delayed flight numbers for each carrier.

How do I accomplish this in Contour?

1

There are 1 best solutions below

0
On

This is a two-step process: (1) Group and order, and (2) filter.

Group and Order: Add an Expression board, and make a new column by assigning a row number for each record. Reset the row number within each carrier group, and ensure that row numbers are assigned in order of flight delay duration.

row_number() OVER ( PARTITION BY "carrier" ORDER BY "delay_duration" DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) 

Filter: Add a Filter board and select those row numbers created above that are less than or equal to 3 to keep only the three most delayed flights for each carrier.