Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在Power query的自定义列中查找max函数_Excel_Powerbi_Powerquery - Fatal编程技术网

Excel 在Power query的自定义列中查找max函数

Excel 在Power query的自定义列中查找max函数,excel,powerbi,powerquery,Excel,Powerbi,Powerquery,我正在尝试为电源查询中的国旗创建两个自定义列。我正在尝试根据县、年、月查找国旗中的最新月份以及所有国家/地区可用的最新公共月份。数据如下所示: 我试过以下方法…: =var year_x=CALCULATE(最大([year]),ALLEXCEPT('tblename','tablename'[Country])) var month_x=计算(最大([月号]),过滤器(ALLEXCEPT('tablename','tablename'[CountryI]),'tablename'[Year]

我正在尝试为电源查询中的国旗创建两个自定义列。我正在尝试根据县、年、月查找国旗中的最新月份以及所有国家/地区可用的最新公共月份。数据如下所示:

我试过以下方法…: =var year_x=CALCULATE(最大([year]),ALLEXCEPT('tblename','tablename'[Country])) var month_x=计算(最大([月号]),过滤器(ALLEXCEPT('tablename','tablename'[CountryI]),'tablename'[Year]=Year_x))

返回月份

但这并不能解决我的问题,因为我想在power query中创建一个自定义标志。我知道我们可以在power BI中这样做…但在Excel power query中不知道

请帮助我找到此选项的一个选项

尝试:

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"
原始数据

结果

*注意:如果其中一个国家/地区没有共同的日期,则所有国家/地区列中将不会显示任何标志

请尝试:

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"
原始数据

结果

*注意:如果其中一个国家/地区没有共同的日期,则所有国家/地区列中将不会显示任何标志

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"
试一试


这看起来像是dax,而不是power queryI尝试过的度量……但我想构建一个自定义列,如您根据县、年、月编写的表格所示。但是您是否只想考虑所有国旗可用的最近一个月的月份?否则,当其他国家没有
11/2020
时,澳大利亚的逻辑是什么?Shi Ron.谢谢…刚刚编辑了表格..第二个标志是全国最新的公共月..我正在考虑这个最新的公共月的年份和月份..谢谢你的回答..我正在尝试使用你的代码..你可以吗republishThat看起来像dax,而不是power queryI尝试过的度量……但我想构建一个自定义列,如您在编写的表中所示,以县、年、月为基础。但是您是否只想考虑所有国旗可用的最近一个月的月份?否则,当其他国家没有
11/2020
时,澳大利亚的逻辑是什么?Shi Ron.谢谢…刚刚编辑了表格..第二个标志是全国最新的公共月..我正在考虑这个最新的公共月的年份和月份..谢谢回答..我正在尝试使用你的代码..你能重新发布吗