Forum Discussion

ArchStanton's avatar
ArchStanton
Power Participant
9 months ago
Solved

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 the...
  • m_dekorte's avatar
    m_dekorte
    9 months ago

    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 up

     

    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.

    I hope this is helpful

  • m_dekorte's avatar
    m_dekorte
    9 months ago

    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