Tableau - 计算日期小于其他数据源值的平均值 [英] Tableau - Calculating average where date is less than value from another data source

查看:47
本文介绍了Tableau - 计算日期小于其他数据源值的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to calculate the average of a column in Tableau, except the problem is I am trying to use a single date value (based on filter) from another data source to only calculate the average where the exam date is <= the filtered date value from the other source.

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

Note: Parameters will not work for me here, since new date values are being added constantly to the set.

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

I have tried many different approaches, but the simplest was trying to use a calculated field that pulls in the filtered exam date from the other data source.

它可以成功拉取过滤后的日期,但公式没有按预期工作.2个版本的计算如下:

It successfully can pull the filtered date, but the formula does not work as expected. 2 versions of the calculation are below:

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 中寻找等价物:

Basically, I am looking for the equivalent of this in SQL Server:

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

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

Below a workbook that shows an example of what I am trying to accomplish. Currently it returns all blanks, likely due to the many-to-one comparison I am trying to use in my calculation.

非常感谢任何反馈!

Tableau 考试作业手册

推荐答案

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

I was able to solve this by using Custom SQL to join the tables together and calculate the average based on my conditions, to get the column results I wanted.

直接在 Tableau 中拥有此功能仍然很棒,但无论如何都能完成工作.

Would still be great to have this ability directly in Tableau, but whatever gets the job done.

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]

这篇关于Tableau - 计算日期小于其他数据源值的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