Returns the row number of the current row as it appeared in the original, source dataset before any steps had been applied.The following transforms might make original row information invalid or otherwise unavailable:
NOTE: If the dataset is sourced from multiple files, a predictable original source row number cannot be guaranteed. This function may return unexpected results.
Tip: If the source row information is still available, you can hover over the left side of a row in the data grid to see the source row number in the original source data.
NOTE: When working with datasets sourced from Avro files, lineage information and the SOURCEROWNUMBER function are not supported.
derive value:SOURCEROWNUMBER() as:'OriginalRowNums'
Output: Generates a new OriginalRowNums column containing the row numbers for each row as it appeared in the original data.Delete Example:
delete row:SOURCEROWNUMBER() > 101
Output: Deletes the rows in the dataset that were after row #101 in the original source data.
There are no arguments for this function.
Example - Header from row that is not the first one
You have imported the following racer data on heat times from a CSV file. When loaded in the Transformer page, it looks like the following:
|1||Racer||Heat 1||Heat 2||Heat 3|
In the above, the
(rowId) column references the row numbers displayed in the data grid; it is not part of the dataset. This information is available when you hover over the black dot on the left side of the screen.
You have examined the heat time columns to determine the best performance in each heat according to the sample. You then notice that the data contains headers, but you forget how it was originally sorted. The data now looks like the following:
|2||Racer||Heat 1||Heat 2||Heat 3|
While you can undo your sort steps to return to the original sort order, this approach works best if you did not include other steps in between that are based on the sort order.
After you have applied the last
header transform, your data should look like the following:
Example - Using sourcerownumber to create unique row identifiers
The following example demonstrates how to unpack nested data. As part of this example, the
SOURCEROWNUMBER function is used as part of a method to create unique row identifiers.
You have the following data on student test scores. Scores on individual scores are stored in the
Scores array, and you need to be able to track each test on a uniquely identifiable row. This example has two goals:
- One row for each student test
- Unique identifier for each student-score combination
When the data is imported from CSV format, you must add a
header transform and remove the quotes from the
Validate test date: To begin, you might want to check to see if you have the proper number of test scores for each student. You can use the following transform to calculate the difference between the expected number of elements in the
replace col:Scores with:'' on:`"` global:true
Scoresarray (4) and the actual number:
When the transform is previewed, you can see in the sample dataset that all tests are included. You might or might not want to include this column in the final dataset, as you might identify missing tests when the recipe is run at scale.
derive value: (4 - ARRAYLEN(Scores)) as: 'numMissingTests'
Unique row identifier: The
Scores array must be broken out into individual rows for each test. However, there is no unique identifier for the row to track individual tests. In theory, you could use the combination of
LastName-FirstName-Scores values to do so, but if a student recorded the same score twice, your dataset has duplicate rows. In the following transform, you create a parallel array called
Tests, which contains an index array for the number of values in the
Scores column. Index values start at
Also, we will want to create an identifier for the source row using the
derive value:RANGE(0,ARRAYLEN(Scores)) as:'Tests'
One row for each student test: Your data should look like the following:
derive value:SOURCEROWNUMBER() as:'orderIndex'
Now, you want to bring together the
Scores arrays into a single nested array using the
Your dataset has been changed:
flatten transform, you can unpack the nested array:
Each test-score combination is now broken out into a separate row. The nested Test-Score combinations must be broken out into separate columns using
flatten col: column1
After you drop
unnest col:column1 keys:'',''
column1, which is no longer needed you should rename the two generated columns:
Unique row identifier: You can do one more step to create unique test identifiers, which identify the specific test for each student. The following uses the original row identifier
OrderIndexas an identifier for the student and the
TestNumbervalue to create the
The above are integer values. To make your identifiers look prettier, you might add the following:
derive value: (orderIndex * 10) + TestNum as: 'TestId'
Extending: You might want to generate some summary statistical information on this dataset. For example, you might be interested in calculating each student's average test score. This step requires figuring out how to properly group the test values. In this case, you cannot group by the
LastNamevalue, and when executed at scale, there might be collisions between first names when this recipe is run at scale. So, you might need to create a kind of primary key using the following:
You can now use this as a grouping parameter for your calculation:
merge col:'LastName','FirstName' with:'-' as:'studentId'
derive value:AVERAGE(TestScore) group:studentId as:'avg_TestScore'
After you drop unnecessary columns and move your columns around, the dataset should look like the following: