locked
If then Statement in M Language RRS feed

  • Question

  • If I have a let statement in M in which a whole bunch of transformations are taking place, is it possible to run a different set of transformations depending on the input parameter that was provided?

    So something like:

    let TabTransform= (filePath as text, sheetName as text) =>
    let
        Source = Excel.Workbook(File.Contents(filePath), null, true),
        #"SourceTab_Sheet" = Source{[Item=sheetName,Kind="Sheet"]}[Data],

    if sheetName = "1" then

      #"Renamed Columns" ....

      #"FilteredRows"...

      etc...

    else

      #"Renamed Columns" ....

      #"FilteredRows"...

      etc...

    Monday, November 30, 2015 12:23 AM

Answers

  • You need to embed the statements with let...in statements. See the example below:

    let
        Source = {1..3},
        Selector = 1,
        #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Filtered Rows" = if Selector=1 then 
                                  let
    			          a= Table.SelectRows(#"Converted to Table", each ([Column1] <> 1)),
                                  	  b= Table.TransformColumns(a,{{"Column1", each Number.Power(_, 2), type number}})
                                  in b
                           else
                                  Table.SelectRows(#"Converted to Table", each ([Column1] <> 2))
    in
        #"Filtered Rows"


    Monday, November 30, 2015 6:14 PM