Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!
Hi all,
I need some guidance on this user-defined M-function.
Based on a "Status" I want to calculate a usage time of an application into a new column
Can you please give a hint?
The Code:
= (appstat as text, sdat as date, edat as date) =>
let
Source = if appstat="Offline"
then Duration.Days(Duration.From(([edat]-[sdat])))
else if appstat="online"
then Duration.Days(Duration.From((Date.From(DateTime.LocalNow())-[sdat])))
else if appstat="planned"
then 0
else 0
in
Source
Solved! Go to Solution.
I think I 've found the error by myself ....
The code was adopted from a former Code based on fields. It used [bracketed] variables as if they were fields.
Mixed up variables with fields..., shame on me.
The corrected code works fine:
= (appstat as text, sdat as date, edat as date, heute as date) =>
let
Source = if appstat="Offline"
then Duration.Days(Duration.From((edat-sdat)))
else if appstat="online"
then Duration.Days(Duration.From(heute-sdat))
else if appstat="planned"
then 0
else 0
in
Source
I think I 've found the error by myself ....
The code was adopted from a former Code based on fields. It used [bracketed] variables as if they were fields.
Mixed up variables with fields..., shame on me.
The corrected code works fine:
= (appstat as text, sdat as date, edat as date, heute as date) =>
let
Source = if appstat="Offline"
then Duration.Days(Duration.From((edat-sdat)))
else if appstat="online"
then Duration.Days(Duration.From(heute-sdat))
else if appstat="planned"
then 0
else 0
in
Source
Hi @thomasreick ,
Just out of interest, is there a reason that you're using a function for this rather than just creating a basic custom column?
Pete
Proud to be a Datanaut!
Hi BA_Pete,
first of all: I am quite new in PBI/M/DAX and come from MS-Access/VBA-World.
Reason(s) why:
e.g. I use this function to set up an "envoked cusom function" to write data to a column. While fiddling around with the function (e.g. avoiding NULL-Fields) you can see immediate effects.
Using functions separates the Business-Logic from Data and gives room enough for try-and-error.
In my actual project I have to deal with LONG field names which makes writing expressions not handy.
Fair enough. I was just concerned that you may have been continuing an inherited process/procedure unnecessarily.
In my experience, functions tend to be laborious to create/maintain, and resource-intensive at runtime, but to each their own.
Thanks for taking the time to respond 🙂
Pete
Proud to be a Datanaut!
Vote for your favorite vizzies from the Power BI World Championship submissions!
If you love stickers, then you will definitely want to check out our Community Sticker Challenge!
Check out the January 2026 Power BI update to learn about new features.
| User | Count |
|---|---|
| 11 | |
| 9 | |
| 6 | |
| 6 | |
| 6 |