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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Ivy_Lau
Regular Visitor

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).

 

Custom function as below:

 

let
Source = (Datefrom as nullable number, Dateto as nullable number) =>
let
Datefrom = if Datefrom is null then Dateto else Datefrom,
Dateto = if Dateto is null then Datefrom else Dateto,
Source = if Datefrom < Dateto
then [Date.Date = {Datefrom .. Dateto}]
else [Date.Date = {Dateto .. Datefrom}]
in Source
in Source

 

Its work. But problem is everytime I need to change the data type of [Datefrom], [Dateto] into number before invoking and change the data type of [Date.Date] back into date then.

Is there any way to include the data type conversion into the function? Or any simpler way? Many thanks!

 

Regards,

Ivy

 

7 REPLIES 7
v-veshwara-msft
Community Support
Community Support

Hi @Ivy_Lau ,
We haven't heard from you since the last response and just wanted to check whether any solution has met your query. If yes, please accept whichever solution has met your query to help others benefit. If no, please feel free to reach out for further assistance.
Thank you.

ronrsnfld
Super User
Super User

Maybe:

 

(dateFrom as nullable date, optional dateTo as nullable date)=>

let
    duration = Number.Abs(Duration.Days(dateTo-dateFrom)??0),
    d1 = List.Min({dateFrom,dateTo}),
    
    dateList = List.Dates(d1,duration+1,#duration(1,0,0,0))
in 
    [Date.Date=dateList]

 

 

olgad
Super User
Super User

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


DID I ANSWER YOUR QUESTION? PLEASE MARK MY POST AS A SOLUTION! APPRECIATE YOUR KUDO/LIKE!
PROUD TO BE A SUPER USER!
Best Stories, Interesting Cases: PowerBI Storytime Newsletter
Linkedin Profile: Linkedin
YouTube Channel: PowerBI Storytime

It solved my problem, thank you!

 

But I just wonder why "try" "otherwise" can handle error in case one below but can't in case two.

 

Just modified the begining :

Ivy_Lau_2-1735322948264.png

 

Case 1 : 

Ivy_Lau_1-1735322722552.png

Case 2 : all the same except changed the data type to date in advance

Ivy_Lau_3-1735323135980.png

Ivy_Lau_0-1735323471168.png

 

Hey,

 

The reason for the error is that the error is already raised at the input. It askes for a date type input:
Source = (Datefrom as nullable date,

if you change this to Dateform as any:
(Datefrom as any, Dateto as any) =>
It will handle every type of value.

Unrelated tip for the following line:
ResolvedDateto = if ConvertedDateto is null then ConvertedDatefrom else ConvertedDateto

 

You can shorten this line using the coalesce function in Power Query using the ?? operator:

ResolvedDateto = ConvertedDatefrom ?? ConvertedDateto ?? null

 

The ?? checks if the first value is null. if so it uses the second value. If that is also null it gives back null.

but I should already changed the data type of the parameters to any (didn't specify the data type in it).

Ivy_Lau_1-1735405330944.png

 


The only difference between two cases should be: 

In case one -
Error occurs when feeding "testing" into Date.From() within the custom function.

"try" "otherwise" can handle this case & return null as result.

In case two -
Error occurs when changing data type to date by Table.TransformColumnTypes () before invoking the custom function. 
"try" "otherwise" can't handle & return error.


If the reason is that the error is already raised at the input for case two. 

I think below should also return error while it return null.

Ivy_Lau_0-1735404470407.png

 

Because the error comes from 2nd step #"Changed Type". Delete this #"Changed Type" step and your function should work.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors