Power Query Date Comparison

Gwill1983

Board Regular
Joined
Oct 24, 2018
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi all,
Bit of a rookie with power query, but trying to adjust some data as part of a report that I am running.

i want to compare 2 dates and if the date in column b is in a later month than the date in column a then enter the month name of column b, if not enter the month name of column a.

currently I am adding custom columns to extract date names and comparing against each other, but feels a bit of a long winded way of doing it!
Can anybody help me with a formula to achieve this?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I'd use something like:
Power Query:
if [Column B]>[Column A] then Date.MonthName([Column B]) else Date.MonthName([Column A]) 
// or just
Date.MonthName(List.Max({[Column A],[Column B]}))
Both formulas compare the dates not just months so if the dates are from different years they might return wrong answers.

If you don't care about the later date but just the greater month wrap the columns in the comparison part of the formula in Date.Month-functions:
Power Query:
if Date.Month([Column B])>Date.Month([Column A]) then Date.MonthName([Column B]) else Date.MonthName([Column A])
 
Upvote 0
This looks at year and month:-

Power Query:
= Table.AddColumn(#"Renamed Columns", "Test", each if (Date.Year([Date b]) - Date.Year([Date a])) * 12 + (Date.Month([Date b]) - Date.Month([Date a])) > 0 then Date.MonthName([Date b]) else Date.MonthName([Date a]))
 
Upvote 0
Solution
This looks at year and month:-

Power Query:
= Table.AddColumn(#"Renamed Columns", "Test", each if (Date.Year([Date b]) - Date.Year([Date a])) * 12 + (Date.Month([Date b]) - Date.Month([Date a])) > 0 then Date.MonthName([Date b]) else Date.MonthName([Date a]))
This worked like a charm!! Thanks alot!
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top