6

I am trying to write a Kusto query, where I have a bool variable and based on that variable I want to call different functions. For example:

let flag = true;
let result = iff(flag == "true", function1, function2)
// function1 will return a different table and function2 will return another table.

The above code is not possible because the methods like iff() and case() can only be operated on scalar values.

So, Is there any way that I could achieve this ?

Lokesh Dogga
  • 65
  • 1
  • 7

1 Answers1

11

the common technique is using a union:

union (function1 | where flag), (function2 | where not(flag))

Here is a full example:

let A = datatable(col1:string)["A"];
let B = datatable(col1:string)["B"];
let funcA = view(){
        A
    };
let funcB= view(){
        B
    };
let flag = true;
union (funcA() | where flag), (funcB() | where not(flag))
col1
A

And when the flag is false:

let A = datatable(col1:string)["A"];
let B = datatable(col1:string)["B"];
let funcA = view(){
        A
    };
let funcB= view(){
        B
    };
let flag = false;
union (funcA() | where flag), (funcB() | where not(flag))
col1
B
Avnera
  • 7,088
  • 9
  • 14
  • I have tried this, but it is executing both the functions. But I want only one of them to get executed. – Lokesh Dogga Jan 06 '22 at 16:00
  • This can't be, you are probably doing something different. I added a full example, try it out, and see why your query is different. – Avnera Jan 06 '22 at 22:19
  • If the set of columns returned by funcA is different than the set from funcB, then this Q&A comes in handy: [Dynamically return columns from a kusto function](https://stackoverflow.com/q/67395722/986533) – Konrad Jamrozik Jul 02 '22 at 22:14