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
chq
Helper II
Helper II

Conditional Column Between two tables?

I have two sets a of data that look like the following...

ID#     Date Valid                                                                          ID#                         Scan Date

1         01/01/2018                                                                       1                              01/01/2018

2         01/05/2017                                                                       1                              01/02/2018

3         01/05/2018                                                                       1                              01/02/2018

4         01/05/2017                                                                       1                              01/08/2018

                                                                                                     2                              01/04/2017

                                                                                                     2                              01/05/2017

                                                                                                     2                              01/06/2017

                                                                                                     3                              01/10/2018

                                                                                                     4                              01/01/2017



I would like to somehow either combine them or use a formula that can create a new column where it flags any Scan Date that happens before the Valid Date. 

Does anyone know how i would go about doing this?

I also need to be able to count the total number of ID#s so I don't think a merge will work because that will increase the total number of rows. Ideally the finished product would look like this.


ID#                           Scan Date            Date Valid              Early Scan

1                              01/01/2018          01/01/2018            0

1                              01/02/2018          01/01/2018            0

1                              01/02/2018          01/01/2018            0

1                              01/08/2018          01/01/2018            0

2                              01/04/2017          01/05/2017            1

2                              01/05/2017          01/05/2017            0

2                              01/06/2017          01/05/2017            0

3                              01/10/2018          01/05/2018            0

4                              01/01/2017          01/05/2017            1

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi chq,

Here's what I did based off your sample date (thanks for providing some!):

Two steps,

1) In your "Scan" table, open the Power Query Editor and click to add a new column, and use this code:

let currentID = [ID]
in 
  List.Single(Table.SelectRows(
    #"Valid",
    each [ID] = currentID
  )[Date Valid])


Here's what it does:
- Sets a variable to hold the value of the current row ID
- Selects the row from the "Valid" table with a matching ID
- Gets the "Date Valid" field from that row (as a list...)
- Retrieves that single item from the list so you have it as a value/date

2) Again, add a new column, and this time use this code:

if [Scan Date] < [Date Valid] then 1 else 0


Alternatively...

perry2k's solution got me thinking - why use 2 columns when you can use 1?  If for some reason you don't need or want the "Date Valid" column, and really just want to see if the scan date was early or not, you can use add a single column and use this combined code:

if (
let currentID = [ID]
in 
  List.Single(Table.SelectRows(
    #"Valid",
    each [ID] = currentID
  )[Date Valid])) <= [Scan Date] then "0" else "1"


It's ugly, but all it's doing is wrapping the code from above if an IF block, and directly checking the lookup result against the scan date.

View solution in original post

4 REPLIES 4
parry2k
Super User
Super User

@chq assuming you have relation between both these table, you can simply add new column in you "scan detail" table, please change the name of tables and columns in following expression as per your model. 

 

Early Scan = IF( ScanTable[SCan Date] < RELATED( ValidTable[Valid Date] ), 0, 1 )


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

They share a relationship based on the ID# but when I punch in the formula I am getting an error that there is not a relationship to any table available in the current context? Do I need to link them via another column other than ID#?

Anonymous
Not applicable

Hi chq,

Here's what I did based off your sample date (thanks for providing some!):

Two steps,

1) In your "Scan" table, open the Power Query Editor and click to add a new column, and use this code:

let currentID = [ID]
in 
  List.Single(Table.SelectRows(
    #"Valid",
    each [ID] = currentID
  )[Date Valid])


Here's what it does:
- Sets a variable to hold the value of the current row ID
- Selects the row from the "Valid" table with a matching ID
- Gets the "Date Valid" field from that row (as a list...)
- Retrieves that single item from the list so you have it as a value/date

2) Again, add a new column, and this time use this code:

if [Scan Date] < [Date Valid] then 1 else 0


Alternatively...

perry2k's solution got me thinking - why use 2 columns when you can use 1?  If for some reason you don't need or want the "Date Valid" column, and really just want to see if the scan date was early or not, you can use add a single column and use this combined code:

if (
let currentID = [ID]
in 
  List.Single(Table.SelectRows(
    #"Valid",
    each [ID] = currentID
  )[Date Valid])) <= [Scan Date] then "0" else "1"


It's ugly, but all it's doing is wrapping the code from above if an IF block, and directly checking the lookup result against the scan date.

@chq it should work if you have one to many relationship between these two tables. can you share pbix if it doesn't contain sensitive data?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.