- Joined
- Dec 16, 2012
- Messages
- 2,403
- Reaction score
- 48
- Points
- 48
- Excel Version(s)
- 365
Say you have a list:
{2,2,3,3,4,5,6,7,8,9,10,13,14,16,17,18,23,25,30,31,32}
to convert to:
2-10, 13-14, 16-18, 23, 25, 30-32
Any ideas?
Here's my very convoluted code for a function (as a query):
Can some PQ ace come up with something slick (or just different)?
{2,2,3,3,4,5,6,7,8,9,10,13,14,16,17,18,23,25,30,31,32}
to convert to:
2-10, 13-14, 16-18, 23, 25, 30-32
Any ideas?
Here's my very convoluted code for a function (as a query):
Code:
let
Source = {2,2,3,3,4,5,6,7,8,9,10,13,14,16,17,18,23,25,30,31,32},
#"Converted to Table1" = Table.FromList(Source, Splitter.SplitByNothing(),{"ref page"}, null, ExtraValues.Error),
#"Removed Duplicates" = Table.Distinct(#"Converted to Table1"),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Duplicates",{{"ref page", Int64.Type}}),
SortedRows = Table.Sort(#"Changed Type",{{"ref page", Order.Ascending}}),
AllNos = {List.Min(SortedRows[ref page])..List.Max(SortedRows[ref page])},
#"Converted to Table" = Table.FromList(AllNos, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, SortedRows, {"ref page"}, "Converted to Table", JoinKind.LeftOuter),
#"Expanded Converted to Table" = Table.ExpandTableColumn(#"Merged Queries", "Converted to Table", {"ref page"}, {"ref page"}),
#"Sorted Rows" = Table.Sort(#"Expanded Converted to Table",{{"Column1", Order.Ascending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows", "Custom", each if [ref page]=null then null else 1),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column1"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Custom"}, {{"grp", each _, type table [ref page=nullable number, Custom=nullable number]}},GroupKind.Local),
#"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Custom] = 1)),
#"Added Custom1" = Table.AddColumn(#"Filtered Rows", "min", each List.Min([grp][ref page])),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "max", each List.Max([grp][ref page])),
#"Added Custom3" = Text.Combine(Table.AddColumn(#"Added Custom2", "pr", each if [min]=[max] then Text.From([min]) else Text.From([min]) & "-" & Text.From([max]))[pr],", ")// else )
in
#"Added Custom3"
Can some PQ ace come up with something slick (or just different)?
Last edited: