You can also obtain your pictured output using Power Query
, available in Windows Excel 2010+ and Office 365 Excel
- Select some cell in your original table
Data => Get&Transform => From Table/Range
- When the PQ UI opens, navigate to
Home => Advanced Editor
- Make note of the Table Name in Line 2 of the code.
- Replace the existing code with the M-Code below
- Change the table name in line 2 of the pasted code to your "real" table name
- Examine any comments, and also the
Applied Steps
window, to better understand the algorithm and steps
Also note the #"Extracted Month Name"
step. This creates the text string that you see in the results table. If you want this to be a "real date", delete this last step from the Applied Steps window, and format that column on the Excel Worksheet for the appearance you desire
M Code
let
//Read in data
//change table name in next line to reflect actual table name in workbook
Source = Excel.CurrentWorkbook(){[Name="Table16"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,
{{"Name", type text}, {"Division", type text}, {"Start Date", type date}, {"End Date", type date}}),
//create list of dates by month with intervening months (between start and end) = first of the months
#"Added Custom" = Table.AddColumn(#"Changed Type", "Month/Year", each
let
StartDate = #date(Date.Year([Start Date]),Date.Month([Start Date]),1),
EndDate = [End Date],
mnthList = List.Generate(
()=>StartDate,
each _ <= EndDate,
each Date.AddMonths(_, 1)),
//Replace first and last months in the list with the actual date
replLast = List.ReplaceRange(mnthList,List.Count(mnthList)-1,1,{[End Date]}),
replFirst = List.ReplaceRange(replLast,0,1,{[Start Date]})
in
replFirst),
//Remove unneeded columns and move the Month/Year column to the beginning
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Start Date", "End Date"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Month/Year", "Name", "Division"}),
//expand the month list into new rows -- one row for each month
#"Expanded mnthList" = Table.ExpandListColumn(#"Reordered Columns", "Month/Year"),
//Extract the month name and year as a string for display
//Can omit these steps if you want actual months in the cells
// in which case you would format them on the worksheet
#"Extracted Month Name" = Table.TransformColumns(#"Expanded mnthList", {
{"Month/Year", each Date.MonthName(_) & " " & Text.From(Date.Year(_)), type text}})
in
#"Extracted Month Name"