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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Conditional column: Date in one table lookup with date ranges in different tables for every record.

I have a Table1:

 

JobNameTimestamp
A21-04-2019 12:30
G22-04-2019 12:30
FF23-04-2019 12:30
S24-04-2019 12:30
E25-04-2019 12:30
G26-04-2019 12:30
R27-04-2019 12:30
H28-04-2019 12:30
RE29-04-2019 12:30
D30-04-2019 12:30
A21-04-2019 12:30
D21-04-2019 12:31
G21-04-2019 12:32
FF21-04-2019 12:33
R17-04-2019 12:34
C21-04-2019 12:35
B18-04-2019 12:36
S21-04-2019 12:37
S21-04-2019 12:38
FF21-04-2019 12:39

 

I have another Table2:

 

KeyJobName1JobName2Jobname3Jobname4StartTimeEndTIme
1A  S20-04-2019 12:3020-04-2019 13:20
2B   16-04-2019 12:3021-04-2019 13:20
3CA G17-04-2019 12:3022-04-2019 13:20
4DGS 18-04-2019 12:3023-04-2019 13:20
5S DG19-04-2019 12:3024-04-2019 13:20
6RSA 20-04-2019 12:3025-04-2019 13:20
7ADF 21-04-2019 12:3026-04-2019 13:20
8GRE A20-04-2019 04:3020-04-2019 07:20
9HFAD23-04-2019 12:3023-04-2019 13:20
10EEGF18-04-2019 07:3018-04-2019 09:20
11SS   25-04-2019 12:3025-04-2019 13:20
12FF AG26-04-2019 12:3026-04-2019 13:20
13S  A17-04-2019 10:3017-04-2019 11:52
14S G 28-04-2019 23:3029-04-2019 00:20
15FFD A29-04-2019 12:3029-04-2019 13:20
16R  G20-04-2019 12:3020-04-2019 13:20
17 G  18-04-2019 12:3018-04-2019 13:20
18    02-05-2019 12:3002-05-2019 13:20
19 H  20-04-2019 17:3020-04-2019 19:23
20  DS17-04-2019 14:3017-04-2019 15:20

 

I want to add another column in Table1:

  • Flag=1 if Timestamp for that Job lies in any of the Time period defined in Table2 against that job (in any of the 4 Jobnames in Table2).
  • Flag=0 otherwise.

I tried to be as clear as possible.

 

I have this data in a relational DB. So I can't go with modifying the source itself after doing the same in python/dataframe.

 

I tried to employ the Python scripting in PowerQuery, but I'm not able to be successful with loading tow tables at once.

 

I need to do it in DAX/Power Query, since I have to do it in PowerBI itself!

1 REPLY 1
JarroVGIT
Resident Rockstar
Resident Rockstar

First off, interesting Question! I've loaded your tables in PBI and got the desired result.

 

Step 1. You need to unpivot the columns JobName1 through JobName4 in Table2. If you require this table to be unchanged in your model, copy your query so you have a duplicate and then unpivot the columns. This results in a Table where column Attribute contains either JobName1 through JobName 4 with a corresponding Value column of the jobnames. 

 

Step 2. We're not interested in the Attributes column for this purpose, so delete it. Rename Value column to JobName. Filter out the blanks of column JobName.

 

Outcome: we now have transformed Table2 into a table with jobnames and their corresponding beginTime and endTIme. 

 

Step 3: Add a calculated column in Table1, with the following formula:

 

 

Flag = IF(CALCULATE(COUNTA(Table2[Key]), FILTER(Table2, (Table1[Timestamp] >= Table2[StartTime] && Table1[Timestamp] <= Table2[EndTIme] && Table1[JobName] = Table2[JobName]))) > 0, TRUE, FALSE)

 

 

Explanation: For every row in Table1, we are doing a rowcount of Table2 where the timestamp and jobname are the same. If the row count is bigger than 0, then return TRUE (or 0 in your case, I used TRUE) and when the row count is zero then return FALSE.

 

This results in the following table:

JobName Timestamp Flag

A21-04-2019 12:30True
G22-04-2019 12:30True
FF23-04-2019 12:30False
S24-04-2019 12:30True
E25-04-2019 12:30False
G26-04-2019 12:30True
R27-04-2019 12:30False
H28-04-2019 12:30False
RE29-04-2019 12:30False
D30-04-2019 12:30False
A21-04-2019 12:30True
D21-04-2019 12:31True
G21-04-2019 12:32True
FF21-04-2019 12:33False
R17-04-2019 12:34False
C21-04-2019 12:35True
B18-04-2019 12:36True
S21-04-2019 12:37True
S21-04-2019 12:38True
FF21-04-2019 12:39False

 

Note: some jobs in Table1 have overlapping timeframes with multiple rows in Table2. You can check this by changing the formula to removing the IF statement and '> 0, TRUE, FALSE)'  part. Hope this helps! 

Please check this as solution if this answered your question.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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