【问题标题】:Finding max function in custom column in Power query在 Power 查询的自定义列中查找最大函数
【发布时间】:2021-05-17 13:38:44
【问题描述】:

我正在尝试在 Power 查询中为标志创建两个自定义列。基于县、年、月,我试图找到一个国家/地区标志的最新月份和所有国家/地区可用的最新通用月份。数据如下所示:

我尝试了以下措施...: =var year_x= CALCULATE(MAX([Year]),ALLEXCEPT('tblename','tablename'[Country])) var month_x = CALCULATE(max([Month No]),FILTER(ALLEXCEPT('tablename','tablename'[CountryI]),'tablename'[Year]=year_x))

返回月份_x

但这不会解决我的问题,因为我想在电源查询中创建一个自定义标志。我知道我们可以在 Power BI 中执行此操作...但在 Excel 电源查询中不知道

请帮我找到一个选项

【问题讨论】:

  • 这看起来像 dax,而不是电源查询
  • 我尝试过测量...但我想构建一个自定义列,如表中所示
  • 你写基于县、年、月。但是您是否只想考虑Latest month available all the country Flag 的月份?否则当11/2020 在其他国家/地区不可用时,Aus 的逻辑是什么
  • 嗨 Ron.thanks ....刚刚编辑了表格..第二个标志是所有国家/地区可用的最新常用月份..我正在考虑这个最新常用月份的年份和月份跨度>
  • 感谢您的回答...我正在尝试使用您的代码...您能重新发布吗

标签: excel powerbi powerquery


【解决方案1】:

试试:

M 码

let
    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"}),

//Determine latest month flag for ALL countries
    #"Grouped Rows1" = Table.Group(#"Removed Columns", {"fullDate"}, {{"Grouped", each _, type table [Country=nullable text, Year=nullable number, Month No=nullable number, fullDate=nullable date, Latest Month Flag=number]}}),
    #"Added Custom2" = Table.AddColumn(#"Grouped Rows1", "Latest month ALL countries", each List.Count(
       List.RemoveMatchingItems(countries, Table.Column([Grouped],"Country"))) = 0),
    #"Grouped Rows2" = Table.Group(#"Added Custom2", {"Latest month ALL countries"}, {{"Grouped", each _, type table [fullDate=nullable date, Grouped=table, Latest month ALL countries=logical]}, {"maxAll", each List.Max([fullDate]), type nullable date}}),
    #"Expanded Grouped" = Table.ExpandTableColumn(#"Grouped Rows2", "Grouped", {"fullDate", "Grouped"}, {"fullDate", "Grouped.1"}),
    #"Added Custom3" = Table.AddColumn(#"Expanded Grouped", "Latest month ALL countries Flag", each if [maxAll] = [fullDate] and 
      [Latest month ALL countries] = true 
      then 1 else 0),
    #"Expanded Grouped.1" = Table.ExpandTableColumn(#"Added Custom3", "Grouped.1", {"Country", "Year", "Month No", "fullDate", "Latest Month Flag"}, {"Country", "Year", "Month No", "fullDate.1", "Latest Month Flag"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded Grouped.1",{"Latest month ALL countries", "fullDate", "fullDate.1", "maxAll"}),
    #"Sorted Rows" = Table.Sort(#"Removed Columns1",{{"Country", Order.Descending}, {"Year", Order.Ascending}, {"Month No", Order.Ascending}})
in
    #"Sorted Rows"

原始数据

结果

*注意:如果其中一个国家没有共同日期,则所有国家列中将没有标记

【讨论】:

【解决方案2】:

试试

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each Number.From([Year])*100+Number.From([Month No])),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Country"}, {{"Count", each List.Max([Custom]), type number}}),
Maxcommon =  List.Min(Table.Group(#"Added Custom", {"Country"}, {{"Count", each List.Max([Custom]), type number}})[Count]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "latest common", each if [Custom]=Maxcommon  then 1 else 0),
#"Merged Queries" = Table.NestedJoin(#"Added Custom1" ,{"Country"},#"Grouped Rows",{"Country"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Count"}, {"Count"}),
#"Added Custom2" = Table.AddColumn(#"Expanded Table2", "latest month Flag", each if [Custom]=[Count] then 1 else 0),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Custom", "Count"})
in #"Removed Columns"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多