首页 > 解决方案 > DAX 计算所有 7 天的平均值 数量值

问题描述

|  DateTime             |    Quantity   |
|:--------------------- |:   ---------- |
| 6/8/2021  1:00PM      |      89       |
| 6/7/2021  8:00PM      |      99       |
| 6/6/2021  11:00PM     |      70       |
| 6/5/2021  9:00PM      |      60       |
| 6/4/2021  5:00AM      |      55.5     |
| 6/3/2021  11:00PM     |      53       |
| 6/2/2021  12:00AM     |      99       |
| 6/1/2021  6:00PM      |      100      |

I tried the following code but not giving me the correct result. There are two varaibles. Not sure if it's correct way.
"Average Last 7 Days= 
var All_Hours= CALCULATE(AVERAGE(Table1[Quantity]), ALLSELECTED(Table1))
var lastOneWeek = LastOneHour - TIME(168,0,0) '''168 is for the past '''7 days hour.

return CALCULATE([Quantity] , FILTER(ALL(Table1), SystemMetrics[timestamp]>=All_Hours))" 非常感谢您的帮助。

标签: powerbidax

解决方案


您想计算从现在开始的 7 天的 AVERAGE 吗?或者在每个 DateTime 结束的最后 7 天?

AVG7dayNOW = 
Calculate (AVERAGE(Table1[Quantity]), FILTER(ALL(Table1[DateTime]), Table1[DateTime] >= NOW() - 7 && Table1[DateTime] <= NOW()))

第二击:

AVG7Day =
calclulate (AVERAGE(Table1[Quantity]), FILTER(ALL(Table1[DateTime]), Table1[DateTime] >= SELECTEDVALUE(Table1[DateTime]) - 7 && Table1[DateTime] <= SELECTEDVALUE(Table1[DateTime])))

如果要应用其他过滤器,可以将 ALL 更改为 ALLSELECTED。


推荐阅读