Skip to main content

Find the Earliest Date Across Multiple Date Fields

  • April 30, 2020
  • 1 reply
  • 7 views

Forum|alt.badge.img+4

When a record has multiple date fields, sometimes you’d like to know which date is the earliest.

In the below example, each record is a social post that has different live dates for different channels. I’d like to know the earliest live date across all three channels. I could do this in a formula, but the ‘LIVE DATE’ field is also used by other teams, so I’d rather calculate the date with the below script (plus the awesome batchAnd script to do this for more than 50 records):

Example Base Here

1 // 🌸🌸 👇Add all the date fields you need here 👇 🌸🌸
2let fields = ['Instagram Date','IG Stories Date','Facebook Date','LIVE DATE']
3
4// Let Airtable know which table and views records you want to use
5let table = base.getTable("Content");
6let view = table.getView('All');
7let query = await view.selectRecordsAsync({fields:fields});
8let records = query.records;
9
10// Find the Earliest Date
11let dates = records.map( c => fields.map( x => (c.getCellValue(x) != null && x != 'LIVE DATE') ? Date.parse(c.getCellValue(x)) : null).filter(x => x));
12let minRaw = dates.map( c => c.reduce((acc,cur) => Math.min(acc,cur)));
13let min = minRaw.map( c => new Date(c).toISOString())
14let update = min.map( (c,i) => ({id:records[i].id,fields:{
15 'LIVE DATE': c
16}}));
17
18/*
19 Use this function to perform 'Update', 'Create', or 'Delete'
20 async actions on batches of records that could potentially
21 more than 50 records.
22
23 ::PARAMETERS::
24 action = string; one of 3 values:
25 - 'Update' to call table.updateRecordsAsync()
26 - 'Create' to call table.createRecordsAsync()
27 - 'Delete' to call table.deleteRecordsAsync()
28
29 table = Table; the table the action will be performed in
30
31 records = Array; the records to perform the action on
32 - Ensure the record objects inside the array are
33 formatted properly for the action you wish to
34 perform
35
36 ::RETURNS::
37 recordsActedOn = integer, array of recordId's, or null;
38 - Update Success: integer; the number of records processed by the function
39 - Delete Success: integer; the number of records processed by the function
40 - Create Success: array; the id strings of records created by the function
41 - Failure: null;
42*/
43async function batchAnd(action, table, records) {
44 let recordsActedOn;
45
46 switch (action) {
47 case 'Update':
48 recordsActedOn = records.length;
49 while (records.length > 0) {
50 await table.updateRecordsAsync(records.slice(0, 50));
51 records = records.slice(50);
52 };
53 break;
54
55 case 'Create':
56 recordsActedOn = [];
57 while (records.length > 0) {
58 let recordIds = await table.createRecordsAsync(records.slice(0, 50));
59 recordsActedOn.push(...recordIds)
60 records = records.slice(50);
61 };
62 break;
63
64 case 'Delete':
65 recordsActedOn = records.length;
66 while (records.length > 0) {
67 await table.deleteRecordsAsync(records.slice(0, 50));
68 records = records.slice(50);
69 }
70 break;
71
72 default:
73 output.markdown(`**Please use either 'Update', 'Create', or 'Delete' as the "action" parameter for the "batchAnd()" function.**`);
74 recordsActedOn = null;
75 }
76 return recordsActedOn;
77}
78
79
80// Update the records
81await batchAnd('Update',table,update)

1 reply

Forum|alt.badge.img+18
  • Inspiring
  • 251 replies
  • April 30, 2020

Hello @VictoriaPlummer,

I’m a part-time scripter coming from python to javascript mostly inside airtable script Block.
Scripting is helping me at my job that isn’t scripting but video color grading and teaching it (education).

I would like to thank you for your generosity in Scripts and Explanations because you take the time to publish them while you also have a job at Airtable!

oLπ
Brussels, Belgium, Europe.


Reply