Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreWe've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. 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!
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 2 |
| User | Count |
|---|---|
| 8 | |
| 7 | |
| 6 | |
| 6 | |
| 5 |