我正在尝试绘制包含库存信息的全局表中的数据。
该表的模式为(库存金额、库存销售额、日期、时间、商店)
挑战是:每个商店每天记录4-5行关于库存和销售,我有4家商店。
我的目标是将这个表过滤到一个新的表中,该表只包括每个商店的最后一次库存金额和每天的库存销售额。
下面是我的输入表:

我想生成如下所示的输出。请注意,只保留9/2的存储600的第二个条目。

任何建议都是值得感谢的。
谢谢。
我尝试了一些过滤表达式,但没有一个对我有效。在此处输入图像描述
更新:因此,我尝试了下面答案中的示例,并得到了以下结果:
“2019年9月4日星期三,手机: 1new sill-fender cell3”显示18023;应该只显示1386.
发布于 2019-09-05 21:41:52
通常我会建议在M中这样做,虽然有点冗长,但我认为正在发生的事情要清楚得多。我在下面的M和DAX中有一个解决方案。对于任何一种解决方案,方法都是完全相同的:
首先是M解决方案:
// Input - query with raw data - no work here:
let
Source = #table(
{"inventory amount", "Sale in dollar", "date", "time", "store"},
{
{54, 100, "2019-09-03", "09:55:00", 500},
{52, 200, "2019-09-04", "10:34:00", 500},
{49, 300, "2019-09-05", "09:43:00", 500},
{112, 200, "2019-09-02", "13:13:00", 600},
{111, 100, "2019-09-02", "13:19:00", 600},
{109, 200, "2019-09-03", "15:25:00", 600}
}
),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"inventory amount", Int64.Type}, {"Sale in dollar", Int64.Type}, {"date", type date}, {"time", type time}, {"store", Int64.Type}})
in
#"Changed Type"
// StoreDateMaxTime - this is our helper table. This is set not to load into the
// model, just used in PQ for data processing.
let
Source = Input,
#"Grouped Rows" = Table.Group(Source, {"date", "store"}, {{"MaxTime", each List.Max([time]), type time}})
in
#"Grouped Rows"
// Output - our final table
let
Source = Input,
#"Merged Queries" =
Table.NestedJoin(
Source, {"store", "date", "time"},
StoreDateMaxTime, {"store", "date", "MaxTime"},
"HelperDedupe",
JoinKind.Inner),
// Note that we join above, but don't use the results of the join in any way
// we only care about the filtering semantic of an inner join.
#"Removed Columns" = Table.RemoveColumns(#"Merged Queries",{"HelperDedupe"})
in
#"Removed Columns"下面是DAX版本做的完全相同的事情:
Output2 =
VAR StoreDateMaxTime =
ADDCOLUMNS (
GROUPBY (
'Input',
'Input'[store],
'Input'[date]
),
"MaxTime", CALCULATE ( MAX ( 'Input'[time] ) )
)
RETURN
CALCULATETABLE (
'Input',
TREATAS ( StoreDateMaxTime, 'Input'[store], 'Input'[date], 'Input'[time] )
)请注意,您还可以加载完整的输入表,并定义一个度量,如下所示,它将返回最后一个库存记录:
FinalInventoryAmount =
VAR LastStoreDateTimes =
GENERATE(
VALUES ( 'Input'[store] ),
CALCULATETABLE (
TOPN (
1,
GROUPBY (
'Input',
'Input'[date],
'Input'[time]
),
'Input'[date], DESC,
'Input'[time], DESC
)
)
)
RETURN
CALCULATE (
SUM ( 'Input'[inventory amount] ),
TREATAS ( LastStoreDateTimes, 'Input'[store], 'Input'[date], 'Input'[time] )
)这个度量看起来应该与制表的模式非常相似。我们又在做一个很大程度上类似的操作。但为了让它能很好地处理过滤和总计,我们需要做一些稍微不同的事情:
只从上下文中的商店开始:对于每个商店,输入它的最新(‘GENERATE)
GENERATE)
您将看到,在总计级别,这是返回158,这是存储500的最后一个值(9/5的49 )和存储600的最后一个值(9/3的109 ),以获取49 + 109 = 158。
以下是使用该度量的视觉效果:

编辑:解释了更多关于测量如何工作的信息。
下面是一个交叉表(一个Matrix可视化),行上有日期和时间,列上有商店id。

让我们来看看这个。在9/2,商店500没有记录。商店600有两条记录。我们可以看到它们各自的时间。后者是13:19的111。您可以看到9/2的商店600的总数是后面的111。9/2,所有商店的总数是111,这是商店600的最新金额。
9月3日,商店500: 54在09:55有一条记录。在15:25,商店600: 109也有一条记录。我们可以在时间级别上看到这些单独的事务。在日期小计中,我们可以看到商店500和600的金额相邻。9/3的total列显示了54 + 109 = 163的添加,这是每个商店加在一起的最新金额,即所有商店的总数。
9/4和9/5很无聊,每一个都只有一个商店500的事务。
“总计”行显示每个商店的值,并在“总计”列中显示总计。存储的合计是该存储的最后一个值。对于商店500,这是49,从9/5在09:43。对于商店600,这是109,从15:25的9/3开始。总计(合计行,合计列)是这两个值-每个商店的最新值-相加:49 + 109 = 158。
总计只考虑来自每个商店的一个值-无论上下文中的最新值是什么。
Edit2:表示同时具有多条记录的值。仅更新度量,但对于M和DAX表版本,如何包含相同的值应该是显而易见的。
FinalInventoryAmount v2
VAR LastStoreDateTimes =
GENERATE(
VALUES ( 'Input'[store] ),
CALCULATETABLE (
SAMPLE ( // SAMPLE will ignore ties for those identical rows
1,
GROUPBY (
'Input',
'Input'[date],
'Input'[time],
'Input'[inventory amount]
),
'Input'[date], DESC,
'Input'[time], DESC,
'Input'[inventory amount], ASC // make sure we're getting the min
// amount value.
)
)
)
RETURN
CALCULATE (
SUM ( 'Input'[inventory amount] ),
// and below is the other difference
TREATAS (
LastStoreDateTimes,
'Input'[store],
'Input'[date],
'Input'[time],
'Input'[inventory amount] // we're also only including rows that tie
// on the min amount here.
)
)如果你不关心合计行为,你也可以用一个MIN替换SUM,以获得(商店,日期,时间)元组中的最小数量。
https://stackoverflow.com/questions/57805690
复制相似问题