【发布时间】:2023-04-11 09:00:02
【问题描述】:
我是一位经验丰富的 SQL 人员... DAX 新手。我正在尝试创建
用于生成移动平均库存的 DAX 查询。
库存计算的内部结构有点复杂,但是
希望本次讨论没有必要。
输入会是这样的:
month inventory
1 5
2 9
3 7
4 11
期望的输出是:
reportMonth average3MthInventory
3 7
4 9
也就是说,要报告的第 3 个月的平均库存是尾随 第 1,2,3 个月的平均值 = (5+9+7) / 3 = 7 ... 第 4 个月将是 第 2、3、4 个月的平均值。
到目前为止,查询使用 summarize 生成中间表 每月库存数量,即
evaluate
filter(
crossJoin(
summarize(
...
, reportMonths[month] -- group by
, "inventory", count(widgets[widgetID])
) -- end summarize
) -- end crossJoin
) -- end filter
这会产生一个中间表:
reportMonths[month] actualMonths[month] [inventory] ... plus some other columns
3 1 5
3 2 9
3 3 7
4 2 9
4 3 7
4 4 11
这完全符合预期。由此,应该可以平均 报告月份的库存。
此时,我将上面的查询包装在另一个 summarize 中 计算最终数字:
evaluate
summarize(
filter(
crossJoin(
summarize(
...
, reportMonths[month] -- group by
, "inventory", count(widgets[widgetID])
) -- end summarize
) -- end crossJoin
) -- end filter
, "month", reportMonths[month]
, "avgInventory", average([inventory])
) -- summarize
但是,这会返回错误:“无法识别包含 [inventory] 列的表。”
找不到引用中间表的方法。必须有另一种方式来构建它。
任何想法表示赞赏。
编辑以回应亚历杭德罗·祖莱塔:
亚历杭德罗,
感谢您的快速回复。更多信息如下......
库存历史是根据产品“列表”建立的。日期粒度是每月,每个列表都有一个 onMarket 月份和 offMarket 月份(月份按顺序编号)。列表还有其他属性 typeID、areaID 等。
listingID onMarketMonth offMarketMonth ... other attribs - typeID, etc.
101 1 2
103 1 6
105 2 2
106 2
109 2 3
117 3 4
123 3
124 3 9
库存是根据 onMarket 和 offMarket 月份计算的,例如第 3 个月库存是 onMarket 月份 3(或空白)的列表数量。根据上表,库存如下:
库存
month inventory note: listings in inventory
1 2 101, 103
2 3 103, 106, 109
3 5 103, 106, 117, 123, 124
4 4 103, 106, 123, 124
...
需要能够报告一系列值,例如对于图表以及移动平均线。特定类型和区域的一系列月份 2 到 6 的示例代码将是:
evaluate
summarize(
filter(
crossJoin(
calculateTable(listings, listings[typeID] = 47)
, filter(reportMonths, [month] >= 2 && [month] <= 6 )
) -- crossjoin
, listings[onMarketMonth] <= reportMonths[month] && (or(listings[offMarketMonth] > reportMonths[month], isBlank(listings[offMarketMonth]))) -- join condition
) -- filter the join
, reportMonths[month]
, "inventory",count(listings[listingID])
) -- summarize
这行得通。挂断是......如何扩展它以创建移动平均库存。
更新 弄清楚了。关键是从 summarize 切换到 groupBy(https://msdn.microsoft.com/en-us/library/mt163693.aspx)。
在示例代码(下方)中请注意,[Total Sales] 在 VAR 的 GroupBy 中定义/计算,然后在下面的查询中引用(在 Evaluate 中的 GroupBy 中)。当我尝试使用 Summarize 进行类似操作时,出现错误。
DEFINE
VAR SalesByCountryAndCategory =
GROUPBY (
Sales,
Geography[Country],
Product[Category],
“Total Sales”, SUMX( CURRENTGROUP(), Sales[Price] * Sales[Qty])
)
Evaluate GROUPBY (
SalesByCountryAndCategory,
Geography[Country],
“Max Sales”, MAXX( CURRENTGROUP(), [Total Sales])
)
【问题讨论】:
标签: dax