DAX计算2个日期之间的行

时间:2018-04-03 19:11:51

标签: date count rows dax between

DECLARE @lower INT = 1
	,@upper INT = 6
	,@randnum INT
	,@date DATE = getdate()
	,@endofcurrentmonth DATE
	,@loopdate DATE
	,@loop INT
	,@loopmax INT
	,@innerloop INT
	,@innerloopmax INT
	,@value DATE

IF object_id('tempdb..#tmp_DateRange') IS NOT NULL
	DROP TABLE #tmp_DateRange

CREATE TABLE #tmp_DateRange (
	ID INT identity(1, 1) PRIMARY KEY NOT NULL
	,[Date] DATE
	)

IF object_id('tempdb..#tmp_RandomData') IS NOT NULL
	DROP TABLE #tmp_RandomData

CREATE TABLE #tmp_RandomData (
	ID INT identity(1, 1) PRIMARY KEY NOT NULL
	,[BidDateWon] DATE
	,[BidWon] BIT
	)

SELECT @endofcurrentmonth = dateadd(mm, datediff(MM, 0, dateadd(MM, 1, @date)), 0) - 1

SELECT @loopdate = dateadd(mm, - 9, dateadd(dd, 1 - datepart(dd, @date), @date))

WHILE @loopdate <= @endofcurrentmonth
BEGIN
	INSERT INTO #tmp_DateRange ([Date])
	VALUES (@loopdate)

	SET @loopdate = dateadd(dd, 1, @loopdate)
END

SELECT @loop = 0
	,@loopmax = max(ID)
FROM #tmp_DateRange

WHILE @loop < @loopmax
BEGIN
	SELECT @randnum = round(((@upper - @lower - 1) * rand() + @lower), 0)

	SET @loop = @loop + 1
	SET @innerloopmax = @randnum
	SET @innerloop = 0

	IF @randnum % 2 = 0
	BEGIN
		SET @value = (
				SELECT [date]
				FROM #tmp_DateRange
				WHERE ID = @loop
				)
	END
	ELSE
	BEGIN
		SET @value = NULL
	END

	WHILE @innerloop <= @innerloopmax
	BEGIN
		SET @innerloop = @innerloop + 1

		INSERT INTO #tmp_RandomData (BidDateWon)
		VALUES (@value)
	END
END

UPDATE #tmp_RandomData
SET BidWon = 1
WHERE BidDateWon IS NOT NULL

SELECT *
FROM #tmp_RandomData

=CALCULATE(COUNTROWS(Fact_SalesSummaries),
     DATESINPERIOD(Fact_SalesSummaries[BidWonDate],
         LASTDATE(Fact_SalesSummaries[BidWonDate]),
         -3, MONTH),
     Fact_SalesSummaries[BidWonDate] <> BLANK())

我尝试过搜索和不同的方法来计算日期介于两个不同日期之间的行,它将行计算为1而不是多于公式中的行。

这意味着从3个月后的3个月滚动记录,以便能够计算所有记录的平均值。

SQL中的示例如下所示。

select MonthBlended, yearblended, BidWonDate, BidWonFlag,
(select count(*) from Fact_SalesSummaries
	where convert(date, cast(MonthBlended as varchar(2))+ '/01/' + cast(YearBlended as varchar(4)), 101) between 
	dateadd(mm, -3, convert(date, cast(t1.MonthBlended as varchar(2))+ '/01/' + cast(t1.YearBlended as varchar(4)), 101))
	and convert(date, cast(t1.MonthBlended as varchar(2))+ '/01/' + cast(t1.YearBlended as varchar(4)), 101)
	and BidWonDate is not null
	) as CountWONPrior3Month,
(select count(*) from Fact_SalesSummaries
	where convert(date, cast(MonthBlended as varchar(2))+ '/01/' + cast(YearBlended as varchar(4)), 101) between 
	dateadd(mm, -3, convert(date, cast(t1.MonthBlended as varchar(2))+ '/01/' + cast(t1.YearBlended as varchar(4)), 101))
	and convert(date, cast(t1.MonthBlended as varchar(2))+ '/01/' + cast(t1.YearBlended as varchar(4)), 101)
	) as CountALLPrior3Month
 from Fact_SalesSummaries t1

BidWonDate                BidWon  CountWonPrior3Month CountAllPrior3Month
-----------------------   ------  ------------------- -------------------
2014-07-17 00:00:00.000	  1	      618	                1048
2014-07-17 00:00:00.000	  1	      618	                1048
2014-07-17 00:00:00.000	  1	      618	                1048
2014-07-17 00:00:00.000	  1	      618	                1048
NULL	                    0	      618	                1048
NULL	                    0	      618	                1048
NULL	                    0	      618	                1048
NULL	                    0	      618	                1048
2014-07-17 00:00:00.000	  1	      618	                1048
NULL	                    0	      618	                1048
NULL	                    0	      618	                1048
2014-07-11 00:00:00.000	  1	      618	                1048

在一个小节中尝试这个确实有效,但是没有正确计算任何帮助将不胜感激。

我添加了一个sql脚本,它会为您提供一个与我拥有的数据相匹配的随机数据样本。运行脚本,它将生成一个随机日期或空值,您可以运行dax表达式。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

我是DAX和PowerBI的新手,

因为我已经理解了计算功能,我认为你的第二个参数是导致错误的,第二个参数必须是过滤器,你不适合你的情况,你只是返回一系列日期,

我真的想要帮助,即使知道我可能会弄错,无论如何,如果你尝试它也不会受到伤害:D

答案 1 :(得分:0)

通过以下措施可以计算从当前日期开始的3个月内的记录数。

enumerator =
CALCULATE (
    COUNTROWS ( Fact_SalesSummaries ),
    DATESINPERIOD (
        'Fact_SalesSummaries'[BidDateWon],
        LASTDATE ( 'Fact_SalesSummaries'[BidDateWon] ),
        3,
        MONTH
    ),
    Fact_SalesSummaries[BidDateWon] <> BLANK ()
)

您丢失的出价没有日期这一事实使得很难准确地做到这一点,因为您必须依靠ID的序数(并希望是连续的)编号来创建分母。

denominator =
CALCULATE (
    MAX ( Fact_SalesSummaries[ID] ) - MIN ( Fact_SalesSummaries[ID] )
        + 1,
    DATESINPERIOD (
        'Fact_SalesSummaries'[BidDateWon],
        LASTDATE ( 'Fact_SalesSummaries'[BidDateWon] ),
        -3,
        MONTH
    )
)

如果您在bidwon = false的行中有日期,则可以使用与枚举器相同的计算方式,只需删除最后一个过滤器即可。