在 cmets 之后重申/澄清我认为你想要的:
所有类别都存在的每个国家/地区的最新日期
来源
代码 cmets 中解释的算法
M 码
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Country", type text}, {"Category", type text}, {"Year", Int64.Type}, {"Month No", Int64.Type}}),
//add index column to be able retain original order
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
//add a "real date" column: it will be useful when we determine latest common date
#"Added Custom2" = Table.AddColumn(#"Added Index", "Date", each #date([Year],[Month No],1)),
//create unique list of all categories for verification that all categories present
// to determine latest common date
allCat = List.Distinct(#"Added Custom2"[Category]),
//group by Country Year Month to assess if all categories present
grp = Table.Group(#"Added Custom2",{"Country","Year","Month No"},
{{"all", each _, type table[Category=text, Index=number]}}),
//if all categories present then mark it true
#"Added Custom" = Table.AddColumn(grp, "Common Month in Yr",
each List.RemoveMatchingItems(allCat,[all][Category])={}, type logical),
//expand. Then group by Country
//then filter each country to show only month/year where all categories are present (common)
#"Expanded all" = Table.ExpandTableColumn(#"Added Custom", "all", {"Category", "Index","Date"}, {"Category", "Index","Date"}),
#"Grouped Rows" = Table.Group(#"Expanded all", {"Country"},
{{"All", each _, type table [Country=nullable text, Year=nullable number, Month No=nullable number, Category=nullable text, Index=nullable number, Common Month in Yr=number]}}),
#"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Max common year/month", each Table.SelectRows([All], each [Common Month in Yr])),
//For each grouped country with only the common month/years, return the latest
#"Added Custom3" = Table.AddColumn(#"Added Custom1", "Max Common", each List.Max([#"Max common year/month"][Date]), type date),
//Remove unneeded columns
#"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Max common year/month"}),
//expand the table again
#"Expanded All" = Table.ExpandTableColumn(#"Removed Columns", "All",
{"Year", "Month No", "Category", "Index"},
{"Year", "Month No", "Category", "Index"}),
//sort back to original order; then remove the Index column
#"Sorted Rows" = Table.Sort(#"Expanded All",{{"Index", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
//Mark latest common year/month by determining if it matches the latest date
//then remove unneeded column
#"Added Custom4" = Table.AddColumn(#"Removed Columns1", "Latest Common Month Country/Category/Year", each
if Date.Year([Max Common]) = [Year] and Date.Month([Max Common]) = [Month No] then 1 else 0),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom4",{"Max Common"})
in
#"Removed Columns2"
结果
编辑:这是将两者结合的一种方式:
来源
M 码
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Country", type text}, {"Category", type text}, {"Year", Int64.Type}, {"Month No", Int64.Type}}),
//add index column to be able retain original order
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
//add a "real date" column: it will be useful when we determine latest common date
#"Added Custom2" = Table.AddColumn(#"Added Index", "Date", each #date([Year],[Month No],1)),
//Create unique list of all countries
allCountries = List.Distinct(#"Added Custom2"[Country]),
//group by Date to get last common month
grpDate = Table.Group(#"Added Custom2","Date",
{"allDate", each _, type table[Added Index=Int64.Type, Date=date, Category=text,Year=Int64.Type, Month No=Int64.Type ]}),
//mark each date that includes all countries
CommonMonths= Table.AddColumn(grpDate,"Last Common Month", each List.RemoveMatchingItems(allCountries,[allDate][Country])={}, type logical),
//Determine maximum date that includes all the countries
lastCommon = List.Max(Table.SelectRows(CommonMonths, each [Last Common Month])[Date]),
//Change the appropriate row to 1, else 0
lastCommonMonth=
Table.FromRecords(
Table.TransformRows(CommonMonths, (row)=>
Record.TransformFields(row, {
{"Last Common Month", each if row[Date] = lastCommon then 1 else 0}
}))),
//Expand this table and then calculate column including categories
expand = Table.ExpandTableColumn(lastCommonMonth,"allDate",
{"Country","Category","Year","Month No","Index"},
{"Country","Category","Year","Month No","Index"}),
//create unique list of all categories for verification that all categories present
// to determine latest common date
allCat = List.Distinct(#"Added Custom2"[Category]),
//group by Country Year Month to assess if all categories present
grp = Table.Group(expand,{"Country","Year","Month No"},
{{"all", each _, type table[Category=text, Index=Int64.Type, Last Common Month=Int64.Type]}}),
//if all categories present then mark it true
#"Added Custom" = Table.AddColumn(grp, "Common Month in Yr",
each List.RemoveMatchingItems(allCat,[all][Category])={}, type logical),
//expand. Then group by Country
//then filter each country to show only month/year where all categories are present (common)
#"Expanded all" = Table.ExpandTableColumn(#"Added Custom", "all",
{"Date","Category", "Index", "Last Common Month"},
{"Date","Category", "Index", "Last Common Month"}),
#"Grouped Rows" = Table.Group(#"Expanded all", {"Country"},
{{"All", each _, type table [Country=nullable text, Year=nullable number, Month No=nullable number, Category=nullable text, Index=nullable number, Common Month in Yr=number]}}),
#"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Max common year/month", each Table.SelectRows([All], each [Common Month in Yr])),
//For each grouped country with only the common month/years, return the latest
#"Added Custom3" = Table.AddColumn(#"Added Custom1", "Max Common", each List.Max([#"Max common year/month"][Date]), type date),
//Remove unneeded columns
#"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Max common year/month"}),
//expand the table again
#"Expanded All" = Table.ExpandTableColumn(#"Removed Columns", "All",
{"Year", "Month No", "Category", "Index","Last Common Month"},
{"Year", "Month No", "Category", "Index","Last Common Month"}),
//sort back to original order; then remove the Index column
#"Sorted Rows" = Table.Sort(#"Expanded All",{{"Index", Order.Ascending}}),
#"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),
//Mark latest common year/month by determining if it matches the latest date
//then remove unneeded column
#"Added Custom4" = Table.AddColumn(#"Removed Columns1", "Latest Common Month Country/Category/Year", each
if Date.Year([Max Common]) = [Year] and Date.Month([Max Common]) = [Month No] then 1 else 0),
#"Removed Columns2" = Table.RemoveColumns(#"Added Custom4",{"Max Common"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns2",{"Country", "Year", "Month No", "Category", "Latest Common Month Country/Category/Year", "Last Common Month"})
in
#"Reordered Columns"
结果