none
How can M use in step N the result from step N+1 ? RRS feed

  • Question

  • Hi,

    I was trying to clean some headers in a file and I noticed something interesting:

    The "Table.Combine" step (6th step) can use the "Table.Skip" step (8th step) in spite of is comes later.

    I imagine it work because the "Table.Skip" step is not directly dependent of the other step, and there is sort of parallel execution.

    If someone who knows could explain.

    Here is the code:

    let
        Source = #table(
    	type table 	[#" id "=Int64.Type, Desc =text], 
    	{
    		{1,"Desc 01"},
    		{2,"Desc 02"},
    		{3,"Desc 03"}
    	}
    ),
        DemoteHeaders = Table.DemoteHeaders(Source),
        HeadersToClean = Table.Transpose(Table.FirstN(DemoteHeaders,1)),
        HeadersClean = Table.TransformColumns(HeadersToClean,{{"Column1", Text.Trim, type text}}),
        TransposeCleanHeaders = Table.Transpose(HeadersClean),
    
    // This step uses the next step result
        Combine = Table.Combine({TransposeCleanHeaders, DataWithoutHeaders}),
        PromoteHeaders = Table.PromoteHeaders(Combine, [PromoteAllScalars=true]),
    
    // this step is used in the previous function
        DataWithoutHeaders = Table.Skip(DemoteHeaders,1),
    
        ChType = Table.TransformColumnTypes(PromoteHeaders,{{"id", Int64.Type}, {"Desc", type text}})
    in
    ChType



    • Edited by anthony34 Thursday, February 8, 2018 11:31 AM
    Thursday, February 8, 2018 11:31 AM

Answers

  • Does it mean for example that I can pass the parameters of a function at the end of the query (they will be called first) while the function is at the begininng ?

    The parameters will be called when the function is called.  The function can be anywhere in the script.

    And second question: I understand that if  a step in not needed for the final "in" result, M is not evaluating it.

    Right. If the chain of execution starting when the expression after "in" does not include one or more steps in the script, those steps are never executed. 

    In this example, how M can now whether a and b are needed since they are called before the function that will need them be called ?

    If a and b are only used in the function, then they will be called when the function is called.

    • Marked as answer by anthony34 Monday, February 12, 2018 8:13 AM
    Friday, February 9, 2018 2:29 PM

All replies

  • Hi Anthony,

    In functional programming, the order of the steps are irrelevant. Steps are executed only when called. In your script, execution begins in the step name indicated after the "in" keyword. So ChType calls PromoteHeaders, which then calls Combine, which then calls DataWithoutHeaders. ChType could be the fist step listed, and the execution path would be exactly the same as it being the last step. So call order is important (we don't want to call Combine before calling PromoteHeaders), but step order is unimportant.

    Thursday, February 8, 2018 5:15 PM
  • Thank you Colin, it is very helpful. I did not know order was not relevant.

    Does it mean for example that I can pass the parameters of a function at the end of the query (they will be called first) while the function is at the begininng ?

    let
       Function(a,b),
       others steps ...,
       a= ...,
       b=...,
       final step that use Function(a,b) results

    in
    final step that use Function(a,b) results

    And second question: I understand that if  a step in not needed for the final "in" result, M is not evaluating it.
    In this example, how M can now whether a and b are needed since they are called before the function that will need them be called ? is M going back during the execution ? Or is M first check what is needed and only then, on a second stage, executes what it decided at the first stage ?


    • Edited by anthony34 Friday, February 9, 2018 7:36 AM
    Friday, February 9, 2018 7:35 AM
  • Does it mean for example that I can pass the parameters of a function at the end of the query (they will be called first) while the function is at the begininng ?

    The parameters will be called when the function is called.  The function can be anywhere in the script.

    And second question: I understand that if  a step in not needed for the final "in" result, M is not evaluating it.

    Right. If the chain of execution starting when the expression after "in" does not include one or more steps in the script, those steps are never executed. 

    In this example, how M can now whether a and b are needed since they are called before the function that will need them be called ?

    If a and b are only used in the function, then they will be called when the function is called.

    • Marked as answer by anthony34 Monday, February 12, 2018 8:13 AM
    Friday, February 9, 2018 2:29 PM
  • Thank you Colin,
    Now that you have explained it, it seems obvious.

    Monday, February 12, 2018 8:39 AM