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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
thomasreick
Resolver I
Resolver I

User defined M-Function throws error, cannot interpret Error-Message

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

  • When running this on "Offline" ist seems to work.
  • Proceeding to "Online" or "Planned" an error is thrown: ExpressionError: There is an unkown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

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

1 ACCEPTED SOLUTION
thomasreick
Resolver I
Resolver I

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

 

View solution in original post

4 REPLIES 4
thomasreick
Resolver I
Resolver I

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

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:

  • Practicing for dealing with M in PowerQuery
  • Reusable if having a good blueprint
  • Easier to maintain in terms of shorting the code
  • I've been used to work on and with functions (VBA)

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.

@thomasreick ,

 

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



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors