您还可以使用Power Query 获取图片输出,在 Windows Excel 2010+ 和 Office 365 Excel 中可用
- 选择原始表格中的某个单元格
Data => Get&Transform => From Table/Range
- 当 PQ UI 打开时,导航到
Home => Advanced Editor
- 记下代码第 2 行中的表名。
- 用下面的M-Code替换现有代码
- 将粘贴代码的第 2 行中的表名更改为您的“真实”表名
- 检查所有 cmets 以及
Applied Steps 窗口,以更好地了解算法和步骤
还要注意#"Extracted Month Name" 步骤。这将创建您在结果表中看到的文本字符串。如果您希望这是一个“真实日期”,请从“应用步骤”窗口中删除最后一步,并在 Excel 工作表上将该列格式化为您想要的外观
M 码
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"