Forum Discussion
M Query: Add list of months between dates
- 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"
It's literally just three columns:
ID award_begin_date award_end_date
1 4/1/2019 3/31/2020
2 6/1/2018 5/31/2020
I want it to expand to be:
ID award_begin_date award_end_date DateRange
1 4/1/2019 3/31/2020 4/1/2019
1 4/1/2019 3/31/2020 5/1/2019
1 4/1/2019 3/31/2020 6/1/2019
1 4/1/2019 3/31/2020 7/1/2019
1 4/1/2019 3/31/2020 8/1/2019
1 4/1/2019 3/31/2020 9/1/2019
1 4/1/2019 3/31/2020 10/1/2019
1 4/1/2019 3/31/2020 11/1/2019
1 4/1/2019 3/31/2020 12/1/2019
1 4/1/2019 3/31/2020 1/1/2020
1 4/1/2019 3/31/2020 2/1/2020
1 4/1/2019 3/31/2020 3/1/2020
2 6/1/2018 5/31/2020 6/1/2018
2 6/1/2018 5/31/2020 7/1/2018
...
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"
- Anonymous5 years agoNot applicable
Brilliant! Thank you!
- JMelo4 years agoFrequent Visitor
This was great, thank you for sharing this!!
It also solved my problem.
But I have this challenge to create one row for each individual day (as opposed one for for month). I'm trying to tweak the code, but it's givning me errors.
Could you please assist?
Much appreciated!
- crossjdesign4 years agoRegular Visitor
JMelo just use the List.Dates function for this, its built in to give you eactly what your lookign for, a list of all dates between a from and to date.
- mherrerag2 years agoRegular Visitor
Life saver! Worked perfectly.
- agusmba1 year agoAdvocate III
Thank you! This is exactly what I was looking for.