Created
May 23, 2025 19:48
-
-
Save raycalvodev/88bb1c887db1060adf8b07d0beec1267 to your computer and use it in GitHub Desktop.
Conectar Power BI a Airtable con paginación (M Code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| BASE_ID = "tubase", | |
| TABLE_ID = "TuTabla", | |
| PERSONAL_ACCESS_TOKEN = "TuToken", | |
| Pagination = List.Skip( | |
| List.Generate( | |
| () => [Page_Key = "init", Counter=0], | |
| each [Page_Key] <> null, | |
| each [ | |
| Page_Key = try if [Counter]<1 then "" | |
| else [WebCall][Value][offset] otherwise null, | |
| WebCall = try if [Counter]<1 | |
| then Json.Document(Web.Contents("https://api.airtable.com", | |
| [RelativePath="v0/"&BASE_ID&"/"&TABLE_ID, | |
| Headers=[Authorization="Bearer "&PERSONAL_ACCESS_TOKEN]])) | |
| else Json.Document(Web.Contents("https://api.airtable.com", | |
| [RelativePath="v0/"&BASE_ID&"/"&TABLE_ID&"?offset="&[WebCall][Value][offset], | |
| Headers=[Authorization="Bearer "&PERSONAL_ACCESS_TOKEN]])), | |
| Counter = [Counter]+1 | |
| ], | |
| each [WebCall] | |
| ),1), | |
| #"Converted to Table" = Table.FromList( | |
| Pagination, Splitter.SplitByNothing(), null, null, ExtraValues.Error), | |
| #"Expanded Column1" = Table.ExpandRecordColumn( | |
| #"Converted to Table", "Column1", {"Value"}, {"Column1.Value"}), | |
| #"Expanded Column1.Value" = Table.ExpandRecordColumn( | |
| #"Expanded Column1", "Column1.Value", {"records"}, {"Column1.Value.records"}), | |
| #"Expanded Column1.Value.records" = Table.ExpandListColumn( | |
| #"Expanded Column1.Value", "Column1.Value.records"), | |
| #"Expanded Column1.Value.records1" = Table.ExpandRecordColumn( | |
| #"Expanded Column1.Value.records", "Column1.Value.records", | |
| {"id", "fields", "createdTime"}, | |
| {"Column1.Value.records.id", "Column1.Value.records.fields", "Column1.Value.records.createdTime"}), | |
| #"Renamed Columns" = Table.RenameColumns( | |
| #"Expanded Column1.Value.records1", | |
| {{"Column1.Value.records.id", "_airtableRecordId"}, | |
| {"Column1.Value.records.createdTime", "_airtableRecordCreatedAt"}, | |
| {"Column1.Value.records.fields", "_airtableRecordFields"}}), | |
| #"Reordered Columns" = Table.ReorderColumns( | |
| #"Renamed Columns", | |
| {"_airtableRecordId", "_airtableRecordCreatedAt", "_airtableRecordFields"}), | |
| #"Expanded Record Fields" = Table.ExpandRecordColumn( | |
| #"Reordered Columns", "_airtableRecordFields", | |
| Record.FieldNames(#"Reordered Columns"{0}[_airtableRecordFields]), | |
| Record.FieldNames(#"Reordered Columns"{0}[_airtableRecordFields])) | |
| in | |
| #"Expanded Record Fields" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Conectar Power BI a Airtable con paginación (M Code) | |
| Este Gist contiene dos scripts en lenguaje M (Power Query) que permiten conectar Power BI directamente con una base de datos de Airtable utilizando su API REST. Los scripts están diseñados para manejar la paginación automática de Airtable, lo que permite importar todos los registros de una tabla, incluso si superan el límite de 100 registros por solicitud. | |
| ## Características | |
| - Conexión autenticada mediante un **Personal Access Token (PAT)**. | |
| - Soporte para **paginación automática** usando el parámetro `offset`. | |
| - Conversión del JSON de la API a tablas de Power BI. | |
| - Expansión completa de los campos (`fields`) para facilitar su análisis. | |
| - Uso de variables (`BASE_ID`, `TABLE_ID`) para facilitar la reutilización con otras tablas. | |
| ## Instrucciones | |
| 1. Reemplaza los valores de `BASE_ID`, `TABLE_ID` y `PERSONAL_ACCESS_TOKEN` con los de tu propia cuenta y tabla de Airtable. | |
| 2. Copia el código en el **Editor Avanzado** de Power BI (Power Query). | |
| 3. Ajusta los campos expandidos en la última etapa si es necesario. | |
| ## ⚠️ Seguridad | |
| **¡No compartas públicamente tu `PERSONAL_ACCESS_TOKEN`!** | |
| Antes de publicar este código, reemplaza el token con un marcador de posición como este: | |
| ```m | |
| PERSONAL_ACCESS_TOKEN = "REEMPLAZAR_CON_TU_TOKEN" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment