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,
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
- edhans5 years agoCommunity Champion
Yes. Just wrap that result with Date.StartOfMonth() and it will go back to Sept 1. So it would be Date.StartOfMonth(Date.AddMonths(....