Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

M Power Query Column Calculation

Adding a new Custom Column.

Want to calculate the number of days to DUE_DATE

 

= Table.AddColumn(#"Changed Type", "DaysToDueDate", each DateTime.LocalNow()-([DUE_DATE]))

 

This is producing resuts that look like   4.88499E + 13

 

DUE_DATE is DATE type.  I want the difference between these two date to produce an integer simply telling how many days remain.

 

 

1 ACCEPTED SOLUTION

Hi @Anonymous 

 

It doesn't have to... This is my complete code:

let
    Origen = Excel.Workbook(File.Contents("C:\example.xlsx"), null, true),
    Hoja1_Sheet = Origen{[Item="Hoja1",Kind="Sheet"]}[Data],
    #"Encabezados promovidos" = Table.PromoteHeaders(Hoja1_Sheet, [PromoteAllScalars=true]),
    #"Tipo cambiado" = Table.TransformColumnTypes(#"Encabezados promovidos",{{"DUE_DATE", type datetime}, {"Column1", type text}}),
    #"Personalizada agregada" = Table.AddColumn(#"Tipo cambiado", "DaysToDueDate", each Duration.Days(DateTime.LocalNow()-([DUE_DATE])))
in
    #"Personalizada agregada"

 

I have used an excel with this data and it is working perfectly:

Column1 DUE_DATE
aaa 14/11/2023 11:35
bbb 21/11/2023 18:10
ccc

18/11/2023 13:45

 

mlsx4_0-1701082047250.png

I haven't defined any type for the new column but it shows correctly the result. I think your problem is with some "naming" or missing punctuation...

View solution in original post

7 REPLIES 7
mlsx4
Super User
Super User

Hi  @Anonymous

 

You will need to use the duration functions: 

 

= Table.AddColumn(#"Changed Type", "DaysToDueDate", each Duration.Days(DateTime.LocalNow()-([DUE_DATE])))

 If you want to learn more: https://learn.microsoft.com/en-us/powerquery-m/duration-days

Anonymous
Not applicable

Thank you for the reply and link. However, I'm receiving an error "Changed Type Not Recognized. Make Sure It's Spelled Correctly".  Any idea what's causing that

Hi @Anonymous 

Have you put a "," in the Power Query Editor before the step (I mean, at the end of the previous sentence)? If so, are you sure the previous step name is "Changed Type"?

Please, check all these things and tell me if it works or not

Anonymous
Not applicable

The [DUE_DATE] file is Date Time data type.  Does that need to be changed to "DATE" only?  Also, what should be the data type of the custom column I'm building?  Whole Number ?

Hi @Anonymous 

 

It doesn't have to... This is my complete code:

let
    Origen = Excel.Workbook(File.Contents("C:\example.xlsx"), null, true),
    Hoja1_Sheet = Origen{[Item="Hoja1",Kind="Sheet"]}[Data],
    #"Encabezados promovidos" = Table.PromoteHeaders(Hoja1_Sheet, [PromoteAllScalars=true]),
    #"Tipo cambiado" = Table.TransformColumnTypes(#"Encabezados promovidos",{{"DUE_DATE", type datetime}, {"Column1", type text}}),
    #"Personalizada agregada" = Table.AddColumn(#"Tipo cambiado", "DaysToDueDate", each Duration.Days(DateTime.LocalNow()-([DUE_DATE])))
in
    #"Personalizada agregada"

 

I have used an excel with this data and it is working perfectly:

Column1 DUE_DATE
aaa 14/11/2023 11:35
bbb 21/11/2023 18:10
ccc

18/11/2023 13:45

 

mlsx4_0-1701082047250.png

I haven't defined any type for the new column but it shows correctly the result. I think your problem is with some "naming" or missing punctuation...

Anonymous
Not applicable

I have this working with a variation. You were a big help. Thank you

I'm glad it finally works!! You're welcome! 😊

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.