I’m trying to figure out the best way to reference multiple tables with the same schema across multiple datasets in, for example, definitions and includes files.
For example, I have a project that has different datasets for each client. And inside those datasets they have FBADS_AD_* sharded tables. My goal would be to dynamically reference all applicable tables and create report-ready views for each table.
To reference multiple tables with the same schema across multiple datasets in Dataform, you can use the following approach:
Use JavaScript to loop through the datasets and tables.
For each table, construct the table name and use the reffunction to reference the table.
The following code shows an example of how to do this:
const CLIENTS = ['client1', 'client2', 'client3'];
const TABLE_PREFIX = 'FBADS_AD_';
const fbadsAdTables = [];
CLIENTS.forEach(client => {
const tableName = `${TABLE_PREFIX}${client}`;
const tableRef = ref(client, tableName);
fbadsAdTables.push(tableRef);
});
// Use the fbadsAdTables variable to reference the tables in your definitions and includes files.
Create report-ready views for each table
Once you have referenced all of the tables, you can use Dataform’s publish method to create a report-ready view for each table. The publish method allows you to specify the table name and the SQL query that you want to use to create the view.
The following code shows an example of how to create a report-ready view for each table in the fbadsAdTables variable:
Hi and thank you for this answer. Everything seems pretty clear in the logic, but I’m missing some basics, which you already assumed (I guess). Dataform allows to wrap Javascript within a SQLX script. So the question is: the first piece of code in your example stays in a .js file? Same for the second piece of code: is it a SQLX that wraps a piece of Javascript or viceversa?