How to call a function with in function based on the passed parameter values

Bexy Morgan 260 Reputation points
2023-11-01T15:22:51.9233333+00:00
.create table inputtable (Country:string, Export:string, Import:string, Rank:int)

.ingest inline into table ["inputtable"] <|
"US","Refined petroleum","Clothing",1
"US","Aircraft parts","Toys",2
"US","Cars","Computers",3
"US","Cars","Computers",4
"Spain","Vehicles","Food",1
"Spain","Vehicles","Food",2
"Spain","Vehicles","Food",3
"Spain","Vehicles","Beverages",4
"Spain","Vehicles","Food",5


.create function
with (docstring = 'Your description', folder='Your folder')
 fun1_select_records(_export: string, _import: string)  
 {
    inputtable 
    | where Export == _export and Import == Import
}

//fun1_select_records(_export = "Cars", _import = "Computers")

.create-or-alter  function
with (docstring = 'Your description', folder='Your folder')
 fun2_select_records(_export: string)  
 {
    inputtable 
    | where Export == _export
    | project Country
}

//fun2_select_records(_export = "Cars")


.create function resultant(Export:dynamic, function_number:int)
{
// Please help how to write this kql Query based on parameter vale for function_number
// if the parameter function_number is given as 1 then fun1_select_records should be executed, 
// if the parameter function_number is given as 2 then fun2_select_records should be executed
}

How to write the resultant( ) function so that when function_number =1 it should run the fun1_select_records;if function_number=2 resultant ( ) should run fun2_select_records

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
576 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bexy Morgan 260 Reputation points
    2023-11-01T23:28:14.71+00:00

    Hi Satish, the query is giving syntax error. Can you please check again, Thanks.

    User's image


  2. Wilko van de Velde 2,236 Reputation points
    2023-11-03T07:56:25.1266667+00:00

    Hi @Bexy Morgan ,

    I didn't found a conditional option (if, case, switch) to use in a Kusto function. The best thing I found was using a union. Downfall is that it will show all columns of the the tables in the union.

    So, I needed to solve this by the narrow operator. Then, removed all columns without any values (I also excluded "##(null)" values which the narrow operator creates of ints) .

    .create-or-alter function resultant(Export:dynamic, function_number:int)
    {        
         //create an empty datatable so the union can be used
        datatable() []
        | union (fun1_select_records(Export, "Computers") | where function_number == 1), 
                (fun2_select_records(Export) | where function_number == 2) 
    	//Use narrow function to exclude empty columns
        | evaluate narrow()
        | where isnotempty(Value) and Value != "##(null)"
    	//pivot the table again to the original format
        | evaluate pivot(Column, any(Value), Row)
        | project-away Row
    }
    
    resultant(Export = "Cars", function_number = 2)
    

    More info:

    https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/unionoperator?pivots=azuredataexplorer

    https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/narrowplugin

    Hope this will help.

    Kind regards,

    Wilko


    Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.