在Power BI的Power Query中选择每个月的最大日期。

huangapple go评论181阅读模式
英文:

Select the maximum day of every month in Power Query in Power BI

问题

我在Power BI中有以下数据集:

我想要为每个月/年仅选择具有最高日期的行(并删除其余行)。

在此示例中,将删除第4至第7行。

您有没有任何方法来解决这个问题?

谢谢,

最好

编辑:月份和年份

英文:

I have the following dataset in power BI :

在Power BI的Power Query中选择每个月的最大日期。

I'd like to select for every month/year, ONLY the rows with highest day (and delete the rest).

In this example, the rows 4-7 would be deleted

Do you have any idea how to approach this problem ?

Thank you,

Best

Edit : month AND year

答案1

得分: 1

你没有提到它,但你不是也需要检查年份吗?无论如何,一种方法是将日期拆分为年、月、日,找到年/月组合的最大日期,查看是否与该行上的日期匹配,然后进行筛选

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "YearMonth", each Date.Year([Date])*1000+Date.Month([Date])),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"YearMonth"}, {{"data", each 
        let a = List.Max(_[Date]),
        #"Added Custom2" = Table.AddColumn(_, "Custom", (x)=>if x[Date]<> a then "XX" else null),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom2", each [Custom] = null)
        in #"Filtered Rows"
    , type table [Date=nullable date]}}),
    #"Expanded Data" = Table.ExpandTableColumn(Table.SelectColumns(#"Grouped Rows",{ "data" }), "data", Table.ColumnNames(#"Changed Type"),Table.ColumnNames(#"Changed Type"))
in #"Expanded Data"

在Power BI的Power Query中选择每个月的最大日期。

英文:

You dont mention it, but don't you have to check year as well? Anyway, one way is to break it down to year, month, day, find the max day for the year/month combination, see if it matches the day on that row, then filter

let Source = Excel.CurrentWorkbook(){[Name=&quot;Table1&quot;]}[Content],
#&quot;Changed Type&quot; = Table.TransformColumnTypes(Source,{{&quot;Date&quot;, type date}}),
#&quot;Added Custom&quot; = Table.AddColumn(#&quot;Changed Type&quot;, &quot;YearMonth&quot;, each Date.Year([Date])*1000+Date.Month([Date])),
#&quot;Grouped Rows&quot; = Table.Group(#&quot;Added Custom&quot;, {&quot;YearMonth&quot;}, {{&quot;data&quot;, each 
    let a = List.Max(_[Date]),
    #&quot;Added Custom2&quot; = Table.AddColumn(_, &quot;Custom&quot;, (x)=&gt;if x[Date]&lt;&gt; a then &quot;XX&quot; else null),
    #&quot;Filtered Rows&quot; = Table.SelectRows(#&quot;Added Custom2&quot;, each [Custom] = null)
    in #&quot;Filtered Rows&quot;
, type table [Date=nullable date]}}),
#&quot;Expanded Data&quot; = Table.ExpandTableColumn(Table.SelectColumns(#&quot;Grouped Rows&quot;,{&quot;data&quot;}), &quot;data&quot;, Table.ColumnNames(#&quot;Changed Type&quot;),Table.ColumnNames(#&quot;Changed Type&quot;))
in #&quot;Expanded Data&quot;

在Power BI的Power Query中选择每个月的最大日期。

huangapple
  • 本文由 发表于 2023年6月15日 18:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定