考虑类别的国家最近的共同月份

Latest Common Month in a Country considering Category

我正在尝试为 Power 查询中的标志创建一个自定义列(一个国家/地区的最新常见月份)。在国家/地区、类别、年份、月份的基础上,我正在尝试查找最近月份国旗。 数据如下所示:

我尝试使用以下 M 代码,但未考虑类别列:

Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source, {{"Country", type text}, {"Year", Int64.Type}, {"Month No", Int64.Type}}),

//Country List
countries = List.Distinct(Table.Column(#"Changed Type","Country")),

//Calculate full date
#"Added Custom" = Table.AddColumn(#"Changed Type", "fullDate", each #date([Year],[Month No],1),type date),

//determine latest month flag by country
#"Grouped Rows" = Table.Group(#"Added Custom", {"Country"}, {{"grouped", each _, type table [Country=nullable text, Year=nullable number, Month No=nullable number, fullDate=date]}, {"latest fullDate", each List.Max([fullDate]), type date}}),
#"Expanded grouped" = Table.ExpandTableColumn(#"Grouped Rows", "grouped", {"Year", "Month No", "fullDate"}, {"Year", "Month No", "fullDate"}),
#"Added Custom1" = Table.AddColumn(#"Expanded grouped", "Latest Month Flag", each if [latest fullDate] = [fullDate] then 1 else 0),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"latest fullDate"}),

请帮我找到每个国家考虑类别的最新常见月份

给restate/clarify我觉得你想要的评论后:

出现所有类别的每个国家的最晚日期

来源

算法在代码注释中解释

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"

结果