May 19 2021 03:41 PM
Hello,
I need help with transforming a table: from the example on the left, to the one on the right.
I tried pivot column but where more dates are present for the same name, I had an error.
I added an additional column and then pivot, but ended up with thousands of columns, one for each individual date.
Many thanks for your time and help.
KL
May 19 2021 04:35 PM
E2: =UNIQUE(A2:A7)
F1: ="DATE "&SEQUENCE(,MAX(COUNTIFS(A2:A7,E2#)))
F2: =TRANSPOSE(FILTER($B$2:$B$7,$A$2:$A$7=E2))
May 20 2021 01:18 AM
May 20 2021 02:29 AM
May 20 2021 03:45 AM
May 20 2021 05:23 AM
(one way) Power Query / M code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
ChangedTypes = Table.TransformColumnTypes(Source,
{{"Alpha", type text}, {"Date", type date}}
),
GroupedRows = Table.Group(ChangedTypes, {"Alpha"},
{"DATA", each
Table.Transpose(
Table.SelectColumns(_, {"Date"})
), type table
}
),
ColumnNames = List.TransformMany({"Column", "DATE "},
each {1..List.Max(List.Transform(GroupedRows[DATA], Table.ColumnCount))},
(x, y)=> x & Text.From(y)
),
SplittedList = List.Split(ColumnNames, List.Count(ColumnNames)/2),
ExpandedData = Table.ExpandTableColumn(GroupedRows, "DATA",
List.First(SplittedList)
),
RenamedColumns = Table.RenameColumns(ExpandedData,
List.Zip(
{ List.First(SplittedList), List.Last(SplittedList) }
)
),
TypedDate = Table.TransformColumnTypes(RenamedColumns,
List.Transform(List.Last(SplittedList), each {_, type date})
)
in
TypedDate
Sample avail. here
May 20 2021 08:18 AM
SolutionOn reflection the following is more straightforward and less complex to understand + Columns names are fully dynamic: whatever the names of your 2 columns are in the source table this will work as long as the column containing the dates is the 2nd column
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
SourceColumns = Table.ColumnNames(Source),
FirstColumn = List.First(SourceColumns),
GroupedRows = Table.Group(Source, FirstColumn,
{"DATA", each Table.FromRows(
{ {Table.FirstValue(_)} & Table.Column(_, List.Last(SourceColumns)) }
), type table
}
),
CombinedTables = Table.Combine(GroupedRows[DATA]),
DateColumns = List.Transform({1..Table.ColumnCount(CombinedTables)-1}, each
"DATE " & Text.From(_)
),
RenamedColumns = Table.RenameColumns(CombinedTables,
List.Zip({
Table.ColumnNames(CombinedTables),
{FirstColumn} & DateColumns
})
),
TypedDate = Table.TransformColumnTypes(RenamedColumns,
List.Transform(DateColumns, each {_, type date})
)
in
TypedDate
Sample updated - see BetterQuery
May 20 2021 10:50 AM
May 20 2021 11:08 AM
May 20 2021 11:13 AM
May 20 2021 08:18 AM
SolutionOn reflection the following is more straightforward and less complex to understand + Columns names are fully dynamic: whatever the names of your 2 columns are in the source table this will work as long as the column containing the dates is the 2nd column
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
SourceColumns = Table.ColumnNames(Source),
FirstColumn = List.First(SourceColumns),
GroupedRows = Table.Group(Source, FirstColumn,
{"DATA", each Table.FromRows(
{ {Table.FirstValue(_)} & Table.Column(_, List.Last(SourceColumns)) }
), type table
}
),
CombinedTables = Table.Combine(GroupedRows[DATA]),
DateColumns = List.Transform({1..Table.ColumnCount(CombinedTables)-1}, each
"DATE " & Text.From(_)
),
RenamedColumns = Table.RenameColumns(CombinedTables,
List.Zip({
Table.ColumnNames(CombinedTables),
{FirstColumn} & DateColumns
})
),
TypedDate = Table.TransformColumnTypes(RenamedColumns,
List.Transform(DateColumns, each {_, type date})
)
in
TypedDate
Sample updated - see BetterQuery