Forum Discussion
Power query, move rows to columns based on there value
- 1 year ago
Hi rafterse
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.
To achieve this transformation in Power Query, you can follow these steps to pivot the landline numbers into separate columns (Landline1, Landline2), based on Company and Phone type:
- Load your data into Power Query.
- Filter to keep only rows where Phone type = "landline".
- Add an Index column grouped by Company to distinguish multiple landlines.
- Pivot the landline table with Index as column headers and number as values.
- Rename columns to Landline1, Landline2, etc.
- Merge the landline table back with the original data (outer join on Company).
Power Query M Script Example:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
FilterLandline = Table.SelectRows(Source, each [Phone type] = "landline"),
AddIndex = Table.AddIndexColumn(FilterLandline, "Index", 1, 1, Int64.Type),
Pivoted = Table.Pivot(Table.TransformColumnTypes(AddIndex, {{"Index", type text}}, "en-US"),
List.Distinct(Table.TransformColumnTypes(AddIndex, {{"Index", type text}}, "en-US")[Index]),
"Index", "number"),
RenamedCols = Table.RenameColumns(Pivoted,{{"1", "Landline1"}, {"2", "Landline2"}}),
RemovedPhoneType = Table.RemoveColumns(RenamedCols,{"Phone type"}),
MergedTables = Table.NestedJoin(Source, {"Company"}, RemovedPhoneType, {"Company"}, "Landlines", JoinKind.LeftOuter),
ExpandedLandlines = Table.ExpandTableColumn(MergedTables, "Landlines", {"Landline1", "Landline2"}),
FinalTable = Table.SelectColumns(ExpandedLandlines, {"Company", "Phone type", "number", "Landline1", "Landline2"})
in
FinalTableThis will give you a final output as shown in your screenshot — each row will show the mobile or landline type, and Landline1, Landline2 for each company.
Tips:
- If you may have more than 2 landlines in the future, consider dynamically handling more columns or using a custom column name pattern like "Landline" & Index.
- Use Microsoft Power Query M documentation to modify for your environment.
✔️ If my message helped solve your issue, please mark it as Resolved! 👍 If it was helpful, consider giving it a Kudos! |