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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Gilson_Souza
Frequent Visitor

Return value based on hour range

Hi, everyone! I need some help, please.

I have two tables that have the listing of events that occurred at certain times. Here's a simplified version of the data file:

 

Table A:

tblA.png

Table B:

tblB.png

 

I need to return the ID value to Table A in case some criteria are met:

 

The cod column matches
If the table B event occurred between the -2 hour and +2 hour range of the table A event.

So the result would be:

 

tblAresult.png

 

Thanks in advance for your help.

2 ACCEPTED SOLUTIONS
SpartaBI
Community Champion
Community Champion

@Gilson_Souza create this caclculated column in table A:

 

ID = 
VAR _current_cod = 'Table A'[COD]
VAR _current_date_time_a = 'Table A'[DATETIME]
VAR _table = 
FILTER(
    'Table B',
    VAR _current_date_time_b = 'Table B'[DATETIME]
    RETURN
    'Table B'[COD] = _current_cod && ABS(_current_date_time_a - _current_date_time_b ) <= 0.083333333
)
VAR _result = CONCATENATEX(_table, 'Table B'[ID])
RETURN
    _result

 

SpartaBI_0-1652531612888.png

 


2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

@Gilson_Souza write this:

 

ID =
VAR _current_cod = 'Table A'[COD]
VAR _current_date_time_a = 'Table A'[DATETIME]
VAR _table =
    FILTER (
        'Table B',
        VAR _current_date_time_b = 'Table B'[DATETIME]
        RETURN
            'Table B'[COD] = _current_cod
                && _current_date_time_a - _current_date_time_b <= 0.0416666667
                	&& _current_date_time_a - _current_date_time_b >= - 0.125
    )
VAR _result =
    CONCATENATEX ( _table, 'Table B'[ID] )
RETURN
    _result

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

View solution in original post

7 REPLIES 7
SpartaBI
Community Champion
Community Champion

@Gilson_Souza create this caclculated column in table A:

 

ID = 
VAR _current_cod = 'Table A'[COD]
VAR _current_date_time_a = 'Table A'[DATETIME]
VAR _table = 
FILTER(
    'Table B',
    VAR _current_date_time_b = 'Table B'[DATETIME]
    RETURN
    'Table B'[COD] = _current_cod && ABS(_current_date_time_a - _current_date_time_b ) <= 0.083333333
)
VAR _result = CONCATENATEX(_table, 'Table B'[ID])
RETURN
    _result

 

SpartaBI_0-1652531612888.png

 


2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Hi, @SpartaBI 

Can you change the condition a little bit?

Instead:
If the table B event occurred between the -2 hour and +2 hour range of the table A event.

It will be:
If the table B event occurred between the -1 hour and +3 hour range of the table A event.

Thank you!

@Gilson_Souza write this:

 

ID =
VAR _current_cod = 'Table A'[COD]
VAR _current_date_time_a = 'Table A'[DATETIME]
VAR _table =
    FILTER (
        'Table B',
        VAR _current_date_time_b = 'Table B'[DATETIME]
        RETURN
            'Table B'[COD] = _current_cod
                && _current_date_time_a - _current_date_time_b <= 0.0416666667
                	&& _current_date_time_a - _current_date_time_b >= - 0.125
    )
VAR _result =
    CONCATENATEX ( _table, 'Table B'[ID] )
RETURN
    _result

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

@SpartaBI Thank you again! This formula worked perfectly 😀

@Gilson_Souza my pleasure 🙂
P.S. Check out my showcase report - got some high level stuff there. Sure you will find there a lot of cool ideas. 
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up over there if you liked it 🙂

@SpartaBI Worked perfectly! I have no words to thank you, I was trying to solve this for days. Thank you very much!

@Gilson_Souza my pleasure 🙂

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.