cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
phauschke
Frequent Visitor

List.dates between dates in same column

Hi,

sorry I cannot find solution for generating dates between dates in same column.

e.g. Valid from is valid till another row is created for its department.

Dept.Hrs rateValid from
ABC100001.01.2022
ABC150001.01.2023
ABC200001.02.2023
XYZ10001.01.2022
XYZ20001.01.2023

Thx PH

1 ACCEPTED SOLUTION

You are genius. Thx a lot. Just one remark for others if they will want to use it, as first precondition it have to be correctly sorted. Otherwise it create a lot of duplicates.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @phauschke 

 

You can try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjQwMABSBoZ6QGRkYGSkFKsDlzNFlTNGkjNC6DNCyEVERkHMxDQSImWEYWIsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Dept." = _t, #"Hrs rate" = _t, #"Valid from" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Dept.", type text}, {"Hrs rate", Int64.Type}, {"Valid from", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Dept.", "Index"}, #"Added Index1", {"Dept.", "Index.1"}, "Added Index1", JoinKind.LeftOuter),
    #"Expanded Added Index1" = Table.ExpandTableColumn(#"Merged Queries", "Added Index1", {"Valid from"}, {"Added Index1.Valid from"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Added Index1",{{"Dept.", Order.Ascending}, {"Valid from", Order.Ascending}}),
    #"Renamed Columns" = Table.RenameColumns(#"Sorted Rows",{{"Added Index1.Valid from", "Valid to"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Renamed Columns",null,Date.From(DateTime.LocalNow()),Replacer.ReplaceValue,{"Valid to"}),
    #"Added Custom" = Table.AddColumn(#"Replaced Value", "Valid date", each List.Dates([Valid from], Duration.Days([Valid to] - [Valid from]), #duration(1,0,0,0))),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Dept.", "Hrs rate", "Valid date"}),
    #"Expanded Valid date" = Table.ExpandListColumn(#"Removed Other Columns", "Valid date")
in
    #"Expanded Valid date"

 

Steps are:

1. Add two Index columns: one is starting from 1 with step 1 and the other is starting from 0 with step 1. 

2. Merge the query with itself based on Dept. and Index columns. Expand the result table column and select only "valid from" to expand. Rename this column to "valid to".

3. Sort rows by "Dept." and "Valid from" columns ascendingly. 

4. Replace null values in "valid to" column to today's date. 

5. Add a custom column with List.Dates to get dates between "valid from" and "valid to".

6. Remove unnecessary columns. 

7. Expand the date list column generated from step 5. 

 

Result:

vjingzhang_0-1675389093424.png

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

You are genius. Thx a lot. Just one remark for others if they will want to use it, as first precondition it have to be correctly sorted. Otherwise it create a lot of duplicates.

Helpful resources

Announcements
May 2023 update

Power BI May 2023 Update

Find out more about the May 2023 update.

Submit your Data Story

Data Stories Gallery

Share your Data Story with the Community in the Data Stories Gallery.

Top Solution Authors
Top Kudoed Authors