Forum Discussion

vincentakatoh's avatar
vincentakatoh
Icon for Helper IV rankHelper IV
9 years ago
Solved

covert DAX to M language

Hi, 

Need help to convert below DAX to M language.

 

Afterwhich, how do i add the M language code? In a) "Add Custom Column" or b) "Advanced Editor"?

 

MaxAttempts =
CALCULATE (
    MAX ( 'Table1'[attempts] ),
    FILTER (
        'Table1',
        'Table1'[Student] = EARLIER ( 'Table1'[Student] )
            && Table1[subject] = EARLIER ( 'Table1'[subject] )
    )
)

 

  • ImkeF's avatar
    ImkeF
    9 years ago

    Sure: https://1drv.ms/u/s!Av_aAl3fXRbehasTVNp_izcVIvgFrA

     

    With a couple of million rows, load might be faster with the "fast" approach (using Table.Group).

     

    Memory-wise: Currently you could skip column "Attempts" and count the rows instead (List.Count instead of List.Max) - actually this might speed up load as well. But your sample data might give a too simplified view of your actual situation.

  • ImkeF's avatar
    ImkeF
    9 years ago

    No worries! You have to reference the previous step (that was "Source" in my query and is #"Changed Type" in yours):

     

    let
        Source = Excel.Workbook(File.Contents("C:\Users\Final Test Results.xlsx"), null, true),
        #"Raw data_no results_Sheet" = Source{[Item="Raw data_no results",Kind="Sheet"]}[Data],
        #"Promoted Headers" = Table.PromoteHeaders(#"Raw data_no results_Sheet", [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Student", Int64.Type}, {"Subject", type text}, {"Attempts", Int64.Type}, {"test result", type text}})
        #"Custom1" = Table.Join(#"Changed Type", {"Student", "Subject"}, Table.Group(#"Changed Type", {"Student", "Subject"}, {{"Max", each List.Max(_[Attempts])}}), {"Student", "Subject"}),
    in
        #"Custom1"

     

8 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    My suspicion is that is going to be fairly difficult to convert to M language due to the fact that M essentially deals with things at a row level primarily. For the first part of that, you could add a custom column with a Table.Max expression perhaps. In any event, perhaps ImkeF has some ideas for you.

    • ImkeF's avatar
      ImkeF
      Icon for Community Champion rankCommunity Champion

      You can do both :-)

       

      a) resembles more the DAX-logic but is slow:

       

      = Table.AddColumn(Table1, "Slow", each 
      List.Max(Table.SelectRows(Table1, (table1)=>
      				table1[Student]=[Student] and 
      				table1[Subject]=[Subject])
      			  [Attempts])
      )

      If you do this via the UI, you just have to paste the code from row 2-5 into the dialogue-field.

       

      b) is fast and a bit more advanced:

       

      = Table.Join(Table1, {"Student", "Subject"}, 
      Table.Group(Table1, {"Student", "Subject"}, {{"Max", each List.Max(_[Attempts])}}), {"Student", "Subject"}
      )

      This row has to be entered into the advanced editor or the formula-bar in the query editor.

      • vincentakatoh's avatar
        vincentakatoh
        Icon for Helper IV rankHelper IV

        Hi ImkeF

         

        Tried both A) & B) but get error messages for both. Can u send me a sample pbix with both? Thanks. 

         

        My current solution uses "Calculated Column" (DAX). Was hoping to use "Custom Column" (M), thinking that M will take less system resource than DAX. For this case, am I correct? Actual data is a few millions rows. As such, system resource is important. 

         

         

         

        StudentSubjectAttemptstest result
        111Test_A1Fail1
        111Test_A2Fail2
        111Test_A3Fail3
        111Test_A4Fail4
        111Test_B1Fail1
        111Test_B2Fail2
        111Test_B3Pass3
        222Test_A1Fail1
        222Test_A2Fail2
        222Test_A3Fail3
        222Test_B1Pass1
        333Test_C1Fail1
        333Test_C2Pass2