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
jbrijalba
Helper I
Helper I

Using an IF statement comparing two date fields in two different tables

I have a master table (will call it Table1) that has a date range, there are also 22 people in the table (meaning a certain date can show up 22 times, while some dates never show up). I have a second table (will call it date table) that is just every single day in that same range. I created a relationship based on the date field, one (date table) to many (table1). I am trying to compare the two tables to see if someone in table1 does not have a value for a date in the date table.

 

I have tried Matched = If('date table'[Date] = related('table1'[date]), "Y", "N"). I get some message saying that there is no connection.  Now if I switch the two tables around and have table1 first then it works but it does not give me what I need.

 

I saw something about relatedtable and other quires while searching but nothing. 

Any help will be appreciated.

1 ACCEPTED SOLUTION
jbrijalba
Helper I
Helper I

Just wanted to let everyone know that I figured it out, not with DAX however. I copied the master table, pasted it to a new Excel file, deleted everything but the date, name and location field. Made the date field to contain all the dates in the range for each person (so 365 days for all 22 people, 365 * 22) . I then in Power Bi created a key column in each table. Key = Concatenate([Name], Concatenate(" - ", [Date]). I created the relationship based on the Key column in each table. From there I was able to get a one to one relationship and use the Related() funtion comparing the Date table to the able with the Peoples work days. 

 

Thank you everyone for your help. 🙂

View solution in original post

7 REPLIES 7
jbrijalba
Helper I
Helper I

Just wanted to let everyone know that I figured it out, not with DAX however. I copied the master table, pasted it to a new Excel file, deleted everything but the date, name and location field. Made the date field to contain all the dates in the range for each person (so 365 days for all 22 people, 365 * 22) . I then in Power Bi created a key column in each table. Key = Concatenate([Name], Concatenate(" - ", [Date]). I created the relationship based on the Key column in each table. From there I was able to get a one to one relationship and use the Related() funtion comparing the Date table to the able with the Peoples work days. 

 

Thank you everyone for your help. 🙂

Sorry when I said "Made the Date field to contain.....) I meant Date Table**

jbrijalba
Helper I
Helper I

Thank you all for replying.

The first table (Table1) looks similar to this Table1.PNG. With a lot more people and a lot more dates.

The second table (Date Table) looks similar to Date.PNG, again with a lot more dates.

The final table I am aiming to acheive after the DAX expression should look something like What I need.PNG

Sorry for the long reply but I think the visuals will help.

Give this measure a try, it should do what you are looking for.

Working = 
VAR HasDate = COUNTROWS(Table1)
RETURN IF ( ISBLANK(HasDate), "N","Y")
Amit2019
Frequent Visitor

Hi,

 

Try with below DAX expression-

 

LOOKUPVALUE(table1[date], table1[date],date[date])

It  will give table1[date] (single value) if matches found and blank otherwise.

 

Hope this will help.

 

 

Thanks,
Amit Dhiman

v-xicai
Community Support
Community Support

Hi @jbrijalba ,

 

The RELATED function requires that a relationship exists between the current table and the table with related information. You specify the column that contains the data that you want, and the function follows an existing many-to-one relationship to fetch the value from the specified column in the related table. That is to say the one relationship side Field should be into RELATED function ,like RELATED(one relationship Field).

 

So try to change your formula like DAX below.

 

Matched = If( 'table1'[date]= related('date table'[Date]), "Y", "N")

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Cmcmahan
Resident Rockstar
Resident Rockstar

Could you use CONTAINS?  Once your data is filtered by person, checking if the [date] column in table1 contains each date in table2 should work.

 

MATCHED = IF(CONTAINS('table1',[date],SELECTEDVALUE('table2'[date])),"yes","no")

 

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.