Another solution is based on recently introduced JS UDF.
It can look more heavy than one I propsed already above, but I like it too as it gives great/fine control on analytics logic.
I doubt this will be your practical choice, but conceptually this can be useful
So, for example from MySQL - Subtracting value from previous row, group by the solution would be
SELECT SN, Date, ROUND(consumption,2) as consumption FROM
js( // input table
(SELECT SN, NEST(STRING(Date) + ',' + STRING(Value)) as Metric
FROM temp.EnergyLog GROUP BY SN) ,
// input columns
SN, Metric,
// output schema
"[{name: 'SN', type: 'integer'},
{name: 'Date', type: 'string'},
{name: 'consumption', type: 'float'}]",
// function
"function(r, emit){
pair = r.Metric.sort(function (a,b) {return a > b;});
val = pair[0].split(','); Date = val[0];
emit({SN: r.SN, Date: Date, consumption: 0});
for (var i=0; i<pair.length -1; i +=1){
val = pair[i].split(','); Date = val[0]; Value1 = val[1];
val = pair[i+1].split(','); Value2 = val[1];
emit({SN: r.SN, Date: Date, consumption: Value2 - Value1});
}
}"
) ORDER BY SN, Date
You can check UDF documentation here: https://cloud.google.com/bigquery/user-defined-functions
The output will be exactly the same as for previosly suggested solution using LAG
Hope you will be able "translate" above code to your case with [bla] table