Page tree

Release 7.6.2




Extracts the ranked value from the values in a column, where k=1 returns the maximum value, when a specified condition is met. The value for k must be between 1 and 1000, inclusive. Inputs can be Integer, Decimal, or Datetime.

KTHLARGESTIF calculations are filtered by a conditional applied to the group.  

For purposes of this calculation, two instances of the same value are treated as separate values. So, if your dataset contains three rows with column values 109, and 9, then KTHLARGEST returns 9 for k=2 and k=3.

Input column can be of Integer, Decimal, or Datetime type. Other values column are ignored. If a row contains a missing or null value, it is not factored into the calculation.

NOTE: When added to a transformation, this function is applied to the current sample. If you change your sample or run the job, the computed values for this function are updated. Transformations that change the number of rows in subsequent recipe steps do not affect the values computed for this step.

To perform a simple kth largest calculation without conditionals, use the KTHLARGEST function. See KTHLARGEST Function.

Wrangle vs. SQL: This function is part of Wrangle, a proprietary data transformation language. Wrangle is not SQL. For more information, see Wrangle Language.

Basic Usage

kthlargestif(POS_Sales, 1, DayOfWeek == 'Saturday')

Output: Returns the top value (rank=1) from the POS_Sales column when the DayOfWeek value is Saturday.

Syntax and Arguments

kthlargestif(col_ref, limit, test_expression) [group:group_col_ref] [limit:limit_count]

ArgumentRequired?Data TypeDescription
col_refYstringReference to the column you wish to evaluate.
k_integerYintegerThe ranking of the value to extract from the source column
test_expressionYstringExpression that is evaluated. Must resolve to true or false

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

For more information on the group and limit parameter, see Pivot Transform.


Name of the column whose values you wish to use in the calculation. Inputs must be Integer, Decimal, or Datetime values.

NOTE: If the input is in Datetime type, the output is in unixtime format. You can wrap these outputs in the DATEFORMAT function to output the results in the appropriate Datetime format. See DATEFORMAT Function.

Usage Notes:

Required?Data TypeExample Value
YesString that corresponds to the name of the columnmyValues


Integer representing the ranking of the value to extract from the source column.

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.


This parameter contains the expression to evaluate. This expression must resolve to a Boolean (true or false) value.

Usage Notes:

Required?Data TypeExample Value
YesString expression that evaluates to true or false(LastName == 'Mouse' && FirstName == 'Mickey')


Tip: For additional examples, see Common Tasks.

Example - Second-most measurements for a specific city

This example illustrates how to use the conditional ranking functions KTHLARGESTIF and KTHLARGESTUNIQUEIF in your recipes.


Here is some example weather data:

1/23/17Center Town0.319.45.3
1/23/17Magic Mountain0.000.07.3
1/24/17Center Town0.541.17.6
1/24/17Magic Mountain0.325.08.8
1/25/17Center Town0.833.35.1
1/25/17Magic Mountain0.59-1.76.4
1/26/17Center Town0.966.17.6
1/26/17Magic Mountain0.77-3.93.0
1/27/17Center Town1.3220.00.2
1/27/17Magic Mountain0.775.65.2
1/28/17Center Town0.145.04.9
1/28/17Magic Mountain1.501.10.4
1/29/17Center Town0.756.19.0
1/29/17Magic Mountain0.603.36.0


In this case, you want to find out the second-most measures for rain, temperature, and wind in Center Town for the week.

Transformation Name Pivot columns
Parameter: Values KTHLARGESTIF(rain_cm,2,city == 'Center Town')
Parameter: Max number of columns to create 1

You can see in the preview that the value is 1.32. Before adding it to your recipe, you change the step to the following:

Transformation Name Pivot columns
Parameter: Values KTHLARGESTIF(temp_C,2,city == 'Center Town')
Parameter: Max number of columns to create 1

The value is 20.

For wind, you modify it to be the following, capturing the third-ranked value:

Transformation Name Pivot columns
Parameter: Values KTHLARGESTIF(wind_mph,3,city == 'Center Town')
Parameter: Max number of columns to create 1

In the results, you notice that there are two values for 8.8. So you change the function to use the KTHLARGESTUNIQUEIF function instead:

Transformation Name Pivot columns
Parameter: Values KTHLARGESTUNIQUEIF(wind_mph,3,city == 'Center Town')
Parameter: Max number of columns to create 1

The result value is 7.6. Note that this value appears twice, so if you change the rank parameter in the above transformation to 4, the results would return a different unique ranked value (7.3). 


You can choose to add any of these steps to generate an aggregated result. As an alternative, you can use a derive transform to insert these calculated results into new columns.


This page has no comments.