DAX度量值忽略DATESBETWEEN筛选器

huangapple go评论62阅读模式
英文:

DAX Measure ignoring DATESBETWEEN filter

问题

我有2个表,一个叫做"Treatments",每一行都包含一个不同的SessionId,它包含了一个Treatment Date和一个Treatment Classification(Completed或Non Completed)。

这个表看起来是这样的

DAX度量值忽略DATESBETWEEN筛选器

另外,我有一个叫做"Date"的日历表,它包含一个名为"Date"的列,其中每一行都是过去3年的一个不同日期。它还有一个名为Date30D的列,其中包含了30天前的日期,这是通过以下方式计算的。

Date30D = CALCULATE(MIN('Date'[Date]), DATEADD('Date'[Date], -30, DAY))

所以我正在尝试通过一个DAX度量来计算一个列,该列对于日期表中的每一行,计算过去30天内的"Completed"治疗次数。

这是我的度量

Completed Treatments =
CALCULATE(
COUNTROWS(Treatments),
Treatments[Treatment Status] = "Completed",
DATESBETWEEN(
Treatments[Treatment Date],
'Date'[Date30D],
'Date'[Date]))

问题是,它是按天而不是按月计算已完成的治疗次数,它完全忽略了DATESBETWEEN条件,我不知道为什么。我可以怎么做来修复这个问题?

DAX度量值忽略DATESBETWEEN筛选器

(这是列的样子,结果应该更高,它只是按天计算)
1: https://i.stack.imgur.com/5goBN.png
2: https://i.stack.imgur.com/Atx4r.png

英文:

I've got 2 tables, one is called "Treatments" and it contains a distinct SessionId on each row, it contains a Treatment Date and a Treatment Classification (Completed or Non Completed)

This table looks something like this

DAX度量值忽略DATESBETWEEN筛选器

Separately, I've got a calendar table called "Date" that contains a column called "Date" where each row is a distinct date for the past 3 years. and it also has a column called Date30D which contains the date for 30 days previous, this is calculated the following way.

Date30D = CALCULATE(MIN('Date'[Date]), DATEADD('Date'[Date], -30, DAY))

So what I am trying to do is calculate a column through a DAX measure that counts for each row on the date table, the number of "Completed" treatments for the past 30 days.

This is my measure

Completed Treatments = 
CALCULATE(
    COUNTROWS(Treatments),
    Treatments[Treatment Status] = "Completed",
    DATESBETWEEN(
        Treatments[Treatment Date],
        'Date'[Date30D],
        'Date'[Date]))

The issue is that, it is calculating the number of completed treatments by day, not by month, it is ignoring the DATESBETWEEN condition completely and I don't know why. What can I do to fix this?

DAX度量值忽略DATESBETWEEN筛选器

(This is what the column looks like, the results should be way higher, it is just calculating by day)

答案1

得分: 0

我是这样做的:
(为了简单起见,我首先使用一个简单的筛选器来计算已完成治疗的数量)。

已完成治疗数量 =
CALCULATE(
COUNTROWS('TreatmentsFact'),
FILTER('TreatmentsFact',[Classification]="Completed")
)
然后我只需要调整时间跨度...

过去30天已完成 =
CALCULATE([已完成治疗数量],
FILTER('DimDate',
'DimDate'[Date]>=MAX('DimDate'[Date])-30 &&
'DimDate'[Date]<='DimDate'[Date])
)

英文:

I did it like this:
(for simplicity, I calculated the Count of Completed Treatments first... with a simple filter).

Count of Completed Treatments = 
      CALCULATE(
          COUNTROWS(&#39;TreatmentsFact&#39;),
          FILTER(&#39;TreatmentsFact&#39;,[Classification]=&quot;Completed&quot;)
        )

Then all I have to do is adjust the time span...

Completed Past 30 Days = 
    CALCULATE([Count of Completed Treatments],
             FILTER(&#39;DimDate&#39;,
                &#39;DimDate&#39;[Date]&gt;=MAX(&#39;DimDate&#39;[Date])-30 &amp;&amp;
                &#39;DimDate&#39;[Date]&lt;=&#39;DimDate&#39;[Date])
    )

答案2

得分: 0

这是解决此问题的方法。

完成的治疗 =
CALCULATE(
COUNTROWS(Treatments),
Treatments[Treatment Status] = "已完成",
'Date'[Date] >= EARLIER('Date'[Date30D]) && 'Date'[Date] <= EARLIER('Date'[Date])
)

EARLIER 函数将仅按照您行中的日期进行筛选。

英文:

This is the way to fix this issue.

Completed Treatments = 
CALCULATE(
    COUNTROWS(Treatments),
    Treatments[Treatment Status] = &quot;Completed&quot;,
    &#39;Date&#39;[Date] &gt;= EARLIER(&#39;Date&#39;[Date30D]) &amp;&amp; &#39;Date&#39;[Date] &lt;= EARLIER(&#39;Date&#39;[Date]))

the EARLIEST function will filter only by the dates within your row.

huangapple
  • 本文由 发表于 2023年7月13日 22:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定