iff()

使用 版本 下拉選單切換服務。 了解更多關於導航的資訊。
Apply to: ✅ Microsoft Fabric ✅ Azure Data Explorer ✅ Azure Monitor ✅ Microsoft Sentinel

當 then 條件評估為 if時,傳回 true 值,否則會傳回 else 值。

iff() 和 iif() 函式相等。

語法

iff( if , then , else )

深入瞭解語法慣例。

參數

名字 類型 必填 描述
if string ✔️ 評估為布爾值的表達式。
then 標量 ✔️ 當 if 條件評估為 true時,會傳回其值的表達式。
else 標量 ✔️ 當 if 條件評估為 false時,會傳回其值的表達式。

返回

當 then 條件評估為 if時,此函式會傳回 true 值,否則會傳回 else 值。

例子

使用 iff() 分類數據

下列查詢會使用 iff() 函式,根據其事件類型,將風暴事件分類為「雨事件」或「非雨事件」,然後投影狀態、事件標識符、事件類型,以及新的降雨類別。

StormEvents
| extend Rain = iff((EventType in ("Heavy Rain", "Flash Flood", "Flood")), "Rain event", "Not rain event")
| project State, EventId, EventType, Rain

輸出

下表僅顯示前五個數據列。

州 EventId EventType 雨
大西洋南部 61032 沃特斯普特 非雨事件
佛羅里達州 60904 大雨 雨事件
佛羅里達州 60913 龍捲風 非雨事件
喬治亞 64588 雷暴風 非雨事件
密西西比州 68796 雷暴風 非雨事件
... ... ... ...

將iff() 與其他函式結合

下列查詢會計算莊稼和財產造成的總損失、根據總損壞、直接傷害和直接死亡來分類風暴事件的嚴重性,然後依嚴重性摘要事件總數和事件數目。

StormEvents
| extend TotalDamage = DamageCrops + DamageProperty
| extend Severity = iff(TotalDamage > 1000000 or InjuriesDirect > 10 or DeathsDirect > 0, "High", iff(TotalDamage < 50000 and InjuriesDirect == 0 and DeathsDirect == 0, "Low", "Moderate"))
| summarize TotalEvents = count(), SeverityEvents = count() by Severity

輸出

嚴厲 TotalEvents
低 54805
高 977
溫和 3284