# Power Query Date Comparison



## Gwill1983 (Jun 11, 2021)

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?


----------



## Misca (Jun 12, 2021)

I'd use something like:

```
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:
	
	
	
	
	
	



```
if Date.Month([Column B])>Date.Month([Column A]) then Date.MonthName([Column B]) else Date.MonthName([Column A])
```


----------



## Alex Blakenburg (Jun 12, 2021)

This looks at year and month:-


```
= 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]))
```


----------



## Gwill1983 (Jun 13, 2021)

Alex Blakenburg said:


> This looks at year and month:-
> 
> 
> ```
> ...


This worked like a charm!! Thanks alot!


----------



## Alex Blakenburg (Jun 13, 2021)

Thanks for letting us know. Glad we could help.


----------

