问题
I want to create an extra column to my dataset. This needs to be done by using M in PowerQuery. I have the weeknumber, dayoftheweek and year available.
This can be done by using the following formula in Excel. I need something similar but then in M.
Date = DATE([year],1,-2)-WEEKDAY(DATE([year],1,3))+[week]*7 + [day]-1
For example: Week 2, Day 3, Year 2019 should be: 9-1-2019 (In #dd-mm-yyyy#)
Thanks in advance
回答1:
This will do it
= 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) )
回答2:
If you wanted to create your own function, it might be something like:
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,
It takes three arguments: week of year, day of week and year (basically all the information you had available).
If you wanted to add it to a table, you could try something like:
Table.AddColumn(someTable, "newColumnName", each dateFromWeekDayYear([weekColumn], [dayColumn], [yearColumn]))
(Provided that your table someTable
contained the columns weekColumn
, dayColumn
, yearColumn
).
来源:https://stackoverflow.com/questions/54159065/get-date-from-weeknumber-dayofweek-and-year-powerquery-m