Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Creating Date Tables

As of today (10/26/18), what's the most convenient way to create date tables in BI? Apparently there used to be a feature whereby one only had to click a single button to drop a date table into the m...
  • edhans's avatar
    7 years ago

    IMHO, the fastest way is:

    1. Open a blank query in Power Query of Power BI
    2. type ={Number.From(#date(2018,1,1))..Number.From(#date(2018,12,31))}
    3. That will generate a series of numbers as a list
    4. Convert it to a table (upper left menu button.
    5. Convert the ABC123 type to date
    6. Rename to Date.
    7. Now you have a date table. Add columns as necessary (year, month, month name, etc) to make your date table suit your needs.
    8. Close and load.
    9. 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's avatar
    edhans
    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"