【问题标题】:Latest Common Month in a Country considering Category考虑类别的国家/地区的最新共同月份
【发布时间】:2021-10-20 09:32:53
【问题描述】:

我正在尝试为 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"}),

请帮我找到考虑类别的每个国家/地区的最新共同月份

【问题讨论】:

  • 我没有遵循你的逻辑。对于某些country/category/date combinations,似乎没有最新的共同月份。对于牙膏2019/10 不是这样吗,因为它是所有三个国家/地区中唯一存在的类别/日期组合?
  • 这里 2019/10 国家牙膏澳大利亚不是最新的一年.. 2021/9 是最新的......对于一个国家,我正在尝试获取最新日期,同时考虑到类别列..
  • 我想我不明白当你写 latest common month 与只是 latest month 为给定国家/类别组合。这些短语有什么区别?提取给定国家/类别组合的最新月份(或年份/月份)只是使用 Table.Group 对话框中的 Max 聚合的问题。比你之前的问题简单很多。但显然我错过了一些东西,因为在这种情况下,我们/牙膏的最小值将是 2019/10
  • 最新的常见我的意思是...最近一年的最后一个月,,,为了决定这个,我不仅考虑国家,而且考虑国家的类别名称。如果牙膏最近一个月是 08/2021和体育 09/2021,那么对我来说,该国最近的共同月份是 08/2021
  • 我看到,对于美国,2021 8 具有两个/所有类别。但是英国呢?对于 2021 年,没有任何月份同时拥有 ToothpasteSports。那么为什么选择其中之一呢?或者为什么不选择2020 12,它是拥有两者的最近日期?

标签: excel powerbi powerquery


【解决方案1】:

在 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"

结果

【讨论】:

  • 谢谢罗恩...这将工作...但现在只有我的第二个标志的问题...之前工作正常..但是使用新逻辑它不起作用...能否请您帮我编写 M 代码以查找“在不考虑类别的情况下在所有国家/地区可用的最新通用月份标志”。我已在屏幕截图中更新了我的数据
  • @faisal 您应该能够合并这两个查询,但您必须进行一些手动编辑。此外,在您更新的示例中,为什么 RUS 2021 8 未在 Last common month including category 列中标记。它有所有类别,2021 8 是一个普通月份。
  • 修复数据错误
  • @faisal 请参阅 Edit 了解将两者结合的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-12
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多