[英]How to create calculated column based on attribute and date range in other table Power BI?
I have two tables, Table A contains an attribute (ID1) and Time (date/time format).我有两个表,表 A 包含一个属性 (ID1) 和时间(日期/时间格式)。 Table B contains an attribute (ID2) and two time columns Time_start and Time_end (both date/time) format.
表 B 包含一个属性 (ID2) 和两个时间列 Time_start 和 Time_end(均为日期/时间)格式。
I need to create a column in Table A that fetches ID2 from Table B where ID1=ID2 and Time is between Time_start and Time_end.我需要在表 A 中创建一个列,从表 B 中获取 ID2,其中 ID1=ID2 并且时间介于 Time_start 和 Time_end 之间。
I have tried the following but due to the quantity of data (5 million rows in TableA, 100 thousand in TableB) I run out of RAM:我尝试了以下但由于数据量(TableA 中有 500 万行,TableB 中有 10 万行)我用完了 RAM:
Column = CALCULATE(FIRSTNONBLANK(TableB[ID2],1),FILTER(TableB,TableB[ID2]='TableA'[ID1]),FILTER(TableB,TableB[Time_start]<='TableA'[Time]),FILTER(TableB,TableB[Time_end]>='TableA'[Time]))
I have already applied a date filter in Power Query and removed columns I don't need.我已经在 Power Query 中应用了日期筛选器并删除了我不需要的列。 Is there a more efficient way of doing this in DAX or Power Query, unfortunately joining the tables in prior to date load in SQL is not an option.
在 DAX 或 Power Query 中是否有更有效的方法来执行此操作,不幸的是,在 SQL 中的日期加载之前加入表不是一个选项。
With the sizes you are dealing with, you should do this in Power Query, not DAX.对于您要处理的大小,您应该在 Power Query 而不是 DAX 中执行此操作。 Merge the 2 tables, then expand the tables to get Time_start and Time_end .
合并 2 个表,然后展开表以获得Time_start和Time_end 。 Then add ID2 with a formula like:
然后使用如下公式添加ID2 :
try if [Time_start] <= [Time] and [Time] <= [Time_end] then [ID1] else null otherwise null)
The try
and otherwise
will handle nulls in Time_start and Time_end , which would otherwise cause an error. try
和otherwise
将处理Time_start和Time_end中的空值,否则会导致错误。
Here are my sample queries:这是我的示例查询:
// Table A
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTLUN9Q3MlIwtDIyVorViVZywiLmjEXMFV0sFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID1 = _t, Time = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID1", type text}, {"Time", type datetime}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID1"}, #"Table B", {"ID2"}, "Table B", JoinKind.LeftOuter),
#"Expanded Table B" = Table.ExpandTableColumn(#"Merged Queries", "Table B", {"Time_start", "Time_end"}, {"Time_start", "Time_end"}),
#"Added Custom" = Table.AddColumn(#"Expanded Table B", "ID2", each try if [Time_start] <= [Time] and [Time] <= [Time_end] then [ID1] else null otherwise null),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Time_start", "Time_end"})
in
#"Removed Columns"
// Table B
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI00jc21DcyVDC0MjAA8cFcIwg3VidayQksiBCD84xgKpyBYkYoKozQVLhgmIGsPjYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID2 = _t, Time_start = _t, Time_end = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID2", type text}, {"Time_start", type datetime}, {"Time_end", type datetime}})
in
#"Changed Type"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.