Tableau - 计算日期小于其他数据源的值的平均值

时间:2016-01-12 20:20:57

标签: tableau

我正在尝试计算Tableau中列的平均值,除了问题是我尝试使用来自其他数据源的单个日期值(基于过滤器)来仅计算考试日期的平均值< =来自其他来源的过滤日期值。

注意:参数在这里对我不起作用,因为新的日期值会不断添加到集合中。

我尝试了很多不同的方法,但最简单的方法是尝试使用计算字段从其他数据源中提取过滤的考试日期。

它成功地可以提取过滤日期,但公式无法按预期工作。计算的2个版本如下:

IF DATE(ATTR([Exam Date])) <= DATE(ATTR([Averages (Tableau Test Scores)].[Updated])) THEN AVG([Raw Score]) END

IF DATEDIFF('day', DATE(ATTR([Exam Date])), DATE(ATTR([Averages (Tableau Test Scores)].[Updated]))) > 1 THEN AVG([Raw Score]) END

基本上,我在SQL Server中寻找相同的东西:

SELECT AVG([Raw Score]) WHERE ExamDate <= (Filtered Exam Date)

在工作簿下面,它显示了我想要完成的一个示例。目前它返回所有空白,可能是由于我在计算中尝试使用的多对一比较。

非常感谢任何反馈!

Tableau Test Exam Workbook

2 个答案:

答案 0 :(得分:1)

我能够通过使用自定义SQL将表连接在一起并根据我的条件计算平均值来解决这个问题,以获得我想要的列结果。

在Tableau中直接拥有此功能仍然很棒,但无论如何完成任务。

编辑:

SELECT 
[AcademicYear]
,[Discipline]
--Get the number of student takers
,COUNT([Id]) AS [Students (N)]
--Get the average of the Raw Score
,CAST(AVG(RawScore) AS DECIMAL(10,2)) AS [School Mean]
--Get the number of failures based on an "adjusted score" column
,COUNT([AdjustedScore] < 70 THEN 1 END) AS [School Failures]
--This is the column used as the cutoff point for including scores
,[Average_Update].[Updated]
FROM [dbo].[Average] [Average]

FULL OUTER JOIN [dbo].[Average_Update] [Average_Update] ON ([Average_Update].[Id] = [Average].UpdateDateId)

--The meat of joining data for accurate calculations
FULL OUTER JOIN (
SELECT DISTINCT S.[Id], S.[LastName], S.[FirstName], S.[ExamDate], S.[RawScoreStandard], S.[RawScorePercent], S.[AdjustedScore], S.[Subject], P.[Id] AS PeriodId
FROM [StudentScore] S
FULL OUTER JOIN
(
--Get only the 1st attempt
SELECT DISTINCT [NBOMEId], S2.[Subject], MIN([ExamDate]) AS ExamDate
FROM [StudentScore] S2
GROUP BY [NBOMEId],S2.[Subject]
) B
ON S.[NBOMEId] = B.[NBOMEId] AND S.[Subject] = B.[Subject] AND S.[ExamDate] = B.[ExamDate]
--Group in "Exam Periods" based on the list of periods w/ start & end dates in another table.
FULL OUTER JOIN [ExamPeriod] P 
ON  S.[ExamDate] = P.PeriodStart AND S.[ExamDate] <= P.PeriodEnd
WHERE S.[Subject] = B.[Subject]
GROUP BY P.[Id], S.[Subject], S.[ExamDate], S.[RawScoreStandard], S.[RawScorePercent], S.[AdjustedScore], S.[NBOMEId], S.[NBOMELastName], S.[NBOMEFirstName], S.[SecondYrTake]) [StudentScore]  
ON 
([StudentScore].PeriodId = [Average_Update].ExamPeriodId 
AND [StudentScore].Subject = [Average].Subject 
AND [StudentScore].[ExamDate] <= [Average_Update].[Updated])
--End meat


--Joins to pull in relevant data for normalized tables
FULL OUTER JOIN [dbo].[Student] [Student] ON ([StudentScore].[NBOMEId] = [Student].[NBOMEId])
INNER JOIN [dbo].[ExamPeriod] [ExamPeriod] ON ([Average_Update].ExamPeriodId = [ExamPeriod].[Id])
INNER JOIN [dbo].[AcademicYear] [AcademicYear] ON ([ExamPeriod].[AcademicYearId] = [AcademicYear].[Id])

--This will pull only the latest update entry for every academic year.
WHERE [Updated] IN (
SELECT DISTINCT MAX([Updated]) AS MaxDate
FROM [Average_Update]
GROUP BY[ExamPeriodId])

GROUP BY [AcademicYear].[AcademicYearText], [Average].[Subject], [Average_Update].[Updated], 
ORDER BY [AcademicYear].[AcademicYearText], [Average_Update].[Updated], [Average].[Subject]

答案 1 :(得分:0)

我无法下载您的文件以测试您的数据,但尝试按顺序取消平均值

平均值(IF DATE(ATTR([考试日期]))&lt; = DATE(ATTR([平均值(Tableau测试分数)]。[更新])然后[原始分数])END)

如上所述,我相信你会在从if语句返回数据之前对数据进行平均,而你想要返回数据,然后对其进行平均。