Forum Discussion
Creating Date Tables
- 7 years ago
IMHO, the fastest way is:
- Open a blank query in Power Query of Power BI
- type ={Number.From(#date(2018,1,1))..Number.From(#date(2018,12,31))}
- That will generate a series of numbers as a list
- Convert it to a table (upper left menu button.
- Convert the ABC123 type to date
- Rename to Date.
- Now you have a date table. Add columns as necessary (year, month, month name, etc) to make your date table suit your needs.
- Close and load.
- Right-click on it and mark it as a date table.
If you have source data with dates in it, get fancy and find the earliest date in your data, then make row #2 above be Jan 1, YYYY where YYYY is the earliest date in your dataset,
- 5 years ago
So if your original line is like this, you just need to use some functions to determine the dates vs hardcoding.
={Number.From(#date(2018,1,1))..Number.From(#date(2018,12,31))}This will always give you a rolling 6 months. I've inserted a lot of line feeds to make the formulas a bit easier to read, but you could type that Source line all on one line. The key to all of this is DateTime.LocalNow() - that is equivalent to @NOW() in Excel - the current date and time from the system clock.
let Source = { Number.From( Date.AddMonths( DateTime.Date( DateTime.LocalNow() ), -6 ) ).. Number.From( DateTime.Date( DateTime.LocalNow() ) ) }, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}) in #"Changed Type"
IMHO, the fastest way is:
- Open a blank query in Power Query of Power BI
- type ={Number.From(#date(2018,1,1))..Number.From(#date(2018,12,31))}
- That will generate a series of numbers as a list
- Convert it to a table (upper left menu button.
- Convert the ABC123 type to date
- Rename to Date.
- Now you have a date table. Add columns as necessary (year, month, month name, etc) to make your date table suit your needs.
- Close and load.
- Right-click on it and mark it as a date table.
If you have source data with dates in it, get fancy and find the earliest date in your data, then make row #2 above be Jan 1, YYYY where YYYY is the earliest date in your dataset,
- Anonymous7 years agoNot applicable
Thanks for the tip. However, do you know if there was ever an automatic date table feature in BI? I know for sure there was a date table you could drop into Power Pivot for Excel 2016.
- edhans7 years agoCommunity Champion
It will do automatic dates but you have little control over it. I 100% of the time disable the automatic date options in Power BI and roll my own date table.
Where in Excel 2016 can you do an automatic date table? Short of the CALENDARAUTO() DAX function. You could also do this in DAX with the CALENDAR() function, but I prefer to create my tables in Power Query. They are more compact. If you do it in DAX you have calculated columns, which I try to avoid.
And CALENDARAUTO() is very dangerous. If you have somethign like a marketing campaign table with bogus expiration dates like 12/31/2999, which is actually not rare, CALENDARAUTO() will create a full calender table 900 years out!
If you are talking about another way to do an automaticl calendar in Excel 2016, I'd be interested in hearing it.
- Anonymous7 years agoNot applicable
Here's a link to an edX class I took about a year ago. This video shows adding date tables directly within PowerPivot in Excel 2016.
If the link doesn't work (or if you don't have a login for edX), in the PowerPivot window, go to the Design ribbon, and there is an option under the Calendars section for new date table.
- Anonymous5 years agoNot applicable
edhans I've got a new adaptation I need.
I have created your mquery date list, but instead of hard coded dates, I want to bring in the previous 6 calendar months, up to and including today.
Go.
Jemma 🙂
- edhans5 years agoCommunity Champion
So if your original line is like this, you just need to use some functions to determine the dates vs hardcoding.
={Number.From(#date(2018,1,1))..Number.From(#date(2018,12,31))}This will always give you a rolling 6 months. I've inserted a lot of line feeds to make the formulas a bit easier to read, but you could type that Source line all on one line. The key to all of this is DateTime.LocalNow() - that is equivalent to @NOW() in Excel - the current date and time from the system clock.
let Source = { Number.From( Date.AddMonths( DateTime.Date( DateTime.LocalNow() ), -6 ) ).. Number.From( DateTime.Date( DateTime.LocalNow() ) ) }, #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"Column1", type date}}) in #"Changed Type"- Anonymous5 years agoNot applicable
edhans thanks for getting back to me.
This is almost perfect. I need the full calendar six months, not a rolling 6 months. So I want it to work out that it should start on the 1st September, not 26th September. Is that possible?
Jemma
- MAAbdullah_475 years agoHelper V
Hi edhans
What do you means by (4) Convert it to a table (upper left menu button , this not clear senince could you eloberate it? - MAAbdullah_475 years agoHelper V
Hi edhans
What do you means by "4. Convert it to a table (upper left menu button." please eloberate it is not clear?
- edhans5 years agoCommunity Champion
The first 3 steps generate a List, not a table, so it looks like this:
- Anonymous4 years agoNot applicable
Signed in to say thank you!
- Anonymous3 years agoNot applicable
I have done this precisely as described above, but when I mark it as date table in the main report I am no longer able to create a Date hierarchy. What am I doing wrong?
- edhans3 years agoCommunity Champion
right-click on a date field and select Create Hierarchy, then right-click on a second date field and add to that hierarchy, and so on.
- Anonymous3 years agoNot applicable
Ah right, ok.
But why does making a Field the date table remove the date hierarchy?