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
T_Francis
Frequent Visitor

Difference Between dates in number of weeks

Hi, 

 

I'm trying to raise a simple report showing the number of weeks between two dates, I have not been able to find how to do it, could someone please help. 

 

Just a third column showing the number of weeks with 1 as a minimum is all i need.

 

T_Francis_0-1595428357975.png

 

Thank you

 

1 ACCEPTED SOLUTION
FrankAT
Community Champion
Community Champion

Hi @T_Francis 

you can do it within Power Query like this:

// Table
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZBRCsAgDEPv4rfQtHVzO4t4/2usCs4q+BmeaRJLCZKIQQLmEIPiFzWWgIsgTcKYgSE6E8eECfdk8hJ4MPews+yZ+dTdVH9TV9/jfHsX9j0tPB0Y9Oxrxd6TL5+3b/uWDVvesl023/LXM7zWDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"HCT START DATE" = _t, #"HCT PLANNED END DATE" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"HCT START DATE", type date}, {"HCT PLANNED END DATE", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "# of Weeks", each Number.RoundUp(Number.From([HCT PLANNED END DATE] - [HCT START DATE]) / 7,0)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"# of Weeks", Int64.Type}})
in
    #"Changed Type1"

 

23-07-_2020_0-17-44.png

 

Regards FrankAT

View solution in original post

9 REPLIES 9
FrankAT
Community Champion
Community Champion

Hi @T_Francis 

you can do it within Power Query like this:

// Table
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZBRCsAgDEPv4rfQtHVzO4t4/2usCs4q+BmeaRJLCZKIQQLmEIPiFzWWgIsgTcKYgSE6E8eECfdk8hJ4MPews+yZ+dTdVH9TV9/jfHsX9j0tPB0Y9Oxrxd6TL5+3b/uWDVvesl023/LXM7zWDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"HCT START DATE" = _t, #"HCT PLANNED END DATE" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"HCT START DATE", type date}, {"HCT PLANNED END DATE", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "# of Weeks", each Number.RoundUp(Number.From([HCT PLANNED END DATE] - [HCT START DATE]) / 7,0)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"# of Weeks", Int64.Type}})
in
    #"Changed Type1"

 

23-07-_2020_0-17-44.png

 

Regards FrankAT

AntrikshSharma
Super User
Super User

( Date 1 - Date 2 ) / 7 ?

Thank you for responding but this way sends up an error message.

 

T_Francis_0-1595430697079.png

 

az38
Community Champion
Community Champion

@T_Francis 

it still looks like a Custom Column for Power Query interface.

 


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Use the transformation buttons from ribbon, see how I did it.

let
    Source = Excel.CurrentWorkbook(){[Name = "Table23"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(
        Source, 
        {{"Date", type date}, {"Other Date", type date}}
    ),
    #"Inserted Date Subtraction" = Table.AddColumn(
        #"Changed Type", 
        "Subtraction", 
        each Duration.Days([Other Date] - [Date]), 
        Int64.Type
    ),
    #"Divided Column" = Table.TransformColumns(
        #"Inserted Date Subtraction", 
        {{"Subtraction", each _ / 7, type number}}
    ),
    #"Rounded Up" = Table.TransformColumns(
        #"Divided Column", 
        {{"Subtraction", Number.RoundUp, Int64.Type}}
    )
in
    #"Rounded Up"

1.PNG

What the error would be would be useful, that it just says error isn't overly helpful

 

If you're wanting it to show 1 week for 1-7 days, 2 weeks for 8-14 days etc, then this really is a simple case of finding the difference in days, dividing by 7, then using Number.RoundUp (if using Power Query) or ROUNDUP (in DAX)

parry2k
Super User
Super User

@T_Francis you can add new column using dax

 

Number of Weeks = MIN ( DATEDIFF ( Table[Start Date], Table[End Dat], WEEK ), 1 )

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Thank you, 

 

The formula does not seem to be working

 

T_Francis_0-1595430236284.png

 

T_Francis_1-1595430259200.png

 

am i making an error somewhere?

 

I need to use the result on a spreadsheet, so is there a way to do it on power query or do i have to create a measure?

Yes what he wrote is DAX code and you are writing M code.

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.