-
Notifications
You must be signed in to change notification settings - Fork 4
/
DateTable_FromDates.pq
40 lines (35 loc) · 1.67 KB
/
DateTable_FromDates.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let
/*
Input:
Table with at least a [Date] column
output:
Adds columns for: [Year], [Month], [Day], [Week of Year], [Date Id] key
future:
optionally add time when datetime is the source
*/
DateTable.FromDates_Impl = () =>
error Error.Record("NYI", "First write ListAll.Dates.pq"),
old_version_Date.TableFromDates = (source as table) as table =>
let
// isDatetime = ..., // if datetime, or datetimezone,
source =
if Table.HasColumns( source, {"Date"} )
then source
else error Error.Record(
"InvalidArgument", "Expects a table with a column named 'Date'", source
),
base =List.ContinuousDates( source[Date] ),
col_Year = Table.AddColumn(base, "Year", (_) as number => Date.Year([Date]), Int64.Type),
col_Month = Table.AddColumn(col_Year, "Month", (_) as number => Date.Month([Date]), Int64.Type),
col_Day = Table.AddColumn(col_Month, "Day", (_) as number => Date.Day([Date]), Int64.Type),
col_WeekOfYear = Table.AddColumn(col_Day, "Week of Year", (_) as number => Date.WeekOfYear([Date]), Int64.Type),
col_Index = Table.AddIndexColumn(col_WeekOfYear, "Date Id", 0, 1, Int64.Type),
verifyDistict = Table.IsDistinct(col_Index, {"Date"}),
Final =
if verifyDistict then col_Index
else error Error.Record(
"TableNotDistinct", "Using column 'date'", col_Index )
in
Final
in
Value.ReplaceType( DateTable.FromDates_Impl, DateTable.FromDates_Type1 )