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
CaveOfWonders
Helper IV
Helper IV

Date Value must contain the Date Component - DAX to Power Query:

Hi All,

 

I have a DAX query I'm trying to shift to Power Query. Below is the DAX which is working.

 

New Column = 
IF (
    'Base_Table'[Opening_Date] < 'Base_Table'[Official_Date],
    ( -1 )
        * MONTH (
            ENDOFMONTH ( 'Base_Table'[Opening_Date] )
                - ENDOFMONTH ( 'Base_Table'[Official_Date] )
        ),
    MONTH (
        ENDOFMONTH ( 'Base_Table'[Opening_Date] )
            - ENDOFMONTH ( 'Base_Table'[Official_Date] )
    )
)

 

However, I'm getting this error in Power Query:

 

PQ_Error.png

 

Any ideas what I need to change in my PQ code below in order to get this working? Columns formatted as 'Date'

 

New_Column
  = Table.AddColumn(
    Previous_Step, 
    "New Column", 
    each 
      if [Opening_Date] < [Official_Date] then
        (- 1)
          * Date.Month(
            Duration.Days(
              Date.EndOfMonth([Opening_Date]) - Date.EndOfMonth([Official_Date])
            )
          )
      else
        Date.Month(
          Duration.Days(Date.EndOfMonth([Opening_Date]) - Date.EndOfMonth([Official_Date]))
        ), 
    Int64.Type
  )

 

Much Appreciated

 

1 ACCEPTED SOLUTION

Hi @CaveOfWonders ,

 

Wow! That's an...um...'creative' way of getting the number of months difference in DAX 😐

 

I believe PQ doesn't have things like DATEDIFF as that is a time intelligence calculation function. PQ isn't really designed for advanced calculations, hence the existence of DAX. I may be wrong, but there's plenty of examples of things you can do in DAX that don't exist in PQ for, I believe, the same reason.

 

For reference, if you want to just get the total months difference in PQ, it would be something like this:

Number.Abs(
  12 * (Date.Year([Opening_Date]) - Date.Year([Official_Date]))
  + Date.Month([Opening_Date]) - Date.Month([Official_Date])
)

 

I've used Number.Abs to swerve the whole issue of one date being earlier/later than the other.

 

This gives me the following output:

BA_Pete_0-1637590741067.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

8 REPLIES 8
CaveOfWonders
Helper IV
Helper IV

It turns out that a difference in months is all that was required....🤦‍♂️

 

Why isn't there a datediff in Power Query? 

Hi @CaveOfWonders ,

 

Wow! That's an...um...'creative' way of getting the number of months difference in DAX 😐

 

I believe PQ doesn't have things like DATEDIFF as that is a time intelligence calculation function. PQ isn't really designed for advanced calculations, hence the existence of DAX. I may be wrong, but there's plenty of examples of things you can do in DAX that don't exist in PQ for, I believe, the same reason.

 

For reference, if you want to just get the total months difference in PQ, it would be something like this:

Number.Abs(
  12 * (Date.Year([Opening_Date]) - Date.Year([Official_Date]))
  + Date.Month([Opening_Date]) - Date.Month([Official_Date])
)

 

I've used Number.Abs to swerve the whole issue of one date being earlier/later than the other.

 

This gives me the following output:

BA_Pete_0-1637590741067.png

 

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




haha 😊, thanks for your patience Pete. Yes, I have no idea how someone came to that DAX solution....

 

Odd, I wonder why there are the some time intelligence functions in PQ such as difference in days, hours, mins, sec's etc... Either way your code worked perfectly, thank you.

 

CoW

Hi @CaveOfWonders ,

 

Cool, glad it worked for you.

To my knowledge, there are no date/time difference functions in Power Query. You have duration functions, but these all require the explicit subraction of one date from another, and just convert the result into an apportioned duration. They also don't work across date borders e.g. I believe that Duration.Hours between 01/01/2021 10:00 and 31/01/2021 14:00 will just give you 4.

Anyway, I agree that there probably should be as it could easily fold to most types of SQL server, but there we are.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




CaveOfWonders
Helper IV
Helper IV

Thanks Pete, The DAX is working, please see below. To be honest this is something I inherited and assumed was logically sound:

 

dax working.png

@CaveOfWonders ,

 

I'm struggling to understand what "New Column" in your example is representing to be honest.

At first glance, it looks like it's the number of months between [Opening_Date] and [Official_Date], but [ID] #74 appears to disprove this, as there's more than six months between these values as they span two years.

 

Can you spell out in layman's terms what this new column is supposed to return please?

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




I think I'm going to have to go back to the drawing board on this one. Suprised it works in DAX and not PQ though. I need to go back to those I inherited this from to try and understand what the intention behind the new column is and get back to you. I was blindly replicating without investigating until this happend.

BA_Pete
Super User
Super User

Hi @CaveOfWonders ,

 

Your errors are here:

BA_Pete_0-1637226307142.png

 

You are trying to get the month number (Date.Month) from a number of days (Duration.Days), rather than from a date value.

 

To be honest, I can't see how even your DAX version is working as it seems as though you make the same mistake there ( MONTH(Duration) ).

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




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.

Top Solution Authors
Top Kudoed Authors