Forum Discussion

Ivy_Lau's avatar
Ivy_Lau
Regular Visitor
1 year ago
Solved

Change data type in Custom function

Hello,   I am new to Power BI and hope someone could help.   I need to split out period from two dates (named [Datefrom] & [Dateto] below) into a series of dates (named [Date.Date] below).   Cu...
  • olgad's avatar
    1 year ago

    let
    Source = (Datefrom as nullable date, Dateto as nullable date) =>
    let
    ConvertedDatefrom = try Date.From(Datefrom) otherwise null,
    ConvertedDateto = try Date.From(Dateto) otherwise null,
    ResolvedDatefrom = if ConvertedDatefrom is null then ConvertedDateto else ConvertedDatefrom,
    ResolvedDateto = if ConvertedDateto is null then ConvertedDatefrom else ConvertedDateto,
    Dates = if ResolvedDatefrom <= ResolvedDateto
    then List.Dates(ResolvedDatefrom, Duration.Days(Duration.From(ResolvedDateto - ResolvedDatefrom)) + 1, #duration(1, 0, 0, 0))
    else List.Dates(ResolvedDateto, Duration.Days(Duration.From(ResolvedDatefrom - ResolvedDateto)) + 1, #duration(1, 0, 0, 0)),
    Output = Table.FromList(Dates, Splitter.SplitByNothing(), {"Date.Date"})
    in
    Output
    in
    Source