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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Txtcher
Helper V
Helper V

How to Create Custom Function in PQ Editor

I am creating 6 custom columns within my power query table. Each will have this expression:  Duration.Days([End Date]-[Start Date])

There will also be a lot of if statements added in. First to deal with null dates so I don't get errors, and then outcomes. 

Here is the core of what we want (but note I still need to add if stmts for null date values):

 

varMilestone = Duration.Days([End date]-[Start Date])

If varMilestone <0 then null

else if varMilestone=0 then 1

else Duration.Days(End Date]-[Start Date])

 

How can I set this up as a function I can invoke to create a custom column and then just plug in my column names?  I have never created one before.

TIA.

1 ACCEPTED SOLUTION
Akash_Varuna
Super User
Super User

Hi @Txtcher You could try something like this and modify it for your needs 

let
    CustomFunction = (StartDate as nullable date, EndDate as nullable date) as nullable number =>
        let
            varMilestone = if StartDate = null or EndDate = null then null else Duration.Days(EndDate - StartDate),
            result = 
                if varMilestone = null then null
                else if varMilestone < 0 then null
                else if varMilestone = 0 then 1
                else varMilestone
        in
            result
in
    CustomFunction

After this you can invoke the custom function by adding a custom column with function like this 

= CustomFunction([Start Date], [End Date])

View solution in original post

3 REPLIES 3
Akash_Varuna
Super User
Super User

Hi @Txtcher You could try something like this and modify it for your needs 

let
    CustomFunction = (StartDate as nullable date, EndDate as nullable date) as nullable number =>
        let
            varMilestone = if StartDate = null or EndDate = null then null else Duration.Days(EndDate - StartDate),
            result = 
                if varMilestone = null then null
                else if varMilestone < 0 then null
                else if varMilestone = 0 then 1
                else varMilestone
        in
            result
in
    CustomFunction

After this you can invoke the custom function by adding a custom column with function like this 

= CustomFunction([Start Date], [End Date])

One more quick question:  How would I add to the function that I want the column type to be Int64.Type? (If possible.)

Works like a charm! I will be sure to keep the syntax for writing custom functions handy. 

Thank you so much!

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.