如何在画面中分配@variables

How to assign @variables in tableau

我试图在 Tableau 中使用以下内容来声明日期变量,稍后我在 SQL 代码中调用它们。

但我无法在 Tableau 中执行相同的操作。你能help/guide我在正确的方向使用这些功能吗

-- Declare the parameter dates and get the start date and date for all queries.
Declare @ParamDate as date = '2018-12-31';
Declare @Trailing90Days as date = DATEADD(DAY,-90,GETDATE());
Declare @MonthStartDate as date = DATEADD(DAY, 1, EOMONTH(@ParamDate, -1));
Declare @MonthEndDate as date = EOMONTH(@ParamDate);
Declare @YearStartDate as date = DATEADD(yy, DATEDIFF(yy, 0, @ParamDate), 0);
Declare @YearEndDate as date = DATEADD (dd, -1, DATEADD(yy, DATEDIFF(yy, 0, @ParamDate) +1, 0));
Declare @YoYMonthStartDate as date = DATEADD(YEAR, -1, @MonthStartDate);
Declare @YoYMonthEndDate as date = DATEADD(YEAR, -1, @MonthEndDate);
Declare @MoMMonthStartDate as date = DATEADD(MONTH, -1, @MonthStartDate);
Declare @MoMMonthEndDate as date = DATEADD(MONTH, -1, @MonthEndDate);
Declare @QoQMonthStartDate as date = DATEADD(Quarter, -1, @MonthStartDate);
Declare @QoQMonthEndDate as date = DATEADD(Quarter, -1, @MonthEndDate);

select 
        @ParamDate as ParamDate, 
        @MonthStartDate as MonthStartDate, 
        @MonthEndDate as MonthEndDate,
        @YearStartDate as YearStartDate,
        @YearEndDate as YearEndDate,
        @YoYMonthStartDate as YoYMonthStartDate,
        @YoYMonthEndDate as YoYMonthEndDate,
        @MoMMonthStartDate as MoMMonthStartDate,
        @MoMMonthEndDate as MoMMonthEndDate,
        @QoQMonthStartDate as QoQMonthStartDate,
        @QoQMonthEndDate as QoQMonthEndDate,
        @Trailing90Days as Trailing90Days

我假设您正在尝试使用自定义 SQL - 您实际上并没有这么说,但代码和错误消息表明了这一点。

Tableau "wraps" 在 select 中自定义 SQL,如下所示:

SELECT * FROM
( <custom sql goes here> ) a

这就是为什么您的代码无法正常工作的原因。

此外,您的参数在 SQL 本身内进行了硬编码。我猜您会在适当的时候用 Tableau 参数替换它。

所以简短的回答是,你不能像你想要的那样使用 DECLARE。 长答案是,无需在 SQL 中声明变量,您只需在自定义 SQL.

中始终引用参数值即可

尽管如此,请尽量不要使用自定义 SQL,这只会让 Tableau 比应有的速度慢,因为当它可能知道有更快的选项时,您强迫它 运行 您的代码.

将所有代码放在 Initial SQL 中。这是您的连接设置中的一个选项。最终声明应将您的数据推送到临时 table.

然后创建自定义 sql 查询:

Select *
From #temp