Forum Discussion
Anonymous
5 years agoNot applicable
M Query: Add list of months between dates
I'm trying to add rows for each month between two dates. I found some example code on the forums and tried to adapt it for my purposes, but it isn't working properly. I'm adding a custom column named...
- 5 years ago
Hi, Anonymous , don't bother to use List.Generate(); List.Accumulate() would come in handy in your senario. You might want to try,
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLRN9Q3MjC0BDKN9Y1BbCMDpVidaCUjoIgZRNICyDRFSMYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, award_begin_date = _t, award_end_date = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"award_begin_date", type date}, {"award_end_date", type date}}), #"Added Custom" = Table.AddColumn( #"Changed Type", "col", each let begin = Date.StartOfMonth([award_begin_date]) in List.Accumulate( {0..(Date.Year([award_end_date])-Date.Year([award_begin_date]))*12+(Date.Month([award_end_date])-Date.Month([award_begin_date]))}, {}, (s,c) => s&{Date.AddMonths(begin,c)} ) ), #"Expanded col" = Table.ExpandListColumn(#"Added Custom", "col") in #"Expanded col"
CNENFRNL
5 years agoCommunity Champion
Hi, Anonymous , don't bother to use List.Generate(); List.Accumulate() would come in handy in your senario. You might want to try,
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTLRN9Q3MjC0BDKN9Y1BbCMDpVidaCUjoIgZRNICyDRFSMYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, award_begin_date = _t, award_end_date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"award_begin_date", type date}, {"award_end_date", type date}}),
#"Added Custom" = Table.AddColumn(
#"Changed Type",
"col",
each
let
begin = Date.StartOfMonth([award_begin_date])
in
List.Accumulate(
{0..(Date.Year([award_end_date])-Date.Year([award_begin_date]))*12+(Date.Month([award_end_date])-Date.Month([award_begin_date]))},
{},
(s,c) => s&{Date.AddMonths(begin,c)}
)
),
#"Expanded col" = Table.ExpandListColumn(#"Added Custom", "col")
in
#"Expanded col"
agusmba
1 year agoAdvocate III
Thank you! This is exactly what I was looking for.