Don't miss your chance to take exam DP-600 or DP-700 on us!
Request nowLearn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register 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!
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
Check out the February 2026 Power BI update to learn about new features.
| User | Count |
|---|---|
| 9 | |
| 9 | |
| 9 | |
| 8 | |
| 6 |