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