Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Output rows based on matching date time intervals

Hi, 

I need help on the same requirement which is handled in the below post, but when there is a many to many relationship. 

Solved: DAX: Flag order if timestamp is in between 2 dates... - Microsoft Power BI Community

Table B will have multiple deal intervals on the same product differentiated using a timestamp.

 

Modified Table B

deal_idstart_date_timeend_date_timeproduct_id
102.01.2021 10:00:00004.01.2021 14:00:00987654321
205.01.2021 8:00:0005.01.2021 8:25:00123456789
305.01.2021 10:00:0005.01.2021 10:25:00123456789

 

Modified Table A - order positions (multiple orders on same product at different times)

order_idtimestampproduct_id
12305.01.2021 8:10:00123456789
45604.01.2021 16:00:00987654321
78905.01.2021 10:15:00123456789
32108.01.2021 8:20:15987654321
65412.01.2021 6:15:15345876112
98715.01.2021 6:10:15345876112
32105.01.2021 8:15:00123456789

 

Output:

deal_idstart_date_timeend_date_timeproduct_idOrder_id
205.01.2021 8:00:0005.01.2021 8:25:00123456789123
205.01.2021 8:00:0005.01.2021 8:25:00123456789321
305.01.2021 10:00:0005.01.2021 10:25:00123456789789

 

Any lead on this would be of great help!

Thanks.

1 REPLY 1
lbendlin
Super User
Super User

Do you really need second level accuracy (as in deals 321 and 654) or is minute level accuracy sufficient?

 

Asking because the standard answer ("Use GENERATESERIES and INTERSECT") would be much tougher to do on seconds level.

 

Here is an alternative approach.  In your "Modified Table A"  add a calculated column:

 

 

 

deal_id = 
var t = 'Modified Table A'[timestamp]
var p = 'Modified Table A'[product_id]
var d = CALCULATETABLE('Modified Table B','Modified Table B'[start_date_time]<=t,'Modified Table B'[end_date_time]>=t,'Modified Table B'[product_id]=p)
return CONCATENATEX(d,'Modified Table B'[deal_id],"")

 

 

 

That will give you the deal ID for any order that has a deal.

 

lbendlin_0-1616813618207.png

 

However, when trying to link the tables via the deal_id field you will (not surprisingly) encounter a circular reference error.  That means it would be better to implement that calculated column in Power Query instead.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc/RDYAgDEXRVUy/SewrFNFViPuvYVuCmuhfQw630DtBMiXCqquwYGkH+GD2I8lF69Z2OlMnG4OVwVANDbe3rWrJgnDuXzmLQf967t21uVZcfnI2BoMMVz0XzmImAQlnt4bT2/Gfm2uf335fd14=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [order_id = _t, timestamp = _t, product_id = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"timestamp", type datetime}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"product_id"}, #"Modified Table B", {"product_id"}, "Modified Table B", JoinKind.LeftOuter),
    #"Expanded Modified Table B" = Table.ExpandTableColumn(#"Merged Queries", "Modified Table B", {"deal_id", "start_date_time", "end_date_time"}, {"deal_id", "start_date_time", "end_date_time"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Modified Table B", "Match", each if [start_date_time]<=[timestamp] and [timestamp]<= [end_date_time] then 1 else 0),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"Match", 0}}),
    #"Added Custom1" = Table.AddColumn(#"Replaced Errors", "d", each if [Match]=1 then [deal_id] else null),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"order_id", "timestamp", "product_id", "d"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"d", "deal_id"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"deal_id", type text}})
in
    #"Changed Type1"

which then allows us to link the tables and produce the expected outcome

lbendlin_1-1616815080345.pnglbendlin_2-1616815141194.png

 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.