Search code examples
powerbipowerquery

How to add a dropdown list with parameters that user chooses and append to API request?


In a power query custom connector I can make the user input some text that gets appended to the url to an API request.

// Example in the navigator:
(optional time_range as text) as table => GetSomething(time_range)

// And the actual function
GetSomething = (optional time_range as text) =>
    let 
        _time_range = if time_range <> null then time_range else "medium_term",
        options = [Headers = [#"Content-Type" = "application/Json"], Query = [limit = "50", time_range=_time_range]],
        source = Web.Contents("https://example.com/v1/me/", options),
        json = Json.Document(source),
        listOfItems = json[items]
    in
        listOfItems;

My goal would be to make the user select from a dropdown list with those three values (short, medium, long), instead of making him write and possibly compromise the request.

Do you know how to achieve this?


Solution

  • So, I found the answer in this blogpost: https://blog.crossjoin.co.uk/2014/11/27/specifying-allowed-values-sample-values-and-descriptions-for-function-parameters-in-power-querypart-1/

    // just add a type for the parameters and enter AllowedValues
    GetSomethingParamType = type text 
                                meta [Documentation.Description = "Please enter a time range",
                                Documentation.AllowedValues = {"short_term","medium_term","long_term"}],
    
    // the type for the function
    GetSomethingType = type function(
                                               _time_range as GetSomethingParamType) 
                                        as list,
    
    // the type replacement
    GetSomethingV2 = Value.ReplaceType(GetSomething, 
                                                       GetSomethingType)