如何在Power Query中生成两个日期之间每个月的第一天的列表。

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

How to generate a list of first day of each month between the two dates in power query

问题

在Power Query中,我尝试根据现有的"StartOfDate"和"EndOfDate"列生成行列表。我想要添加一个新列,其中包含如下表所示的日期列表。

StartOfDate EndOfDate New Column
2023/1/1 2023/2/15 列表
2023/2/3 2023/4/2 列表

在展开列表后,我只需要在"StartOfDate"和"EndOfDate"之间的每个月的第一天。

StartOfDate EndOfDate New Column
2023/1/1 2023/2/15 2023/1/1
2023/1/1 2023/2/15 2023/2/1
2023/2/15 2023/4/2 2023/2/1
2023/2/15 2023/4/2 2023/3/1
2023/2/15 2023/4/2 2023/4/1

我知道我可以通过{Number.From([StartOfDate])..Number.From([EndOfDate])}生成一个列表,然后通过展开列表获取两个日期之间的所有日期。

但我只需要在日期之间的每个月的第一天...

而且,由于表中已经有很多行,我不想在查询中创建一个步骤来展开所有日期的列表。

有人知道如何做到这一点吗?
谢谢您提前的帮助。

英文:

in power query I'm trying to generate list of rows depending on the existing columns StartOfDate and EndOfDate.
I want to add a New Column which has a list of dates like below table.

StartOfDate EndOfDate New Column
2023/1/1 2023/2/15 List
2023/2/3 2023/4/2 List

After expanding the list, I only need the first day of months between the StartOfDate and EndOfDate.

StartOfDate EndOfDate New Column
2023/1/1 2023/2/15 2023/1/1
2023/1/1 2023/2/15 2023/2/1
2023/2/15 2023/4/2 2023/2/1
2023/2/15 2023/4/2 2023/3/1
2023/2/15 2023/4/2 2023/4/1

I know I can just generate a list by {Number.From([StartOfDate])..Number.From([EndOfDate])} and then get all the dates between the two date by expanding the list.

But I only need the first day of each month between the dates...

And, since there are already many rows in the table, I do not want to create a step in the query to expand lists of all the dates.

Does anyone know how to to this?
Thank you in advance.

答案1

得分: 4

添加列,自定义列,

=List.Distinct(List.Transform({Number.From([StartOfDate])..Number.From([EndOfDate])}, each Date.StartOfMonth(Date.From(_))))

然后使用新列上方的箭头展开列为行。

英文:

Add column, custom column,

 =List.Distinct(List.Transform({Number.From([StartOfDate])..Number.From([EndOfDate])}, each Date.StartOfMonth(Date.From(_))))

then expand the column into rows using the arrows atop the new column

答案2

得分: 2

仅供娱乐,另一种使用 List.Generate() 的方法:

=let s=Date.StartOfMonth([StartOfDate]), e=[EndOfDate] in List.Generate(()=>s, each _<=e, each Date.AddMonths(_,1))
英文:

Just for fun, an alternative using List.Generate():

=let s=Date.StartOfMonth([StartOfDate]), e=[EndOfDate] in List.Generate(()=&gt;s, each _&lt;=e, each Date.AddMonths(_,1))

huangapple
  • 本文由 发表于 2023年6月8日 19:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431472.html
匿名

发表评论

匿名网友

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

确定