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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
wadidaw
Frequent Visitor

Calculate per month in a same row

I need to calculate days per month >>(departure - arrive) +1, but in the table show different month in the same row

I have table like this

wadidaw_5-1721615558188.png

And this is the table result i want to have.

wadidaw_6-1721615573938.png

 

Any suggestions are very useful for me

Thanks.

 

 

4 REPLIES 4
AlienSx
Super User
Super User

@wadidaw why last row days is 16?

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    days = (from as date, to as date) => List.Generate(
        () => [f = from, t = List.Min({to, Date.EndOfMonth(f)})], 
        (x) => x[f] <= to, 
        (x) => [f = Date.AddDays(x[t], 1), t = List.Min({to, Date.EndOfMonth(f)})],
        (x) => {x[f], x[t], Duration.Days(x[t] - x[f]) + 1}
    ), 
    to_list = Table.ToList(
        Source, 
        (x) => days(Date.From(x{0}), Date.From(x{1}))
    ), 
    tbl = Table.FromRows(List.Combine(to_list), Table.ColumnNames(Source) & {"Days"})
in
    tbl
jennratten
Super User
Super User

Hello @wadidaw - this is how you can accomplish this calculation.  Note, the results for the bolded items are the same; the results for the other cells are a little different but the math is correct based on the formula you provided in the description. 

jennratten_0-1721619993139.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WstQ31DcyMDJR0gEyjSHMWB2QuBFC3MgcRcIcSYcBQsbQAGEWkG2KImOKJGMIk4oFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Arrive = _t, Depart = _t]),
    #"Changed Type1" = Table.TransformColumnTypes(Source,{{"Depart", type date}, {"Arrive", type date}}),
    #"Inserted Date Subtraction" = Table.AddColumn(#"Changed Type1", "Subtraction", each Duration.Days([Depart] - [Arrive]) + 1, Int64.Type)
in
    #"Inserted Date Subtraction"

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

Hi @jennratten Thanks for your reply, i need to have additional row every each different month period to add 1st day of month (Arrive) and last day of month (depart). I need to calculate per month

You most recent description of what you are trying to achieve seems very different from the expected result originally posted.  Can you please clarify what it is you are trying to accomplish?  Are you trying to add a new column which returns the number of days between the arrival and departure date - or are you trying to add rows for some sort of monthly calculation?  Perhaps you could reply with another example of the expected result.

Thanks!

 

 

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.