ROLLINGKTHLARGESTUNIQUE Function

Computes the rolling unique kth largest value forward or backward of the current row.

For purposes of this calculation, two instances of the same value are treated at one value for k. So, if your dataset contains four rows with column values 10, 9, 9, and 8, then KTHLARGESTUNIQUE returns 9 for k=2 and 8 for k=3.

ROLLINGKTHLARGESTUNIQUE computes the KTHLARGESTUNIQUE value across a defined window of values within a column.

  • If an input value is missing or null, it is not factored in the computation. For example, for the first row in the dataset, the rolling calculation of previous values is undefined.
  • The row from which to extract a value is determined by the order in which the rows are organized based on the order parameter.

    If you are working on a randomly generated sample of your dataset, the values that you see for this function might not correspond to the values that are generated on the full dataset during job execution.

  • Inputs:
    • Required column name
    • Required kth value, which is a positive integer
    • Two optional integer parameters that determine the window backward and forward of the current row. The default integer parameter values are -1 and 0, which computes the rolling function from the current row back to the first row of the dataset.
  • This function works with the following transforms:

For more information on a non-rolling version of this function, see KTHLARGESTUNIQUE Function.

Basic Usage

Column example:

derive type:single value:ROLLINGKTHLARGESTUNIQUE(myCol, 2)

Output: Generates a new column containing the rolling second largest unique value in the myCol column from the first row of the dataset to the current one.

Rows before example:

window value:ROLLINGKTHLARGESTUNIQUE(myNumber, 2, 3)

Output: Generates the new column, which contains the rolling second largest unique value of the current row and the two previous row values in the myNumber column.

Rows before and after example:

window value:value:ROLLINGKTHLARGESTUNIQUE(myNumber, 4, 3, 2)

Output: Generates the new column, which contains the rolling fourth largest unique value of the two previous row values, the current row value, and the two rows after the current one in the myNumber column.

Syntax

window value:value:ROLLINGKTHLARGESTUNIQUE(col_ref, rowsBefore_integer, rowsAfter_integer) order: order_col [group: group_col]

ArgumentRequired?Data TypeDescription
col_refYstringName of column whose values are applied to the function
k_integerYinteger (positive)The ranking of the unique value to extract from the source column
rowsBefore_integerNintegerNumber of rows before the current one to include in the computation
rowsAfter_integerNintegerNumber of rows after the current one to include in the computation

For more information on the order and group parameters, see Window Transform.

For more information on syntax standards, see Language Documentation Syntax Notes.

col_ref

Name of the column whose values are used to compute the function.

  • Multiple columns and wildcards are not supported.

Usage Notes:

Required?Data TypeExample Value
YesString (column reference to Integer or Decimal values)myColumn

k_integer

Integer representing the ranking of the unique value to extract from the source column. Duplicate values are treated as a single value for purposes of this function's calculation.

NOTE: The value for k must be an integer between 1 and 1,000 inclusive.

  • k=1 represents the maximum value in the column.
  • If k is greater than or equal to the number of values in the column, the minimum value is returned.
  • Missing and null values are not factored into the ranking of k.

Usage Notes:

Required? Data Type Example Value
YesInteger (positive)4

rowsBefore_integer, rowsAfter_integer

Integers representing the number of rows before or after the current one from which to compute the rolling function, including the current row. For example, if the first value is 5, the current row and the four rows after it are used in the computation. Negative values for k compute the rolling average from rows preceding the current one.

  • rowBefore=1 generates the current row value only.
  • rowBefore=-1 uses all rows preceding the current one.
  • If rowsAfter is not specified, then the value 0 is applied.
  • If a group parameter is applied, then these parameter values should be no more than the maximum number of rows in the groups.

Usage Notes:

Required?Data TypeExample Value
NoInteger4

Examples

Example - ROLLINGKTHLARGEST functions

This example describes how to use the following rolling computational functions:

  • ROLLINGKTHLARGEST - computes the kth largest value from a rolling window of rows before and after the current row. Duplicate values are treated as having the same k values. See ROLLINGKTHLARGEST Function.
  • ROLLINGKTHLARGESTUNIQUE - computes the unique kth largest value from a rolling window of rows before and after the current row. Duplicate values are treated as having different k values. See ROLLINGKTHLARGESTUNIQUE Function.

The following dataset contains daily counts of server restarts across three servers over the preceding week. High server restart counts can indicate poor server health. In this example, you are interested in knowing for each server the rolling highest and second highest count of restarts per server over the previous week.

Source:

DateServerRestarts
2/21/18s014
2/21/18s020
2/21/18s030
2/22/18s014
2/22/18s021
2/22/18s032
2/23/18s012
2/23/18s023
2/23/18s034
2/24/18s011
2/24/18s020
2/24/18s032
2/25/18s015
2/25/18s020
2/25/18s034
2/26/18s011
2/26/18s022
2/26/18s031
2/27/18s011
2/27/18s022
2/27/18s032


Transform:

First, you want to maintain the row information as a separate column. Since data is ordered already by the Date column, you can use the following:

derive type:single value:ROWNUMBER() as:'entryId'

Use the following function to compute the rolling kth largest value of server restarts per server over the previous week. In this case, you can use the ROLLINGKTHLARGEST function, setting k=1. Uniqueness doesn't matter for calculating the highest value:

derive type: multiple value: rollingkthlargest(Restarts, 1, 6, 0) group: Server order: Server as: 'rollingkthlargest_1'

Use the following function to compute the rolling second highest value. In this case, you can use ROLLINGKTHLARGESTUNIQUE:

derive type: multiple value: rollingkthlargestunique(Restarts, 2, 6, 0) group: Server order: Server as: 'rollingKthLargestUnique_2'


Results:

entryIdDateServerRestartsrollingKthLargestUnique_2rollingkthlargest_Restarts
32/21/18s02000
62/22/18s02101
92/23/18s02313
122/24/18s02013
152/25/18s02013
182/26/18s02223
212/27/18s02223
42/21/18s03000
72/22/18s03202
102/23/18s03424
132/24/18s03224
162/25/18s03424
192/26/18s03124
222/27/18s03224
22/21/18s01444
52/22/18s01444
82/23/18s01224
112/24/18s01124
142/25/18s01545
172/26/18s01145
202/27/18s01145


Was this page helpful? Let us know how we did:

Send feedback about...

Google Cloud Dataprep Documentation