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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
DataSkills
Resolver I
Resolver I

Creating an age bucket using a custom function

Hello, 
 
I have a need to calculate age buckets for a variety of tables. 
 
So what I have done is:
 
1) Created a custom query called "TodaysDate" to get today's date:
let
Source1 = Date.From(DateTime.LocalNow())
in
Source1

meta [IsParameterQuery=true, Type=date, IsParameterQueryRequired=true]
 
2) I then created a function as follows:
(EndDt as date, StartDt as date) =>
let
    EndDt =TodaysDate,
    Age = Duration.Days(StartDt, EndDt)
in
Age
 
When I invoke the function, 
DataSkills_0-1689338726790.png

 

 
I get...
DataSkills_1-1689338726791.png

 

 
I am attempting to reuse this same Duration.Days(StartDt, EndDt) so that I can simplify creating custom columns as per this example:
 
if Duration.Days(TodaysDate - [CreatedDate]) <=14 then "New"
else if [LastVerifiedDate] is null then "Never Verified"
else if Duration.Days(TodaysDate - [LastVerifiedDate]) <= 30 then "Healthy"
else if Duration.Days(TodaysDate - [LastVerifiedDate]) <= 30 then "Healthy"
else if Duration.Days(TodaysDate - [LastVerifiedDate]) <= 79 then "Moderate"
else if Duration.Days(TodaysDate - [LastVerifiedDate]) >79 then "Aged"
else
"Other"
 
Thank you for assistance. 
 
 
 
1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Duration.Days needs to have only one parameter.

You will need to replace it with

Duration.Days(EndDt-StartDt)

 

Also note, you need not use EndDt as date in your function as anyway you are getting the value from another query. This is redundant. Below would work perfectly

 

(StartDt as date) =>
let
    EndDt =TodaysDate,
    Age = Duration.Days(EndDt - StartDt)
in
Age

The above query can be simply written as

(StartDt as date) => Duration.Days(TodaysDate - StartDt)

 

View solution in original post

2 REPLIES 2
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Duration.Days needs to have only one parameter.

You will need to replace it with

Duration.Days(EndDt-StartDt)

 

Also note, you need not use EndDt as date in your function as anyway you are getting the value from another query. This is redundant. Below would work perfectly

 

(StartDt as date) =>
let
    EndDt =TodaysDate,
    Age = Duration.Days(EndDt - StartDt)
in
Age

The above query can be simply written as

(StartDt as date) => Duration.Days(TodaysDate - StartDt)

 

Thank you Vijay! I am annoyed at myself for missing that obvious error! I was looking in the wrong place!

 

Your improved code also helps so thank you for that too. 😎

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.