Write Power Query (M) steps
Inputs
source_description (required): Where the data comes from (Excel files in a folder, CSV, a SharePoint list, a database table, a web table), what it looks like (column names, sample rows, header rows, totals, quirks) and how often new data arrives.
desired_output (required): The table you want at the end (columns, one row per what, types) and where it goes (worksheet table, data model, Power BI).
Arguments fill these in order. If a required value is empty, take it from the user’s message or ask for it once.
into this output:
- Restate the transformation as a short plan: source → steps → output grain (one row per what). If the source layout, the header row or the output grain is unclear in a way that changes the code, ask up to three specific questions and stop instead of guessing.
- Write the full query as one
let … in block with descriptive step names (for example #"Removed blank rows"). If several queries are needed (a file-combine helper function, a lookup table, a parameter), give each separately and say which to load and which to set to connection only.
- Use refresh-safe patterns:
- File paths and other environment values as parameters, not literals in the code.
- For folders of files, filter by extension and name pattern, ignore temporary files (names starting with
~$), and combine with a function applied to each file, so a new file is picked up automatically.
- Promote headers and set types explicitly for the columns you need, using a locale (
Table.TransformColumnTypes(…, "en-GB") or similar) when dates or decimals depend on it; select the needed columns by name with MissingField.UseNull or MissingField.Ignore where a missing column should not break the refresh.
- Reshape with
Table.UnpivotOtherColumns so new period columns are included, rather than unpivoting a hard-coded list.
- Remove totals, blank and repeated header rows by a rule (a filter on a key column), not by fixed row positions, unless the layout guarantees them.
- Use
try … otherwise only for expected bad values, and keep a way to see rows that failed (for example an errors query), never silently drop them.
- Merge queries on cleaned keys (trimmed, consistent case and type), and say whether the join can duplicate rows.
- Explain each step in one plain sentence: what it does and why.
- Note query folding where the source is a database: which steps will fold and which will break folding, and order the steps to keep folding as long as possible.
- Give checks the user can run after refresh: row counts against the source, a total that should match, a count of nulls in key columns.
Query
One fenced code block per query, each with its name and whether it loads.
Step by step
A numbered list: step name — what it does and why.
Refresh safety
What happens when a new file, a new column, an empty month or a renamed column arrives, and how the query handles it.
Checks
Checks to run after the first refresh.
Questions
Only if anything is still assumed.
1---2name: write-power-query3description: Writes Power Query (M) steps that import, clean, combine and reshape data with refresh-safe logic, explaining each step. Use in Excel or Power BI to automate data prep you redo by hand.4license: CC0-1.05arguments:6 - source_description7 - desired_output8argument-hint: <source_description> <desired_output>9disable-model-invocation: true10metadata:11 version: 1.0.012 kind: prompt13 category: spreadsheets14 source: https://skillmd.com/skills/hermes-hq/write-power-query15 upstream: https://hermes-ide.com/prompts/write-power-query16 catalog: 2026.1014.117---1819# Write Power Query (M) steps2021## Inputs2223- `source_description` (required): Where the data comes from (Excel files in a folder, CSV, a SharePoint list, a database table, a web table), what it looks like (column names, sample rows, header rows, totals, quirks) and how often new data arrives.24- `desired_output` (required): The table you want at the end (columns, one row per what, types) and where it goes (worksheet table, data model, Power BI).2526Arguments fill these in order. If a required value is empty, take it from the user’s message or ask for it once.2728<context>29You write Power Query for people who will press Refresh every week without opening the editor. The query must keep working when a new file lands in the folder, when a column is added at the source, when a month has no rows, or when someone's locale writes dates differently. You know the M language well (`let … in`, `Table.*`, `List.*`, `each`, `try … otherwise`), the patterns the UI generates and where those patterns are brittle, for example the automatic "Changed Type" step that hard-codes every column name.30</context>3132<task>33Write the Power Query (M) that turns this source:3435<source_description>36$source_description37</source_description>3839into this output:4041<desired_output>42$desired_output43</desired_output>44451. Restate the transformation as a short plan: source → steps → output grain (one row per what). If the source layout, the header row or the output grain is unclear in a way that changes the code, ask up to three specific questions and stop instead of guessing.462. Write the full query as one `let … in` block with descriptive step names (for example `#"Removed blank rows"`). If several queries are needed (a file-combine helper function, a lookup table, a parameter), give each separately and say which to load and which to set to connection only.473. Use refresh-safe patterns:48 - File paths and other environment values as parameters, not literals in the code.49 - For folders of files, filter by extension and name pattern, ignore temporary files (names starting with `~$`), and combine with a function applied to each file, so a new file is picked up automatically.50 - Promote headers and set types explicitly for the columns you need, using a locale (`Table.TransformColumnTypes(…, "en-GB")` or similar) when dates or decimals depend on it; select the needed columns by name with `MissingField.UseNull` or `MissingField.Ignore` where a missing column should not break the refresh.51 - Reshape with `Table.UnpivotOtherColumns` so new period columns are included, rather than unpivoting a hard-coded list.52 - Remove totals, blank and repeated header rows by a rule (a filter on a key column), not by fixed row positions, unless the layout guarantees them.53 - Use `try … otherwise` only for expected bad values, and keep a way to see rows that failed (for example an errors query), never silently drop them.54 - Merge queries on cleaned keys (trimmed, consistent case and type), and say whether the join can duplicate rows.554. Explain each step in one plain sentence: what it does and why.565. Note query folding where the source is a database: which steps will fold and which will break folding, and order the steps to keep folding as long as possible.576. Give checks the user can run after refresh: row counts against the source, a total that should match, a count of nulls in key columns.58</task>5960<constraints>61- Write valid M. Use only functions that exist in Power Query; if you are not sure a function or option is available in the user's version, say so.62- Do not invent column names or sample values. Use the names given; where you must assume one, mark it in the code with a comment (`// assumed column name`).63- Comment non-obvious steps inside the code with `//` comments.64- Mention privacy levels if the query combines sources of different kinds (for example a file and a web source), because they can block refresh.65- Keep it as simple as the job allows; prefer UI-reproducible steps where possible so the user can still maintain the query in the editor.66</constraints>6768<output_format>69## Plan70Source → steps → output grain, in three to six lines.7172## Query73One fenced code block per query, each with its name and whether it loads.7475## Step by step76A numbered list: step name — what it does and why.7778## Refresh safety79What happens when a new file, a new column, an empty month or a renamed column arrives, and how the query handles it.8081## Checks82Checks to run after the first refresh.8384## Questions85Only if anything is still assumed.86</output_format>