# inlunarweektodate 脚本和图表功能

此函数用于判断 timestamp 是否位于截止以及包括 base\_date 最后毫秒的阴历周的某部分以内。Qlik Sense 中的农历周定义为将 1 月 1 日计算为一周的第一天，除一年的最后一周外，将正好包含七天。

语法：

InLunarWeekToDate (timestamp, base\_date, period\_no [, first\_week\_day])

返回数据类型： 布尔值

信息注释

在 Qlik Sense 中，布尔 true 值由 -1 表示，false 值由 0 表示。

`inlunarweektodate()` 函数的示例图表

`inlunarweektodate()` 函数的示例图表

`inlunarweektodate()`inlunarweektodate() 函数的示例图表 ![inlunarweektodate 函数的示例图，显示给定输入信息时函数将返回 TRUE 值的日期。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inlunarweektodate-overview.png)

`inlunarweektodate()` 函数作为农历周的终点。相比之下，`inlunarweek()` 函数决定 `base_date` 属于哪个农历周。例如，如果 `base_date` 是 1 月 5 日，那么 1 月 1 日到 1 月 5 号之间的任何时间戳都将返回布尔值结果 `TRUE`，而 1 月 6 日和 7 日以及之后的日期将返回布尔值结果 `FALSE`。

参数

| 参数 | 描述 |
| --- | --- |
| timestamp | 想要用来与 base\_date 进行比较的日期。 |
| base\_date | 日期用于计算阴历周的值。 |
| period\_no | 阴历周可通过 period\_no 偏移。period\_no 为整数，其中值 0 表示该阴历周包含 base\_date。period\_no 为负数表示前几个阴历星期，为正数表示随后的几个阴历星期。 |
| first\_week\_day | 偏移可以大小或小于零。这可以按指定的天数和/或某日内时间的小数对更改一年的开始。 |

## 适合使用 inlunarweektodate 的情况

`inlunarweektodate()` 函数返回布尔结果。通常，这种类型的函数将用作 IF 表达式中的条件。`inlunarweektodate()` 函数将在用户希望计算返回聚合或计算时使用，具体取决于评估日期是否发生在所讨论的一周的特定时段。

例如，`inlunarweektodate()` 函数可用于识别特定日期之前（包括特定日期）在特定周内制造的所有设备。

函数示例

| 示例 | 结果 |
| --- | --- |
| `inlunarweektodate('01/12/2013', '01/13/2013',<br>						0)` | 由于 `timestamp`、01/12/2013 的值， 属于 01/08/2013 至 01/13/2013 的周之内，返回 `TRUE`。 |
| `inlunarweektodate('01/12/2013', '01/11/2013',<br>						0)` | 由于 `timestamp` 的值晚于 `base_date` 的值，尽管这两个日期都属于 01/12/2012 之前的同一阴历周，返回 `FALSE`。 |
| `inlunarweektodate('01/12/2006', '01/05/2006',<br>						1)` | 返回 `TRUE`。将 `period_no` 的值指定为 1，表示 `base_date` 向前移动一周，因此 `timestamp` 的值属于阴历周的一部分。 |

`inlunarweektodate()` 函数通常与以下功能结合使用：

相关函数

| 函数 | 交互 |
| --- | --- |
| [lunarweekname 脚本和图表功能](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/lunarweekname.htm) | 此函数用于确定输入日期所在年份的农历周数。 |

## 区域设置

除非另有规定，本主题中的示例使用以下日期格式：MM/DD/YYYY。日期格式已经在数据加载脚本中的 `SET DateFormat` 语句中指定。由于区域设置和其他因素，系统中的默认日期格式可能有所不同。您可以更改以下示例中的格式以满足您的要求。或者，您可以更改加载脚本中的格式以匹配这些示例。有关更多信息，请参阅[修改应用程序和脚本的区域设置](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Apps/regional-settings-in-apps.htm)。

应用程序中的默认区域设置基于用户配置文件。这些区域格式设置与 Qlik Cloud 用户界面中显示的语言无关。Qlik Cloud 将以与您使用的浏览器相同的语言显示。

