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
Anonymous
Not applicable

Calculate number of days between today's date and previous date

I have a policy effective date in my dataset.

I need to calculate the number of days from that date, to today. And I want to do this in Query Editor, not Dax.

 

I used the following formula which doesn't work, see screenshot. Changing the column type also doesn't seem to make a difference

 

= Table.AddColumn(PreviousStep, "Days since EffectiveDate", each (DateTime.LocalNow()-[EffectiveDate]))

 

Sample.PNG

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

Hmm, try this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddFBCoQwDIXhqwxZC21SrXZ2uplDlJ5hRPD+zqID5dfuCl+SkrycxZypM6/p5d/eyyCf43vu6++hUobWFW5wgwd4gI/wET7BJ3iER/j893Cfv7X7hfv8rd3vwdeHfv7P/rl3n+pL7z7VU+8+1ZUBRhYwQV5IGeHCAmaYpJQL", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Timestamp = _t, Group = _t, RunningTotal = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Timestamp", type datetime}, {"Group", type text}, {"RunningTotal", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each DateTime.LocalNow() - [Timestamp])
in
    #"Added Custom"

Perhaps the issue is your time is a Date and not a Date Time?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

Yeah, that's really strange, shouldn't be any different putting it into a formula versus a separate column. Weird!


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

Hmm, try this:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddFBCoQwDIXhqwxZC21SrXZ2uplDlJ5hRPD+zqID5dfuCl+SkrycxZypM6/p5d/eyyCf43vu6++hUobWFW5wgwd4gI/wET7BJ3iER/j893Cfv7X7hfv8rd3vwdeHfv7P/rl3n+pL7z7VU+8+1ZUBRhYwQV5IGeHCAmaYpJQL", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Timestamp = _t, Group = _t, RunningTotal = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Timestamp", type datetime}, {"Group", type text}, {"RunningTotal", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each DateTime.LocalNow() - [Timestamp])
in
    #"Added Custom"

Perhaps the issue is your time is a Date and not a Date Time?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler good point, so I tried changing [EffectiveDate] to DateTime and it made the formula even worse! 

 

To get around this I have created a [Today] column, and then highlighted both columns > Add Column > Date > Subtract
This got me my value. I wanted to embed the [Today] in one formula though rather than create a separate column. In excel it is literally 

=Sum(Today() - [@EffectiveDate])

 

Anyway, thank you for your suggested solution but I think mine is a little simpler (you might say cruder!)

 

Jemma

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.