This function returns
a value corresponding to a timestamp with the first millisecond of the
day contained in the time argument. The
default output format will be the TimestampFormat
set in the script.
Syntax:
DayStart(time[, [period_no[, day_start]])
Return data type: dual
Arguments
Argument
Description
time
The timestamp to evaluate.
period_no
period_no is an
integer, or expression that resolves to an integer, where the value 0 indicates the day that contains time.
Negative values in period_no indicate
preceding days and positive values indicate succeeding days.
day_start
To specify that days do not starting at midnight, indicate an offset as a
fraction of a day in day_start. For example, 0.125 to denote 3:00 AM.
In other words, to create the offset, divide the start time by 24 hours. For example, for a day to begin at 7:00 AM, use the fraction 7/24.
When to use it
The daystart() function is commonly used as part of an expression when the user would like the calculation to use the fraction of the day that has elapsed thus far. For example, it could be used to calculate the total wages earned by employees in the day so far.
These examples use the timestamp format 'M/D/YYYY h:mm:ss[.fff] TT'. The timestamp format is specified in the SET TimeStamp statement at the top of your data load script. Change the format in the examples to suit your requirements.
Function examples
Example
Result
daystart('01/25/2013 4:45:00 PM')
Returns 1/25/2013 12:00:00 AM.
daystart('1/25/2013 4:45:00 PM', -1)
Returns 1/24/2013 12:00:00 AM.
daystart('1/25/2013 16:45:00',0,0.5
)
Returns 1/25/2013 12:00:00 PM.
Regional settings
Unless otherwise specified, the examples in this topic use the following date format: MM/DD/YYYY. The date format is specified in the SET DateFormat statement in your data load script. The default date formatting may be different in your system, due to your regional settings and other factors. You can change the formats in the examples below to suit your requirements. Or you can change the formats in your load script to match these examples. For more information, see Modifying regional settings for apps and scripts.
Default regional settings in apps are based on the user profile. These regional format settings are not related to the language displayed in the Qlik Cloud user interface. Qlik Cloud will be displayed in the same language as the browser you are using.
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A simple dataset containing a list of dates, which is loaded into a table named Calendar.
The default TimeStampFormat system variable(M/D/YYYY h:mm:ss[.fff] TT) is used.
A preceding load which creates an additional field named SOD_timestamp, using the daystart() function.
Aside from the date, no additional parameters are provided to the function.
Load script
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff] TT';
Calendar:
Load
date,
daystart(date) as SOD_timestamp
;
Load
date
Inline
[
date
03/11/2022 1:47:15 AM
03/12/2022 4:34:58 AM
03/13/2022 5:15:55 AM
03/14/2022 9:25:14 AM
03/15/2022 10:06:54 AM
03/16/2022 10:44:42 AM
03/17/2022 11:33:30 AM
03/18/2022 12:58:14 PM
03/19/2022 4:23:12 PM
03/20/2022 6:42:15 PM
03/21/2022 7:41:16 PM
];
Results
Load the data and open a sheet. Create a new table and add these fields as dimensions:
date
SOD_timestamp
Results table
date
SOD_timestamp
03/11/2022 1:47:15 AM
3/11/2022 12:00:00 AM
03/12/2022 4:34:58 AM
3/12/2022 12:00:00 AM
03/13/2022 5:15:55 AM
3/13/2022 12:00:00 AM
03/14/2022 9:25:14 AM
3/14/2022 12:00:00 AM
03/15/2022 10:06:54 AM
3/15/2022 12:00:00 AM
03/16/2022 10:44:42 AM
3/16/2022 12:00:00 AM
03/17/2022 11:33:30 AM
3/17/2022 12:00:00 AM
03/18/2022 12:58:14 PM
3/18/2022 12:00:00 AM
03/19/2022 4:23:12 PM
3/19/2022 12:00:00 AM
03/20/2022 6:42:15 PM
3/20/2022 12:00:00 AM
03/21/2022 7:41:16 PM
3/21/2022 12:00:00 AM
As can be seen in the table above, the end of day timestamp is generated for each date in our dataset. The timestamp is in the format of the system variable TimestampFormat M/D/YYYY h:mm:ss[.fff] TT.
Example 2 - period_no
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
A dataset containing parking fines, which is loaded into a table named Fines. The dataset includes the following fields:
id
due_date
number_plate
amount
A preceding load using the daystart() function and supplying all three parameters: time, period_no, and day_start. This preceding load creates the following two new date fields:
An early_repayment_period date field, beginning seven days before the payment is due.
A late_penalty_period date field, beginning 14 days after the payment is due.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
due_date
early_repayment_period
late_penalty_period
Results table
due_date
early_repayment_period
late_penalty_period
02/11/2022 9:25:14 AM
2/4/2022 12:00:00 AM
2/25/2022 12:00:00 AM
03/25/2022 10:06:54 AM
3/18/2022 12:00:00 AM
4/8/2022 12:00:00 AM
04/14/2022 10:44:42 AM
4/7/2022 12:00:00 AM
4/28/2022 12:00:00 AM
06/28/2022 11:33:30 AM
6/21/2022 12:00:00 AM
7/12/2022 12:00:00 AM
08/15/2022 12:58:14 PM
8/8/2022 12:00:00 AM
8/29/2022 12:00:00 AM
11/16/2022 4:23:12 PM
11/9/2022 12:00:00 AM
11/30/2022 12:00:00 AM
01/17/2023 6:42:15 PM
1/10/2023 12:00:00 AM
1/31/2023 12:00:00 AM
03/22/2023 7:41:16 PM
3/15/2023 12:00:00 AM
4/5/2023 12:00:00 AM
The values of the new fields are in the TimestampFormat M/DD/YYYY tt. Because the function daystart() was used, the timestamp values are all the first millisecond of the day.
The early repayment period values are seven days before the due date, as a result of the second argument being passed in the daystart() function being negative.
The late repayment period values are 14 days after the due date, as a result of the second argument being passed in the daystart() function being positive.
Example 3 - day_start
Overview
Open the Data load editor and add the load script below to a new tab.
The load script contains:
The same dataset and scenario as the previous example.
The same preceding load as the previous example.
In this example, we set the working day to begin and end at 7:00 AM each day.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
due_date
early_repayment_period
late_penalty_period
Results table
due_date
early_repayment_period
late_penalty_period
02/11/2022
2/3/2022 7:00:00 AM
2/24/2022 7:00:00 AM
03/25/2022
3/17/2022 7:00:00 AM
4/7/2022 7:00:00 AM
04/14/2022
4/6/2022 7:00:00 AM
4/27/2022 7:00:00 AM
06/28/2022
6/20/2022 7:00:00 AM
7/11/2022 7:00:00 AM
08/15/2022
8/7/2022 7:00:00 AM
8/28/2022 7:00:00 AM
11/16/2022
11/8/2022 7:00:00 AM
11/29/2022 7:00:00 AM
01/17/2023
1/9/2023 7:00:00 AM
1/30/2023 7:00:00 AM
03/22/2023
3/14/2023 7:00:00 AM
4/4/2023 7:00:00 AM
The dates now have a timestamp of 7:00 AM because the value of the day_start argument which was passed into the daystart() function was 7/24. This sets the beginning of the day to 7:00 AM.
Because the due_date field does not have a timestamp, it is treated as 12:00 AM, which is thus still part of the previous day, since the days start and end at 7:00 AM. Therefore, the early repayment period for a fine due on February 11 begins on February 3 at 7:00 AM.
Example 4 - Chart object example
Overview
Open the Data load editor and add the load script below to a new tab.
This example uses the same dataset and scenario as the previous example.
However, only the original Fines table is loaded into the application, with the two additional due dates values being calculated in a chart object.
Load the data and open a sheet. Create a new table and add this field as a dimension: due_date.
To create the early_repayment_period field, create the following measure.
=daystart(due_date,-7,7/24)
To create the late_penalty_period field, create the following measure:
=daystart(due_date,14,7/24)
Results table
due_date
=daystart(due_date,-7,7/24)
=daystart(due_date,14,7/24)
02/11/2022 9:25:14 AM
2/4/2022 7:00:00 AM
2/25/2022 7:00:00 AM
03/25/2022 10:06:54 AM
3/18/2022 7:00:00 AM
4/8/2022 7:00:00 AM
04/14/2022 10:44:42 AM
4/7/2022 7:00:00 AM
4/28/2022 7:00:00 AM
06/28/2022 11:33:30 AM
6/21/2022 7:00:00 AM
7/12/2022 7:00:00 AM
08/15/2022 12:58:14 PM
8/8/2022 7:00:00 AM
8/29/2022 7:00:00 AM
11/16/2022 4:23:12 PM
11/9/2022 7:00:00 AM
11/30/2022 7:00:00 AM
01/17/2023 6:42:15 PM
1/10/2023 7:00:00 AM
1/31/2023 7:00:00 AM
03/22/2023 7:41:16 PM
3/15/2023 7:00:00 AM
4/5/2023 7:00:00 AM
The values of the new fields are in the TimestampFormat M/D/YYYY h:mm:ss[.fff] TT. Because the daystart() function was used, the timestamp values correspond to the first millisecond of the day.
The early repayment period values are seven days before the due date, since the second argument passed in the daystart() function was negative.
The late repayment period values are 14 days after the due date, since the second argument passed in the daystart() function was positive.
The dates have a timestamp of 7:00 AM because the value of the third argument passed into the daystart() function, day_start, was 7/24.
Load script
The load script is a sequence of statements that defines what data to load and how to link the different loaded tables. It can be generated with the Data manager, or with the Data load editor, where it also can be viewed and edited.
The term dataset is sometimes synonymous with table. It can refer to the original source table, the table after undergoing transformations, or the fact and dimension tables in a data mart.
It can also refer to a logical table, where there are several instance tables and views:
Current data
History, which holds previous versions of the table
A preceding load is a script construct that allows you to load from the following LOAD or SELECT statement without specifying that source. Preceding loads are often faster than resident loads.
A field contains values, loaded from a data source. At a basic level, a field corresponds to a column in a table. Fields are used to create dimensions and measures in visualizations.
A measure is a calculation base on one ore more aggregations. For example, the sum of sales is a single aggregation, while the sum of sales divided by the count of customers is a measure based on two aggregations.