Reshape an Excel table between wide and long formats

Author: AILesson7 min setupTested with:ChatGPTReviewed: 2026-08-28

Quick answer

Unpivot or pivot data while protecting record grain, identifiers, null meaning, and aggregation rules. Provide: Source schema and samples, Target schema and use, Keys, labels, blanks, and aggregation. Expected result: A reversible reshape plan with Power Query steps, key checks, and row-count reconciliation.

1

Add your context

Your text stays in this browser. AILesson Prompts does not send it to a model or server.

2

Your prompt

Unfilled fields remain visible as placeholders, so you can still copy and edit the prompt

Design an Excel reshape from wide to long or long to wide using the supplied schema and rules.

Headers, types, grain, keys, measures, samples, blanks, and errors:
[source]

Target orientation, columns, order, downstream use, refresh, and reversibility:
[target]

Unique keys, label parsing, null/zero, duplicates, units, totals, aggregation, and validation:
[rules]

State the source and target record grain before proposing steps. For wide-to-long, separate identifier columns from measure columns and define how original headers become attribute fields; do not unpivot totals or notes accidentally. For long-to-wide, prove that each row-key and new-column combination is unique or apply only an explicitly approved aggregation. Preserve null versus zero, data types, units, and source labels. Prefer Power Query for refreshable transformations and provide menu-level steps plus key M operations when useful; use formulas only if the stated environment warrants them. Return: grain and key audit; source-to-target mapping; transformation steps; duplicate and header-parsing policy; expected row/column reconciliation; validation queries; sample output; refresh/reversal notes; and failure conditions that should stop the reshape.
Private by defaultPrompt assembly happens locally in your browser. Avoid placing confidential information into any AI service unless your organization allows it.

From input to outcome

A worked example

See how concrete context turns this recipe into a usable result

Actual input

Source schema and samples
Excel Table QuarterlyTargets, one row per Product ID + Region. Columns: Product ID text, Region text, Owner text, Q1 Target whole number, Q2 Target whole number, Q3 Target whole number, Q4 Target whole number, Annual Total formula, Notes text. 120 rows. Product ID + Region is unique. Q2 may be blank meaning target not set; zero means explicitly no target. No formula errors. Example P-01, East, Ana, 100, blank, 0, 140, 240, launch delayed.
Target schema and use
Wide to long for Power BI. Target columns Product ID, Region, Owner, Quarter, Target, Notes. One row per product-region-quarter, keeping blank target rows so missingness can be analyzed. Refresh monthly from the Excel Table. Annual Total must not become a quarter. Reversal to original four quarter columns should be possible, but Annual Total may be recalculated.
Keys, labels, blanks, and aggregation
Quarter headers must map exactly Q1 Target -> Q1 through Q4 Target -> Q4. Do not aggregate. Product ID + Region + Quarter must be unique. Preserve Target as nullable whole number and zero as zero. Owner and Notes repeat across quarter rows. Validate 120×4=480 rows and exactly four quarters per source key.

Example output

Grain audit: source key is Product ID + Region; target key is Product ID + Region + Quarter. Owner and Notes are descriptive attributes repeated by design. Annual Total is derived metadata and excluded from unpivot. Expected target is exactly 480 rows, including null targets.

Power Query steps: load QuarterlyTargets with Data > From Table/Range. Set Product ID, Region, Owner, Notes to text; Q1–Q4 Target to nullable whole number. Select Product ID, Region, Owner, Annual Total, Notes, then choose Unpivot Other Columns only after confirming the “other” set is exactly the four quarter columns; a safer refresh-resistant approach is explicitly select Q1 Target through Q4 Target and choose Unpivot Columns. Rename Attribute to Quarter Source and Value to Target. Map the four allowed header values to Q1–Q4; if any other value appears, return an error rather than trimming arbitrary text. Remove Annual Total after retaining it only for reconciliation, then order target columns. Do not filter null Target rows.

Validation: group by Product ID, Region, Quarter and stop if any count differs from 1. Group by source key and require four rows. Require row count 480, allowed Quarter set Q1–Q4, and whole-number-or-null Target. Recalculate non-null quarterly sum and compare with source Annual Total while treating null as missing, not automatically zero; for P-01 the long rows are Q1=100, Q2=null, Q3=0, Q4=140, with Owner Ana and Notes repeated. Reverse by pivoting Quarter using Target only after uniqueness passes and “Don't Aggregate” is available; recalculate Annual Total under an approved null rule.

Why this works

  1. 1

    Declaring grain before reshaping prevents repeated-looking rows from being mistaken for duplicates.

  2. 2

    Row-count and key reconciliation detects dropped measures, accidental totals, and unauthorized aggregation.

Check the result

  • Are identifier, measure, metadata, total, and note columns classified before transformation?

  • Is every long-to-wide row-key/column-key combination unique or explicitly aggregated?

  • Do source and target reconcile for keys, non-null measures, zeros, totals, and units?

Use it with confidence

Frequently asked questions

Practical answers about when to use this recipe, what to provide, and where human review still matters

What should I prepare before using “Reshape an Excel table between wide and long formats”?

For “Reshape an Excel table between wide and long formats,” prepare Source schema and samples, Target schema and use, and Keys, labels, blanks, and aggregation. Replace placeholders only with information you can verify. If a detail is unknown, preserve that uncertainty explicitly instead of asking the model to infer it.

When is the “Reshape an Excel table between wide and long formats” result not ready to use?

The result is not ready if it does not yet deliver the stated outcome—A reversible reshape plan with Power Query steps, key checks, and row-count reconciliation—from the supplied evidence, or if it relies on unresolved assumptions, missing approvals, or invented details. Use the checks as release gates: revise the source inputs or assign a named, authorized reviewer instead of polishing an unsupported output.

Which AI tools have recorded tests for “Reshape an Excel table between wide and long formats”?

The published test record for “Reshape an Excel table between wide and long formats” lists ChatGPT as of 2026-08-28. This confirms recorded runs, not guaranteed compatibility or identical results in later product versions. For another tool or version, keep every constraint visible and repeat the result checks before use.

More ways to explore

Where this recipe fits

Keep the work moving