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

Multiple column date comparison and replace with earliest date

Hello,

I am a relatively new user to PowerBI. I am finding it challenging to implement some of the requirements using powerBI.  

 

I have 4 date columns, DateA, DateB, DateC and DateD. (Some of these may have blank value)  

I want to loop through these and do the following:

1. If any 3 of the 4 columns have the same date value, then compare that value with the 4th column to find the earliest date and replace all 4 dates with this lowest date. only non blank columns should be replaced. 

2. If out of the 4, some are blank, ignore that for the comparison. i.e if one column is blank, then if 2 columns are same, it can be compared against the 3rd one and earliest date found. 

3. if 3 of them do not match, then flag entire row as "manual intervention"  

 

Without having "for" loops, this is a bit challenging for me. 

 

I managed to identify the earliest date for #1 and "manual intervention" for #3 with the help of a bunch of if statements, but:

a. If any column is blank, I dont know how the logic will work

b. I dont know how to flag and replace all the cells with the lowest value found here. 

c. General question. Can "if - then" have multiple statements after then. like if a=b then { c=d; e=f} ? 

 

Here is my code to identify #1 and #3 above: 

= Table.AddColumn(#"Added col for FutureHire", "FutureHires-Updated all dates with original hiredate", each
if ([DateA] = [DateB]) then
     if ([DateB] =[DateC] ) then
           if [DateC] < [DateD] then [DateC] else [DateD]
     else if ( [DateB] = [DateD]) then
            if ([DateC] < [DateD]) then [DateC] else [DateD]
     else "this condition should not arise"
else if ([DateB] = [DateC]) then
        if ([DateC] = [DateD]) then
               if ([DateA] < [DateD]) then [DateA] else [DateD]
        else "this condition should not arise"
else if([DateA] = [DateC] and [DateA] = [DateD]) then
           if ([DateA] < [DateB]) then [DateA] else [DateB]
else "Manual Intervention" 

 

Is my approach completely wrong here? can you help guide me on how to achieve this? To acheive #2 above (if one column is blank), I need to have whole another set of if conditions? 

 

Thanks in advance,

Jas

1 ACCEPTED SOLUTION
v-zhenbw-msft
Community Support
Community Support

Hi @jasmine9 ,

 

We can use the following steps to meet your requirement.

 

1. Click ID column and unpivot other columns.

 

Mu1.jpg

 

2. Copy the table and group one of them.

 

Mu2.jpg

 

3. Merge the group table and expand the min date.

 

Mu3.jpg

 

4. Then select id column and pivot value column.

 

Mu4.jpg

 

5. Use conditional column to create four date columns to separate the null.

 

Mu5.jpg

Mu6.jpg

 

6. At last we can add another conditional column, then delete the Date.A, B, C, D.

 

Mu7.jpg

 

Mu8.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

 

Best regards,

 

Community Support Team _ zhenbw

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

 

BTW, pbix as attached.

View solution in original post

5 REPLIES 5
v-zhenbw-msft
Community Support
Community Support

Hi @jasmine9 ,

 

We can use the following steps to meet your requirement.

 

1. Click ID column and unpivot other columns.

 

Mu1.jpg

 

2. Copy the table and group one of them.

 

Mu2.jpg

 

3. Merge the group table and expand the min date.

 

Mu3.jpg

 

4. Then select id column and pivot value column.

 

Mu4.jpg

 

5. Use conditional column to create four date columns to separate the null.

 

Mu5.jpg

Mu6.jpg

 

6. At last we can add another conditional column, then delete the Date.A, B, C, D.

 

Mu7.jpg

 

Mu8.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

 

Best regards,

 

Community Support Team _ zhenbw

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

 

BTW, pbix as attached.

Ashish_Mathur
Super User
Super User

Hi,

Share data in a format the can be pasted in an MS Excel workbook and show the exact result you are expecting.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

This is a sample of my data and Action required:  My code is in powerquery, not excel.  

IdDateADateBDateCDateDAction to take
18/25/20208/1/20178/25/20208/25/2020replace col A, col C and col d with 8/1/2017
27/1/2020 7/2/20207/3/2020flag as manual intervention
36/1/20206/1/20206/5/2020 replace col c with 6/1/2020
4 5/1/2020 5/2/2020flag as manual intervention
54/1/2020 4/21/20204/21/2020replace col c and d with 4/1/2020
63/1/20203/1/2020 4/1/2020replace col d with 3/1/2020

Id          DateA             DateB            DateC            DateD                 Action to Take
18/25/20208/1/20178/25/20208/25/2020replace col A, col C and col D with 8/1/2017
27/1/2020 7/2/20207/3/2020flag as manual intervention
36/1/20206/1/20206/5/2020 replace col c with 6/1/2020
4 5/1/2020 5/2/2020flag as manual intervention
54/1/2020 4/21/20204/21/2020replace col c and d with 4/1/2020
63/1/20203/1/2020 4/1/2020replace col d with 3/1/2020

 

1. If any 3 of the 4 columns have the same date value, then compare that value with the 4th column to find the earliest date and replace all 4 dates with this lowest date. only non blank columns should be replaced. 

2. If, out of the 4, some are blank, ignore that for the comparison. i.e if one column is blank, then if 2 columns are same, it can be compared against the 3rd one and earliest date found. 

3. if 3 of them do not match, then flag entire row as "manual intervention"  

Hi,

Someone else will help you with this please.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.