Forum Discussion
How to Include "Time" in Date Hierarchy
Yep, mechanix85 has the right idea here. Way easier than what I posted.
Keep in mind that you'll need to do a hotswap on that date column in subsequent lines that try to run Date-based functions, because at this point, it's a DateTime (alternatively, use a new column called Date generated from the Date part of the DateTime). From there, you can isolate your date and time into new columns if you want, or just leave it as-is.
Using mechanix85's suggestion, the final formula could look something like this:
let fnDateTable = (StartDate as datetime, EndDate as datetime, optional Culture as nullable text) as table =>
let
Source = List.DateTimes(StartDate, DayCount , #duration (0,0,30,0)),
DayCount = Duration.TotalMinutes(Duration.From (EndDate - StartDate))+1,TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),
ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type datetime}}),
RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "DateTime"}}),
InsertDate = Table.AddColumn(RenamedColumns, "Date", each Date.From([DateTime]), type date),
InsertTime = Table.AddColumn(InsertDate, "Time", each Time.From([DateTime]), type time),
InsertYear = Table.AddColumn(InsertTime , "Year", each Date.Year([Date]),type text),
InsertQuarterNum = Table.AddColumn(InsertYear, "Quarter Num", each Date.QuarterOfYear([Date])),
InsertQuarter = Table.AddColumn(InsertQuarterNum, "Quarter", each "Q" & Number.ToText([Quarter Num])),
InsertMonth = Table.AddColumn(InsertQuarter, "Month Num", each Date.Month([Date]), type text),
InsertStartOfMonth = Table.AddColumn(InsertMonth, "StartOfMonth", each Date.StartOfMonth([Date]), type date),
InsertEndOfMonth = Table.AddColumn(InsertStartOfMonth, "EndOfMonth", each Date.EndOfMonth([Date]), type date),
InsertDay = Table.AddColumn(InsertEndOfMonth, "DayOfMonth", each Date.Day([Date])),
InsertDayInt = Table.AddColumn(InsertDay, "DateInt", each [Year]*10000 + [Month Num]*100 + [DayOfMonth]),
InsertMonthName = Table.AddColumn(InsertDayInt, "Month", each Date.ToText([Date], "MMMM", Culture), type text),
InsertShortMonthName = Table.AddColumn(InsertMonthName, "Month short", each Date.ToText([Date], "MMM", Culture), type text),
InsertCalendarMonth = Table.AddColumn(InsertShortMonthName, "Month Year", each [Month short]& " " & Number.ToText([Year]),type text),
InsertCalendarQtr = Table.AddColumn(InsertCalendarMonth, "Quarter Year", each "Q" & Number.ToText([Quarter Num]) & " " & Number.ToText([Year]), type text),
InsertDayWeek = Table.AddColumn(InsertCalendarQtr, "Weekday Num", each Date.DayOfWeek([Date])),
InsertDayName = Table.AddColumn(InsertDayWeek, "Weekday", each Date.ToText([Date], "dddd", Culture), type text),
InsertShortDayName = Table.AddColumn(InsertDayName, "Weekday short", each Date.ToText([Date], "ddd", Culture), type text),
InsertWeekEnding = Table.AddColumn(InsertShortDayName , "EndOfWeek", each Date.EndOfWeek([Date]), type date),
InsertWeekNumber= Table.AddColumn(InsertWeekEnding, "Week Num", each Date.WeekOfYear([Date])),
InsertMonthWeekNumber= Table.AddColumn(InsertWeekNumber, "WeekOfMonth Num", each Date.WeekOfMonth([Date])),
InsertMonthnYear = Table.AddColumn(InsertMonthWeekNumber,"Month-YearOrder", each [Year]*10000 + [Month Num]*100),
InsertQuarternYear = Table.AddColumn(InsertMonthnYear,"Quarter-YearOrder", each [Year]*10000 + [Quarter Num]*100),
ChangedType1 = Table.TransformColumnTypes(InsertQuarternYear,{{"Quarter-YearOrder", Int64.Type},{"Week Num", Int64.Type},{"WeekOfMonth Num", Int64.Type},{"Quarter", type text},{"Year", type text},{"Month-YearOrder", Int64.Type}, {"DateInt", Int64.Type}, {"DayOfMonth", Int64.Type}, {"Month Num", Int64.Type}, {"Quarter Num", Int64.Type}, {"Weekday Num", Int64.Type}})
in
ChangedType1
in
fnDateTable
From a design point of view I would have gone with 2 seperate tables, a date table and a seperate time table.
You will end up with the same result from a reporting point of view and your data model will be smaller and simplier.
- MJC19 years agoFrequent Visitor
Hi OpenDataLab,
I like your idea very much.
I would like to try your approach out.
Do you have the sript of for the Date Table and for the Time Table that can be shared?
I look forward to hearing from you.
Cheers,
MJC
- OpenDataLab9 years ago
Helper II
Here are links to a typical date dimension and time dimension:
In Power Query you will need to split your date time field into a date field and a time field you can use the parse function to do this:
- MJC19 years agoFrequent Visitor
Hi OpenDataLab,
I am now trying this option but I am having some problems.
I can load both power queries as per your script below. (just FYI I select the dates from 01/01/2017 to 31/12/2017)
Now I am having problems when I load my date. Essentially my CSV file has two columns. Column 1 Header is "Date" and contains dates of the following format 01/01/2017 and Column 2 Header is "XYZ" and contains numbers as this is my dataset. My data set starts at 01/01/2017 and finishes 24/07/2017 and moves on a 30 min time step. When I try to load my get i get a message sayin that there are problems with rows. Perhaps this has to do with the Parse thing you mentioned. I'd be greatful if you could clarify how I could solve this or consider the parse. I need a little more info than that on your figure. I am a real novice.
Could it also be because the dates on my data are for half year whereas the range i selected in the power query is for entire year.
Many thanks for your help in advance.