从星期数,星期几和年份中获取日期PowerQuery M

时间:2019-01-12 11:16:41

标签: excel powerquery m

我想为我的数据集创建一个额外的列。这需要通过在PowerQuery中使用M来完成。我有星期几,星期几和年份。

这可以通过在Excel中使用以下公式来完成。我需要类似的东西,但是在M中。

Date = DATE([year],1,-2)-WEEKDAY(DATE([year],1,3))+[week]*7 + [day]-1

例如:2019年的第2周,第3天,应为:9-1-2019(在#dd-mm-yyyy#中)

预先感谢

2 个答案:

答案 0 :(得分:1)

如果您想创建自己的函数,则可能类似于:

dateFromWeekDayYear = (WeekOfYear as number, DayOfWeek as number, Year as number) as date =>
    let
        endOfFirstWeekOfYear = Date.EndOfWeek(#date(Year, 1, 1), Day.Monday), // Consumes one week from "WeekOfYear"
        addRemainingWeeks = Date.AddWeeks(endOfFirstWeekOfYear, WeekOfYear - 2), // Subtract 2 because consumed one week above and because 0-based    
        addDays = Date.AddDays(addRemainingWeeks, DayOfWeek) // Don't need to make 0-based as previous step gives us end of prior week.
    in
        addDays,

它需要三个参数:一年中的一周,一周中的一天和一年(基本上是您拥有的所有信息)。

如果要将其添加到表中,可以尝试以下操作:

Table.AddColumn(someTable, "newColumnName", each dateFromWeekDayYear([weekColumn], [dayColumn], [yearColumn]))

(假设您的表someTable包含列weekColumndayColumnyearColumn)。

答案 1 :(得分:1)

这会做到

= Table.AddColumn(Source, "Custom", each Date.From(Number.From(Date.AddDays(Date.FromText("1/1/"&Number.ToText([year])),-3))-Date.DayOfWeek(Date.FromText("1/3/"&Number.ToText([year])))-1+[week]*7+[day]-1) )