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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
ArchStanton
Impactful Individual
Impactful Individual

Convert DAX to M Code

My M knowledge is limited so I was wondering if someone could show me how I could write this DAX code as M?

I have several such columns in my data model that are iterating over 50,000 rows - I therefore would like to build these in Power Query beforehand as it will be much less expensive to do so.

Case Length = 
IF (
    'Cases'[statecode] = "Active",
    DATEDIFF (
        IF (
            ISBLANK ( 'Cases'[legacycasecreationdate] ),
            'Cases'[Created On],
            'Cases'[legacycasecreationdate]
        ),
        NOW (),
        DAY
    ),
    DATEDIFF (
        IF (
            ISBLANK ( 'Cases'[legacycasecreationdate] ),
            'Cases'[Created On],
            'Cases'[legacycasecreationdate]
        ),
        'Cases'[Resolution Date],
        DAY
    )
)

Thanks

2 ACCEPTED SOLUTIONS

Hi @ArchStanton,

That doesn't look right. Let's try that again.

  1. Create a new blank query,
  2. rename it to: fxCaseLength,
  3. open the advanced editor select all you see in there
  4. paste the function code shared earlier - in its place

These steps create a function query, named: fxCaseLength

 

Now select the Cases query you want to apply this logic to.

  1. Go to the Add column tab on the ribbon
  2. Press the Invoke Custom Function option

The depicted dialog box shows upm_dekorte_3-1761565488011.png

 

  1. give the column a name: Case Length
  2. select the function query: fxCaseLength
  3. map each argument to a column
    1. switch to column input
    2. select the column name from the drop down

 

Here's what that looks like, in your Cases query, when only the first two arguments were mapped.

m_dekorte_1-1761565122643.png

I hope this is helpful

View solution in original post

Hi @ArchStanton,


Give update a go

(statecode, legacyCreated, createdOn, resolutionDate) =>
let
    start = try Date.From(legacyCreated ?? createdOn) otherwise null, 
    isActive = Text.Proper(statecode) = "Active",
    end = if isActive then Date.From(DateTimeZone.FixedUtcNow())
        else (try Date.From(resolutionDate) otherwise null), 
    days = try Duration.Days(end - start) otherwise null,
    n = if not isActive and Value.Equals(days, 0) then 1 else days
in
    n

 

View solution in original post

14 REPLIES 14
v-saisrao-msft
Community Support
Community Support

Hi @ArchStanton,

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Please show the expected outcome based on the sample data you provided.

Need help uploading data? 

https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

 

Thank you.

v-saisrao-msft
Community Support
Community Support

Hi @ArchStanton,

Have you had a chance to review the solution we shared by @Poojara_D12 @wdx223_Daniel @m_dekorte @Cookistador ? If the issue persists, feel free to reply so we can help further.

 

Thank you.

Poojara_D12
Super User
Super User

Hi @ArchStanton 

let
    Source = #"PreviousStepName",
    CurrentDate = DateTime.LocalNow(),
    AddCaseLength = Table.AddColumn(
        Source,
        "Case Length",
        each 
            let
                StartDate = if [legacycasecreationdate] = null then [Created On] else [legacycasecreationdate],
                EndDate = if [statecode] = "Active" then CurrentDate else [Resolution Date],
                DurationDays = Duration.Days(EndDate - StartDate)
            in
                DurationDays
    )
in
    AddCaseLength

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos"

Kind Regards,
Poojara - Proud to be a Super User
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS

Hi, when I tried your solution I got this:

ArchStanton_0-1761563355157.png

When I open the table I have every column in the database listed

wdx223_Daniel
Super User
Super User

NewStep=Table.AddColumn(YourSource,"Case Length",each Duration.Days((if [statecode]="Active" then DateTime.LocalNow() else [Resolution Date])-([legacycasecreationdate]??[Created On])))

m_dekorte
Super User
Super User

Hi @ArchStanton 

 

Alternatively, you can create a custom function like below. That makes it easy to implement your logic accross different projects, name it: fxCaseLength

(statecode, legacyCreated, createdOn, resolutionDate) =>
let
    start = try Date.From(legacyCreated ?? createdOn) otherwise null, 
    end = if Text.Proper(statecode) = "Active" 
        then Date.From(DateTimeZone.FixedUtcNow())
        else (try Date.From(resolutionDate) otherwise null), 
    days = try Duration.Days(end - start) otherwise null
in
    days

 

Then you can use the Invoke Custom Function option from the Add Column tab. Select the fxCaseLength function  query and set Column inputs for all its arguments.

m_dekorte_1-1761317609145.png

 

 

 

Hi, can you add the folliwing to the code please?
If the Createdon or Legacy Created On date is the same as the Resolution date then it should be 1 not zero.

Thanks


Hi @ArchStanton,


Give update a go

(statecode, legacyCreated, createdOn, resolutionDate) =>
let
    start = try Date.From(legacyCreated ?? createdOn) otherwise null, 
    isActive = Text.Proper(statecode) = "Active",
    end = if isActive then Date.From(DateTimeZone.FixedUtcNow())
        else (try Date.From(resolutionDate) otherwise null), 
    days = try Duration.Days(end - start) otherwise null,
    n = if not isActive and Value.Equals(days, 0) then 1 else days
in
    n

 

Brilliant! 
Thanks so much

Hi, I'm not sure what this solution is going to give me, will I be able to have the Case Length as a column in may main Fact table where I can use DAX on it if necessary?

I have implemented your suggestion but I don't really understand what invoking a custom function is and does.
This is what I can see after following your steps:

ArchStanton_1-1761563825358.png

 



Hi @ArchStanton,

That doesn't look right. Let's try that again.

  1. Create a new blank query,
  2. rename it to: fxCaseLength,
  3. open the advanced editor select all you see in there
  4. paste the function code shared earlier - in its place

These steps create a function query, named: fxCaseLength

 

Now select the Cases query you want to apply this logic to.

  1. Go to the Add column tab on the ribbon
  2. Press the Invoke Custom Function option

The depicted dialog box shows upm_dekorte_3-1761565488011.png

 

  1. give the column a name: Case Length
  2. select the function query: fxCaseLength
  3. map each argument to a column
    1. switch to column input
    2. select the column name from the drop down

 

Here's what that looks like, in your Cases query, when only the first two arguments were mapped.

m_dekorte_1-1761565122643.png

I hope this is helpful

This worked, thank you!!

Cookistador
Super User
Super User

Hello @ArchStanton 

 

The following M measure code should return what you need

Like with the VAR in dax code, we are using variables to get something easier to read

 

let
    StartDate =
        if [legacycasecreationdate] <> null then
            [legacycasecreationdate]
        else
            [Created On] ,

    EndDate =
        if [statecode] = "Active" then
            Date.From(DateTime.LocalNow())
        else
            [Resolution Date],

    DurationInDays =
        if StartDate <> null and EndDate <> null then
            Duration.Days(EndDate - StartDate)
        else
            null
in
    DurationInDays

You just have to paste this code in the pop-up of the creation of a custom column and you will have the desired column

Cookistador_0-1761314555232.png

Hi,
I got this error when I tried:

ArchStanton_0-1761562720565.png

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.