如何在 Google Data Studio 中将日期字段转换为数字?

How can I convert a date field to number in Google Data Studio?

在 Google Data Studio 中,我正在搜索将日期字段转换为数字字段的公式。

我一直在尝试这些选项,但它们都不起作用。

CAST(Date_field as NUMBER)

Invalid formula - Cannot cast from DATE to DOUBLE

Date_field+0

Invalid formula - Operator "+" doesn't support DATE + NUMBER. Operator "+" supports NUMBER + NUMBER.

datevalue() or value()也不存在。

精度:我正在寻找一种解决方案,无需更改数据源或连接中的原始日期字段。只有一个公式。

预期结果是这样的连续整数系列(零参考点无所谓,只要连续2天递增1即可。)

Date_field Expected_output
23 Feb 2022 44,615
22 Feb 2022 44,614
21 Feb 2022 44,613
2 Jan 2022 44,563
1 Jan 2022 44,562
31 Dec 2021 44,561
30 Dec 2021 44,560

Expected_output 值是 Microsoft Excel 和 Google 表格中基础日期的格式,它们使用 1900 date system, where day 1 in Excel is 01 Jan 1900 while in Google Sheets day 1 is 31 Dec 1899 (day 2 is 01 Jan 1900), the reason being that "Excel incorrectly assumes that the year 1900 is a leap year",因此1900 年 2 月 29 日存在于 Excel 但不存在于 Google 表格中(因此,下文将使用 1899 年 12 月 31 日)。

calculated field below recreates the values in the Expected_output field by using the DATETIME_DIFF function to find the difference between the values in the Date_field and fixed date, 31 Dec 1899 (explained above), that was created using the DATE 函数“创建一个年月日格式的日期”;添加 +1 以包含 n+1 天(以计算初始日期):

DATETIME_DIFF(Date_field, DATE(1899, 12, 31), DAY) + 1

Editable Google Data Studio Report (Embedded Google Sheets 数据源)和一张 GIF 来详细说明: