How to create a Data Transformation Script in Fabric Copilot
This tutorial shows how to use Copilot in Fabric to generate a Data Transformation Script that standardizes formats, applies business rules, and enriches data (e.g.: concatenation, name normalization, date conversion). Knowing how to automate these transformations makes ETL easier and reduces manual errors.
Prerequisites
- An account with access to Microsoft Fabric and permissions to edit the Lakehouse or the Power BI dataset.
- A sample data table with text and date type columns (e.g.: CustomerName, BirthDate, CountryCode, Email).
- Basic familiarity with SQL and with the Copilot interface in Fabric.
Step 1: Prepare the sample and identify goals
Open your table in the Lakehouse or dataset and identify which transformations you need: normalize names (capitalization), convert dates, validate emails, map country codes, and create a derived column (age). Defining goals makes the prompt more precise.
Step 2: Open Copilot and describe the objective clearly
In the Fabric workspace, click Copilot and start a new conversation. Write a clear prompt with the column names and the expected result. Common mistake examples: being vague about date formats or how to handle null values. Be specific.
Prompt exemplo:
"Tenho uma tabela chamada Customers com colunas: CustomerName (texto), BirthDate (texto ISO ou dd/MM/yyyy), CountryCode (ex: PT, FR), Email (texto). Quero um Data Transformation Script que:
- Padronize CustomerName para Title Case e remova espaços duplos;
- Converta BirthDate para tipo DATE, aceitando ISO e dd/MM/yyyy, e crie coluna Age INT;
- Valide Email com regex e marque EmailValid BOOLEAN;
- Mapear CountryCode para CountryName (PT -> Portugal, FR -> France);
- Substituir strings vazias por NULL.
Gera o script em SQL compatível com Fabric (Spark SQL / T-SQL similar) e explica cada passo em 3-4 linhas."
Step 3: Adjust the script generated by Copilot
Copilot will generate a script. Review and adjust it for the dialect of your environment (for example Spark SQL in the Lakehouse). Check the functions used (e.g.: regexp_extract, to_date, trim). Correct table/column names if necessary.
Exemplo de script (estilo Spark SQL):
CREATE OR REPLACE VIEW Customers_Transformed AS
SELECT
TRIM(REGEXP_REPLACE(INITCAP(CustomerName), '\\s+', ' ')) AS CustomerName,
CASE
WHEN BirthDate IS NULL OR trim(BirthDate) = '' THEN NULL
WHEN BirthDate RLIKE '^\\d{4}-\\d{2}-\\d{2}$' THEN to_date(BirthDate)
WHEN BirthDate RLIKE '^\\d{2}/\\d{2}/\\d{4}$' THEN to_date(from_unixtime(unix_timestamp(BirthDate, 'dd/MM/yyyy')))
ELSE NULL
END AS BirthDate,
CASE
WHEN BirthDate IS NULL THEN NULL
ELSE CAST(FLOOR(datediff(current_date(), to_date(BirthDate)) / 365.25) AS INT)
END AS Age,
CASE
WHEN Email RLIKE '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$' THEN true
ELSE false
END AS EmailValid,
CASE CountryCode
WHEN 'PT' THEN 'Portugal'
WHEN 'FR' THEN 'France'
WHEN 'ES' THEN 'Spain'
ELSE 'Unknown'
END AS CountryName
FROM Customers;
Step 4: Test and iterate with Copilot to cover edge cases
Run the script on a copy of the table or on a view. Check rows with unexpected values and ask Copilot for improvements (for example: add accent normalization, handle multiple email formats, orchestrate a fallback for dates). Use concrete examples in the prompt for Copilot to reproduce the issue.
Step 5: Implement as a repeatable pipeline
When the script is validated, integrate it into your ETL: create a Notebook or a Pipeline in Fabric that runs the transformation regularly. Automate with parameters (e.g.: table names) to reuse the script. Common mistake: forgetting to manage permissions when replacing tables.
Verify the result
Validate the results with simple queries: 1) Count NULLs vs previous; 2) View CustomerName content to ensure Title Case; 3) Count EmailValid = false; 4) Validate Age with manual samples. Example queries:
SELECT COUNT(*) AS total, SUM(CASE WHEN EmailValid THEN 1 ELSE 0 END) AS valid_emails FROM Customers_Transformed;
SELECT CustomerName FROM Customers_Transformed WHERE CustomerName RLIKE '\\b[a-z]{1}'; -- procura nomes não capitalizados
SELECT BirthDate, Age FROM Customers_Transformed ORDER BY Age DESC LIMIT 10;
Conclusion
With Copilot in Fabric you can quickly generate a Data Transformation Script that standardizes, validates, and enriches data. Next steps: add automated tests, integrate with Data Quality Rules and parameterize the script for multiple tables. Tip: save effective prompts to reuse and refine with real data examples.