Tutorial | Top N recipe#

Try out the Top N recipe to identify the rows containing the largest or smallest values of a particular metric.

Get started#

Objectives#

In this tutorial, you will:

  • Extract the top and bottom N rows of a dataset according to a sorted metric.

  • Calculate ranks based on this metric.

  • Compute the same for subsets of the dataset.

Prerequisites#

To complete this tutorial, you’ll need the following:

  • Dataiku 14.0 or later.

Create the project#

  1. From the Dataiku Design homepage, click + New Project.

  2. Select Learning projects.

  3. Search for and select Top N Recipe.

  4. If needed, change the folder into which the project will be installed, and click Create.

  5. From the project homepage, click Go to Flow (or type g + f).

Note

You can also download the starter project from this website and import it as a ZIP file.

You’ll next want to build the Flow.

  1. Click Flow Actions at the bottom right of the Flow.

  2. Click Build all.

  3. Keep the default settings and click Build.

Use case summary#

Let’s say we’re a financial company that uses some credit card data to detect fraudulent transactions.

The project comes with three datasets, described in the table below.

Dataset

Description

tx

Each row is a unique credit card transaction with information such as the card that was used and the merchant where the transaction was made.

It also indicates whether the transaction has either been:

  • Authorized (a score of 1 in the authorized_flag column)

  • Flagged for potential fraud (a score of 0)

merchants

Each row is a unique merchant with information such as the merchant’s location and category.

cards

Each row is a unique credit card ID with information such as the card’s activation month or the cardholder’s FICO score (a common measure of creditworthiness in the US).

Create the Top N recipe#

You have a prepared dataset of transactions, but the next phase of analysis calls for a leaderboard. You’ll need to find the largest transactions.

Start by creating a Top N recipe from the tx_prepared dataset.

  1. From the Flow, select the tx_prepared dataset.

  2. In the Actions tab of the right panel, select the Top N recipe from the visual recipes section.

  3. Name the output dataset tx_topn.

  4. Click Create Recipe.

Dataiku screenshot of the new Top N recipe window.

Retrieve the top N rows#

Filter the whole dataset for the rows representing the five largest purchases.

  1. Retrieve the 5 top rows and 0 bottom rows.

  2. Select the purchase_amount column for sorting.

  3. Change the sort order to descending (Sort descending icon.) so the most expensive orders appear at the top of the dataset.

  4. Run the recipe, and open the output dataset.

    A screenshot of the Top N step in the Top N recipe.
  5. Verify the output has only five records in descending order in terms of purchase_amount.

Rank and filter by groups#

The Top N recipe also supports grouping. This can be convenient instead of turning to other possible tools like the Group, Window, and Sort/Filter recipes.

Modify the previous example to find the five largest purchases per item category.

  1. Click Parent Recipe to reopen the Top N recipe.

  2. Under from, select each group of rows identified by.

  3. Choose item_category as the key column.

  4. Under In addition, compute for each row, check the row number within its group checkbox.

A screenshot of the Top N step showing how to group top purchases by item category.

Retrieve a subset of columns#

The Top N recipe also allows you to select which columns to retain in the output schema.

The leaderboard would look cleaner with only columns pertaining to the analysis. You can discard the others:

  1. Navigate to the Retrieve columns step.

  2. Change the Mode to Select columns.

  3. Move all columns into the Available columns section using the double left chevron (Double left chevron icon.).

  4. Move item_category and purchase_amount back to the Selected columns section using the right chevron (Right chevron icon.).

  5. Run the recipe, and open the output dataset.

Screenshot of the output dataset showing top purchases grouped by item category.

You’ll see that for each category — A, B, C, and D — there are five purchase amounts that decrease within their grouping. You can confirm from the _row_number values that each grouping has five values.

Next steps#

You just practiced using the Top N recipe to find the most expensive transactions in the dataset.

See also

For more information on this recipe, see Top N: retrieve first N rows in the reference documentation.

Continue to the Tutorial | Pivot recipe to learn more about how you can create pivot tables using another visual recipe.