如果你是应用程序创建者，你可以为自己创建的应用程序设置默认区域。有关更多信息，请参阅[设置 Qlik Cloud Analytics 中创建应用程序和脚本所用的首选区域设置](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/UserInterface/Languages.htm#Setting)。

## 示例 1 – 没有其他参数

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 包含 1 月交易集的数据集，该数据集加载到名为 `Transactions` 的表中。使用默认 `DateFormat` 系统变量 MM/DD/YYYY。

* 创建一个字段 `in_lunar_week_to_date`，用于确定截至 1 月 10 日的农历周中发生了哪些交易。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions:
    Load
        *,
        inlunarweektodate(date,'01/10/2022', 0) as in_lunar_week_to_date
        ;
Load
*
Inline 
[
id,date,amount
8188,'1/10/2022',37.23
8189,'1/17/2022',17.17
8190,'1/26/2022',88.27
8191,'1/12/2022',57.42
8192,'1/19/2022',53.80
8193,'1/21/2022',82.06
8194,'1/1/2022',40.39
8195,'1/27/2022',87.21
8196,'1/11/2022',95.93
8197,'1/29/2022',45.89
8198,'1/31/2022',36.23
8199,'1/18/2022',25.66
8200,'1/23/2022',82.77
8201,'1/15/2022',69.98
8202,'1/4/2022',76.11
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `in_lunar_week_to_date`

结果表

| 日期 | in\_lunar\_week\_to\_date |
| --- | --- |
| 1/1/2022 | 0 |
| 1/4/2022 | 0 |
| 1/10/2022 | -1 |
| 1/11/2022 | 0 |
| 1/12/2022 | 0 |
| 1/15/2022 | 0 |
| 1/17/2022 | 0 |
| 1/18/2022 | 0 |
| 1/19/2022 | 0 |
| 1/21/2022 | 0 |
| 1/23/2022 | 0 |
| 1/26/2022 | 0 |
| 1/27/2022 | 0 |
| 1/29/2022 | 0 |
| 1/31/2022 | 0 |

`inlunarweektodate()` 函数，没有其他参数

`inlunarweektodate()` 函数，没有其他参数

`inlunarweektodate()`inlunarweektodate() 函数，没有其他参数 ![inlunarweektodate 函数的使用示例，该函数显示在给定输入信息的情况下，函数将返回值 TRUE 的日期范围。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inlunarweektodate-example-no-additional-arguments.png)

`in_lunar_week_to_date` 字段是在前置 Load 语句中使用 `inlunarweektodate()` 函数创建的，并将 `date` 字段、作为我们 `base_date` 的 1 月 10 日的硬编码日期和 0 的偏移值作为函数的参数传递。

因为农历周从 1 月 1 日开始，所以 1 月 10 日正好是从 1 月 8 日开始的农历周；因为我们正在使用 `inlunarweektodate()` 函数，所以农历周将在 10 号结束。因此，在 1 月份这两个日期之间发生的任何交易都将返回布尔值 `TRUE`。这在结果表中得到验证。

## 示例 2 – period\_no

[](https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/inlunarweektodate.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新选项卡。

加载脚本包含与第一个示例相同的数据集和场景。然而，在本例中，任务是创建一个字段 `2_lunar_weeks_later`，用于确定交易是否发生在日期 1 月 1 日的农历周后两周。

加载脚本

```
SET DateFormat='MM/DD/YYYY';
Transactions:
    Load
        *,
        inlunarweektodate(date,'01/10/2022', 2) as [2_lunar_weeks_later]
        ;
Load
*
Inline
[
id,date,amount
8188,'1/10/2022',37.23
8189,'1/17/2022',17.17
8190,'1/26/2022',88.27
8191,'1/12/2022',57.42
8192,'1/19/2022',53.80
8193,'1/21/2022',82.06
8194,'1/1/2022',40.39
8195,'1/27/2022',87.21
8196,'1/11/2022',95.93
8197,'1/29/2022',45.89
8198,'1/31/2022',36.23
8199,'1/18/2022',25.66
8200,'1/23/2022',82.77
8201,'1/15/2022',69.98
8202,'1/4/2022',76.11
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `2_lunar_weeks_later`

结果表

| 日期 | 2\_lunar\_weeks\_later |
| --- | --- |
| 1/1/2022 | 0 |
| 1/4/2022 | 0 |
| 1/10/2022 | 0 |
| 1/11/2022 | 0 |
| 1/12/2022 | 0 |
| 1/15/2022 | 0 |
| 1/17/2022 | 0 |
| 1/18/2022 | 0 |
| 1/19/2022 | 0 |
| 1/21/2022 | 0 |
| 1/23/2022 | -1 |
| 1/26/2022 | 0 |
| 1/27/2022 | 0 |
| 1/29/2022 | 0 |
| 1/31/2022 | 0 |

`inlunarweektodate()` 函数，period\_no 示例

`inlunarweektodate()` 函数，period\_no 示例

`inlunarweektodate()`inlunarweektodate() 函数，period\_no 示例 ![inlunarweektodate 函数的使用示例，该函数显示在给定输入信息的情况下，函数将返回值 TRUE 的日期范围。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inlunarweektodate-example-period-no.png)

在这种情况下，`inlunarweektodate()` 函数确定截至 1 月 10 日的农历周等于三天（1 月 8 日、9 日、10 日）。由于使用了值为 2 的 `period_no` 作为偏移参数，因此本农历周偏移了 14 天。因此，这定义了三天的农历周以包括 1 月 22 日、23 日和 24 日。1 月 22 日至 1 月 24 日之间发生的任何交易都将返回布尔值结果 `TRUE`。

## 示例 3 – first\_week\_day

[](https://help.qlik.com/en-US/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/DateAndTimeFunctions/inlunarweektodate.htm)

#### 加载脚本和结果

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 与第一个示例相同的数据集和场景。

* 使用默认 `DateFormat` 系统变量 MM/DD/YYYY。

* `first_week_date` 参数为 3。这将设定农历周从 1 月 3 日开始。

加载脚本

```
SET DateFormat='MM/DD/YYYY'; 

Transactions:
    Load
        *,
        inlunarweek(date,'01/10/2022', 0,3) as in_lunar_week_to_date
        ;
Load
*
Inline
[
id,date,amount
8188,'1/10/2022',37.23
8189,'1/17/2022',17.17
8190,'1/26/2022',88.27
8191,'1/12/2022',57.42
8192,'1/19/2022',53.80
8193,'1/21/2022',82.06
8194,'1/1/2022',40.39
8195,'1/27/2022',87.21
8196,'1/11/2022',95.93
8197,'1/29/2022',45.89
8198,'1/31/2022',36.23
8199,'1/18/2022',25.66
8200,'1/23/2022',82.77
8201,'1/15/2022',69.98
8202,'1/4/2022',76.11
];
```

结果

加载数据并打开工作表。创建新表并将这些字段添加为维度：

* `date`

* `in_lunar_week_to_date`

结果表

| 日期 | in\_lunar\_week\_to\_date |
| --- | --- |
| 1/1/2022 | 0 |
| 1/4/2022 | -1 |
| 1/10/2022 | -1 |
| 1/11/2022 | 0 |
| 1/12/2022 | 0 |
| 1/15/2022 | 0 |
| 1/17/2022 | 0 |
| 1/18/2022 | 0 |
| 1/19/2022 | 0 |
| 1/21/2022 | 0 |
| 1/23/2022 | 0 |
| 1/26/2022 | 0 |
| 1/27/2022 | 0 |
| 1/29/2022 | 0 |
| 1/31/2022 | 0 |

`inlunarweektodate()` 函数，first\_week\_day 示例

`inlunarweektodate()` 函数，first\_week\_day 示例

`inlunarweektodate()`inlunarweektodate() 函数，first\_week\_day 示例 ![inlunarweektodate 函数的使用示例，该函数显示在给定输入信息的情况下，函数将返回值 TRUE 的日期范围。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inlunarweektodate-example-first-week-date.png)

在本例中，因为 `inlunarweek()` 函数中使用了值为 3 的参数 `the first_week_date`，所以农历第一周将从 1 月 3 日到 1 月 10 日。由于 1 月 10 日也是 `base_date`，这两个日期之间的任何交易都将返回布尔值 `TRUE`。

## 示例 4 – 图表对象示例

#### 加载脚本和图表表达式

概述

打开数据加载编辑器，并将下面的加载脚本添加到新选项卡。

加载脚本包含与第一个示例相同的数据集和场景。

然而，在本例中，未更改的数据集被加载到应用程序中。确定交易是否发生在 1 月 10 日之前的农历周的计算被创建为应用程序图表对象中的度量。

加载脚本

```
SET DateFormat='MM/DD/YYYY';

Transactions: 
Load
*
Inline
[
id,date,amount
8188,'1/10/2022',37.23
8189,'1/17/2022',17.17
8190,'1/26/2022',88.27
8191,'1/12/2022',57.42
8192,'1/19/2022',53.80
8193,'1/21/2022',82.06
8194,'1/1/2022',40.39
8195,'1/27/2022',87.21
8196,'1/11/2022',95.93
8197,'1/29/2022',45.89
8198,'1/31/2022',36.23
8199,'1/18/2022',25.66
8200,'1/23/2022',82.77
8201,'1/15/2022',69.98
8202,'1/4/2022',76.11
];
```

结果

加载数据并打开工作表。创建新表并将该字段添加为维度： date。

创建以下度量：

=inlunarweektodate(date,'01/10/2022', 0)

结果表

| 日期 | =inlunarweektodate(date,'01/10/2022', 0) |
| --- | --- |
| 1/1/2022 | 0 |
| 1/4/2022 | 0 |
| 1/10/2022 | -1 |
| 1/11/2022 | 0 |
| 1/12/2022 | 0 |
| 1/15/2022 | 0 |
| 1/17/2022 | 0 |
| 1/18/2022 | 0 |
| 1/19/2022 | 0 |
| 1/21/2022 | 0 |
| 1/23/2022 | 0 |
| 1/26/2022 | 0 |
| 1/27/2022 | 0 |
| 1/29/2022 | 0 |
| 1/31/2022 | 0 |

inlunarweektodate() 函数，图表对象示例

inlunarweektodate() 函数，图表对象示例

inlunarweektodate()inlunarweektodate() 函数，图表对象示例 ![inlunarweektodate 函数的使用示例图，使用图表表达式确定交易是否发生在包含 1 月 10 日的农历周。](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Resources/Images/scripting-functions-inlunarweektodate-example-chart-object.png)

in\_lunar\_week\_to\_date 度量是在图表对象中使用 inlunarweektodate() 函数创建的，并将日期字段、作为我们的 base\_date 的 1 月 10 日的硬编码日期和 0 的偏移值作为函数的参数传递。

因为农历周从 1 月 1 日开始，所以 1 月 10 日正好是从 1 月 8 日开始的农历周。此外，因为我们正在使用 inlunarweektodate() 函数，所以农历周将在 10 号终止。因此，在 1 月份这两个日期之间发生的任何交易都将返回布尔值 TRUE。这在结果表中得到验证。

## 示例 5 – 场景

#### 加载脚本和图表表达式

概述

打开数据加载编辑器，并将下面的加载脚本添加到新部分。

加载脚本包含：

* 加载到名为 Products 的表中的数据集。

* 包含产品 ID、制造日期和成本价格的信息。

已确定，由于设备错误，1 月 12 日的农历周内生产的产品存在缺陷。该问题于 1 月 13 日得到解决。最终用户想要一个图表对象，它按周显示生产的产品是“有缺陷”还是“无缺陷”的状态，以及该周生产的产品的成本。

加载脚本

```
SET TimestampFormat='M/D/YYYY h:mm:ss[.fff]';

Products:
Load
*
Inline 
[
product_id,manufacture_date,cost_price
8188,'01/02/2022 12:22:06',37.23
8189,'01/05/2022 01:02:30',17.17
8190,'01/06/2022 15:36:20',88.27
8191,'01/08/2022 10:58:35',57.42
8192,'01/09/2022 08:53:32',53.80
8193,'01/10/2022 21:13:01',82.06
8194,'01/11/2022 00:57:13',40.39
8195,'01/12/2022 09:26:02',87.21
8196,'01/13/2022 15:05:09',95.93
8197,'01/14/2022 18:44:57',45.89
8198,'01/15/2022 06:10:46',36.23
8199,'01/16/2022 06:39:27',25.66
8200,'01/17/2022 10:44:16',82.77
8201,'01/18/2022 18:48:17',69.98
8202,'01/26/2022 04:36:03',76.11
8203,'01/27/2022 08:07:49',25.12
8204,'01/28/2022 12:24:29',46.23
8205,'01/30/2022 11:56:56',84.21
8206,'01/30/2022 14:40:19',96.24
8207,'01/31/2022 05:28:21',67.67
];
```

结果

执行以下操作：

1. 加载数据并打开工作表。新建表格。

2. 创建维度以显示周名称：

   =weekname(manufacture\_date)

3. 接下来，创建一个维度，其使用 inlunarweektodate() 函数识别哪些产品有缺陷，哪些无缺陷：

   =if(inlunarweektodate(manufacture\_date,makedate(2022,01,12),0),'Defective','Faultless')

4. 创建一个度量来求对产品的 cost\_price 求和：

   =sum(cost\_price)

5. 将度量的数字格式设置为金额。

结果表

| =lunarweekname(manufacture\_date) | =if(InLunarWeekToDate(manufacture\_date,makedate(2022,01,12),0),'Defective','Faultless') | =Sum(cost\_price) |
| --- | --- | --- |
| 2022/01 | Faultless | $142.67 |
| 2022/02 | Defective | $320.88 |
| 2022/02 | Faultless | $141.82 |
| 2022/03 | Faultless | $214.64 |
| 2022/04 | Faultless | $147.46 |
| 2022/05 | Faultless | $248.12 |

inlunarweektodate() 函数在评估每个产品的制造日期时返回布尔值。对于返回布尔值 TRUE 的产品，它将产品标记为 ‘Defective’。对于返回值为 FALSE 的任何产品（因此在截至 1 月 12 日的农历周内未生产），它将产品标记为 ‘Faultless’。