Jul 13, 2020 05:04 PM
What’s the best way to do a simple analysis of data? One of the most basic things I want to do is count the number of records that match a set of filter criteria.
Jul 13, 2020 05:27 PM
Are you skilled in simple javascript?
Jul 13, 2020 06:37 PM
I would not say skilled, but I’ve written some. What would you type into the JS block to do it?
Jul 14, 2020 04:25 AM
Brian,
Let’s set some expectations at the outset. We’ve not defined “it” or “simple analysis of data”. Until we know what these terms actually mean, there is no helpful answer to this question and there is nothing to type into a Script Block. :winking_face:
Analysis of data is at least on the doorstep of data science which requires a little planning and a stated objective. Below is a reference to one script block I built ages ago that generates a forecast based on historical data and then takes that model to a little higher level with machine learning (simply, as new operational results are added, the forecasts learn and become more precise. This example includes the script block code and liberal comments so you can get a sense of the script itself.
To get a few enterprising script block designers to guide you in this community, I recommend you take some time to give us an example of some data and an analysis you’d like to perform. Be as detailed as possible because no one like to write code for free to later discover that they wrote the wrong code.
Jul 14, 2020 11:59 AM
Sure, yes details are good to have before beginning a project.
Two bases of interest:
A general item is created by me through Airtable. A specific item is created by a user through an app. Each general item has a list of specific items that belong to it from various users. The app software I’m using doesn’t currently allow custom Javascript unfortunately. I am also limited in that I cannot create linked fields with the data coming from the app, so I cannot use rollup fields.
So my goal in Airtable is:
Count the number of specific items that belong to a general item without using any linked relationship fields. I am using General Item ID as a field in the Specific Items base to make the connection manually.
Jul 14, 2020 05:14 PM
Here’s a start … it’s a block that lets you determine the occurrence of a given string in a text field for all records in the selected table.
Source Code
/*
***********************************************************
Airdrop - Counting Example
Copyright (c) 2020 by Global Technologies Corporation
ALL RIGHTS RESERVED
***********************************************************
*/
output.markdown('# Counting Example');
// get the table name
output.markdown("### Select the Table");
let sourceTable = await input.tableAsync("Pick the table:");
let sourceTableName = sourceTable.name;
// get the source field name
output.markdown("### Select the Field to be Counted");
let sourceField = await input.fieldAsync("Pick the name of the field to be counted:", sourceTable.id);
let sourceFieldName = sourceField.name;
// set the value to be counted
output.markdown("### Enter the Value to be Counted:");
let thisQueryValue = await input.textAsync('What value do you want to count?');
// get the records
let result = await sourceTable.selectRecordsAsync();
let sourceRecords = result.records;
// iterate across all the records
let thisCount = 0;
let thisFieldValue;
for (var r = 0; r < sourceRecords.length; r++)
{
// get the field value for this record
thisFieldValue = sourceRecords[r].getCellValue(sourceFieldName);
if (thisFieldValue != null) {
if (thisFieldValue.toString().toLowerCase().includes(thisQueryValue.toString().toLowerCase()))
thisCount += 1;
}
}
// display the count
output.markdown("Counting the occurrence of **'" + thisQueryValue + "'** in field **'" + sourceFieldName + "'** = **" + thisCount + "**");
Jul 15, 2020 08:59 AM
hey Bill, I like the simplicity of the layout.
It doesn’t quite do what I need though. This is how I would break it down:
Jul 15, 2020 12:32 PM
I see in the Javascript block examples there is an example for Running Total for a particular base view. Is there a way to apply that script to make counts for filtered groups within a view?
Jul 15, 2020 12:35 PM
Sure - it’s just a matter of writing script to embrace the data in a way that hives you the expected metrics.