Is Today =True - Date column wrong Power Query code.

elliotstan

New Member
Joined
Feb 5, 2011
Messages
18
Hi All,
I have a powerquery script to create my date table. Think I copied it from powerpivotpro.com (great site)
Has been working ok but I just realised that the IsToday column is wrong.
Right now it is Wednesday the 9th of September but it thinks today is Tuesday the 9th of September.

below is the code I use

Any help appreciated.

Code:
let

    CreateDateTable = (StartDate, EndDate) =>

let

    /*StartDate=#date(2008,1,1),

    EndDate=#date(2019,12,31),*/

    //Create lists of month and day names for use later on

    MonthList = {"January", "February", "March", "April", "May", "June"

                 , "July", "August", "September", "October", "November", "December"},

    DayList = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},

    //Find the number of days between the end date and the start date

    NumberOfDates = Duration.Days(EndDate-StartDate),

    //Generate a continuous list of dates from the start date to the end date

    DateList = List.Dates(StartDate, NumberOfDates, #duration(1, 0, 0, 0)),

    //Turn this list into a table

    TableFromList = Table.FromList(DateList, Splitter.SplitByNothing(), {"Date"}

                     , null, ExtraValues.Error),

    //Caste the single column in the table to type date

    ChangedType = Table.TransformColumnTypes(TableFromList,{{"Date", type date}}),

    //Add custom columns for day of month, month number, year

    DayOfMonth = Table.AddColumn(ChangedType, "DayOfMonth", each Date.Day([Date])),

    MonthNumber = Table.AddColumn(DayOfMonth, "MonthNumberOfYear", each Date.Month([Date])),

    Year = Table.AddColumn(MonthNumber, "Year", each Date.Year([Date])),

    DayOfWeekNumber = Table.AddColumn(Year, "DayOfWeekNumber", each Date.DayOfWeek([Date])+1), 

    //Since Power Query doesn't have functions to return day or month names, 

    //use the lists created earlier for this

    MonthName = Table.AddColumn(DayOfWeekNumber, "MonthName", each MonthList{[MonthNumberOfYear]-1}),

    DayName = Table.AddColumn(MonthName, "DayName", each DayList{[DayOfWeekNumber]-1}),
    

    //Add a column that returns true if the date on rows is the current date

    IsToday = Table.AddColumn(DayName, "IsToday", each Date.IsInCurrentDay([Date]))



in
IsToday,
    #"Invoked FunctionCreateDateTable1" = CreateDateTable(#date(2008, 1, 1), #date(2019, 12, 31)),
    #"Filtered Rows" = Table.SelectRows(#"Invoked FunctionCreateDateTable1", each ([IsToday] = true))
in
    #"Filtered Rows"
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I think this is due to different calendar Settings: The code you've provided would deliver the correct results if the first day of the week would be sunday. But yours is probably monday. So try putting sunday at the end of the DayList.
 
Upvote 0
Hi elliotstan,
I'm late but for further cases you can use the second argument of Date.DayOfWeek function to be sure which day is the first day of week.
So the line should look like this below.
Code:
DayOfWeekNumber = Table.AddColumn(Year, "DayOfWeekNumber", each Date.DayOfWeek([Date], Day.Sunday)+1),
Additionally, you do not need lists (MonthList and DayList) if you use Date.ToText function. In this case you can choose language (culture) as you wish for the name of the month or day
Code:
MonthName = Table.AddColumn(DayOfWeekNumber, "MonthName", each Date.ToText([Date], "MMMM", "en-US")),

DayName = Table.AddColumn(MonthName, "DayName", each Date.ToText([Date], "dddd", "en-US")),

So, this part
Code:
    MonthList = {"January", "February", "March", "April", "May", "June"

                 , "July", "August", "September", "October", "November", "December"},

    DayList = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"},
is not needed.

Regards
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top