Forum Discussion

otravers's avatar
otravers
Icon for Community Champion rankCommunity Champion
9 years ago
Solved

Can query parameters be applied to functions?

I have a function that retrieves data from a web API. One of the fields to be passed to the API accepts just two values, "10-Q" and "10-K". I've read the blog posts about parameters and created a parameter list with these two values, but is there a way to use that parameter right in the function definition (i.e. not in a downstream query)? This would work as a form of data validation when the function is invoked.

 

Screenshot below to try and clarify what I mean:

 

 

 

  • Hi otravers

     

    I think that you can put your variable as part of your function in your code below.

     

    let
        Source = (ticker as text, FormTypes as text, order as text) as table =>
    let
        Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & #"ticker" & "/balancesheet?formType=" & #"FormTypes" & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),
        #"Converted to Table" = Record.ToTable(Source),
        #"Transposed Table" = Table.Transpose(#"Converted to Table"),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table")
    in
        #"Promoted Headers"
    in
        Source

    What I have done is to change it from your parameters in your Source line which might work?

     

        Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & #"ticker" & "/balancesheet?formType=" & #"FormTypes" & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),

     

6 Replies

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

      OK, I did some more reading on function syntax but didn't find how to "just code" the acceptable values into the function using an array as you suggested. May I ask where you'd put it in the following code?

       

      let
          Source = (ticker as text, FormTypes as text, order as text) as table =>
      let
          Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & ticker & "/balancesheet?formType=" & FormTypes & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),
          #"Converted to Table" = Record.ToTable(Source),
          #"Transposed Table" = Table.Transpose(#"Converted to Table"),
          #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table")
      in
          #"Promoted Headers"
      in
          Source
      • GilbertQ's avatar
        GilbertQ
        Icon for Super User rankSuper User

        Hi otravers

         

        I think that you can put your variable as part of your function in your code below.

         

        let
            Source = (ticker as text, FormTypes as text, order as text) as table =>
        let
            Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & #"ticker" & "/balancesheet?formType=" & #"FormTypes" & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),
            #"Converted to Table" = Record.ToTable(Source),
            #"Transposed Table" = Table.Transpose(#"Converted to Table"),
            #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table")
        in
            #"Promoted Headers"
        in
            Source

        What I have done is to change it from your parameters in your Source line which might work?

         

            Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & #"ticker" & "/balancesheet?formType=" & #"FormTypes" & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi otravers

     

    Try this code:

    = (ticker as text) as table =>
    let
    FormTypes = "10-Q",
    order = "10-K", 
    
        Source = Json.Document(Web.Contents("https://services.last10k.com/v1/company/" & ticker & "/balancesheet?formType=" & FormTypes & "&filingOrder=" & order, [Headers=[#"Ocp-Apim-Subscription-Key" = "redacted"]])),
        #"Converted to Table" = Record.ToTable(Source),
        #"Transposed Table" = Table.Transpose(#"Converted to Table"),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table")
    in
        #"Promoted Headers"

    I couldn't test it properly, as I don't have permission for this site.

     

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

      Hp Pfister, order is a separate variable. What you're doing just hardcodes "10-Q" as the value for FormTypes, while what I'm looking for is a way to restrict input for FormTypes to its two valid values, 10-Q and 10-K.

       

      smoupre, thanks but it looks too complicated for my purposes. I'll live with M's limitations :)