g_first(G;S;O;X)
Returns the first non-N/A value within a given group.
Function type
Vector only
Syntax
g_first(G;S;O;X)
t_first(X)
Input
Argument | Type | Description |
---|---|---|
G |
any | A space- or comma-separated list of column names Rows are in the same group
if their values for all of the columns listed in If If any of the columns listed in |
S |
integer | The name of a column in which every row evaluates to a 1 or 0, which determines
whether or not that row is selected to be included in the calculation If
If any of the values in
|
O |
integer | A space- or comma-separated list of column names that
determine the row order within a particular group If
If any of the values in |
X |
any simple type | A column name |
Return Value
For every row in each group defined by G
and ordered by O
(and for those rows where S
=1, if specified),
g_first
returns the first non-N/A value in the column listed in
X
. The result is the same data type as X
.
If no rows have valid (non-N/A) values for X
, the result is N/A.
g_
(and t_
) functions.Sample Usage
<base table="pub.doc.samples.ref.func.g_func_time_series_sample_usage"/> <willbe name="g_first_1" value="g_first(state;include;order;value)"/> <willbe name="g_first_2" value="g_first(state city;include;order;value)"/>
Example
For this example, let's use the table pub.demo.retail.prod.
dept_na
and
deptdesc_na
column, which have the same
values as the dept
and deptdesc
columns but with some N/A values in
them.<willbe name="dept_na" value="if(dept=14;NA;dept)"/>
<willbe name="deptdesc_na" value="if(dept=14;NA;deptdesc)"/>
<colord cols="dept,deptdesc,dept_na,deptdesc_na"/>
We might assume that g_first
would return
22 for the
dept_na
column and
"SNACKS" for the
deptdesc_na
column, since
g_first
returns the first non-N/A value
within a given group. Let's see what we get if we run
g_first
on each of these columns:
<willbe name="dept_first" value="g_first(;;;dept_na)"/>
<willbe name="deptdesc_first" value="g_first(;;;deptdesc_na)"/>
We can see from this example that g_first
does indeed
return a 22 for the
dept_na
column. However,
g_first
returns the empty string for the
deptdesc_na
column. This is because the
deptdesc_na
column is a text column, and
the empty string is not considered to be an N/A value for the
purposes of g_
(and t_
)
functions.
g_first
to return the first value in
deptdesc_na
that was not an empty string,
you could use the S
argument with the
g_first(G;S;O;X)
function. First, create a
computed column that contains a 1 for
those rows in deptdesc_na
that are not
N/A:<willbe name="not_na" value="deptdesc_na<>NA"/>
S
argument to the
g_first
function so that it only considers
those
rows:<willbe name="deptdesc_first_not_na" value="g_first(;not_na;;deptdesc_na)"/>
You can see that g_first
now returns
"SNACKS" for the
deptdesc_na
column:
Additional Information
- The
t_
version of this function defaults theG
argument and omits theS
andO
arguments. The default forG
is set at table load time based on the organization of the table.