Power Query workout - Type operator

いちど整理して理解しておくだけでも十分。
# Type operator
型演算子 "かたえんざんし" と呼ぶより タイプ演算子 "たいぷえんざんし" と呼ぶことが多いかな
is / as の評価式、いずれも プリミティブ データタイプを右辺で
*any*, *null*, *logical*, *number*, *time*, *date*, *datetime*, *datetimezone*, *duration*, *text*, *binary*, *type*, *list*, *record*, *table*, *function*, *anynonnull*, *none*

- *any* だけ *null* を許容する
- *any* 以外は *null* を許容しない

ほか、*action* も定義されているけれども。 

## is
左辺の値について右辺のタイプに互換性があるかの検査

```text
1 is number     // true
"a" is text     // true
1 is text       // false

[] is record    // true
{} is list      // true

null is number  // false
null is nullable number // true
null is any     // true
null is anynonnull  // false
```

## Type.Is
```text
Type.Is( Value.Type( 1 ), type number )     // true
Type.Is( Value.Type( 1 ), Number.Type )     // true
Type.Is( Value.Type( 1.5 ), Int64.Type )    // true
Type.Is( Value.Type( "a" ), type text )     // true
Type.Is( Value.Type( 1 ), type text )       // false

Type.Is( Value.Type( [] ), type record )    // true
Type.Is( Value.Type( {} ), type list )      // true

Type.Is( Value.Type( null ), type number )  // false 
Type.Is( Value.Type( null ), type nullable number ) // true

Type.Is( Value.Type( null ), Int64.Type )   // false

Type.Is( type none , Type.NonNullable( type null ) )    // true
Type.Is( None.Type , Type.NonNullable( type null ) )    // true

Type.Is( type null , Type.NonNullable( type null ) )    // false
```

## as
タイプ アサーション
内部的に is 演算子 による評価。互換性があるとき、右辺の値がそのまま返る。

```text
( 1 as number ) = 1     // true
( "a" as text ) = "a"   // true
( null as nullable number ) = null  // true
( null as nullable text ) = null    // true

( 1 as any ) = 1        // true
( "a" as any ) = "a"    // true

( 1 as anynonnull ) = 1     // true

( [] as record ) = []   // true
( {} as list ) = {}     // true
( {1} as list ) = {1}   // true
```
互換性がない場合、Expression.Error

```text
"a" as number   // Expression.Error
------
[
    Reason = "Expression.Error",
    Message = "We cannot convert the value "a" to type Number.",
    Detail = [
        Value = "a",
        Type = type number
    ]
]
```

## Value.As

```text
Value.As( 1, type number ) = 1  // true
Value.As( 1, Number.Type ) = 1  // true
Value.As( 1.5, Int64.Type ) = 1.5   // true

Value.As( null, type nullable number ) = null   // true

Value.As( "a", type text ) = "a"    // true
Value.As( "a", Text.Type ) = "a"    // true
```

```text
Value.As( null, Type.NonNullable( type nullable number ) )
// Expression.Error
------
[
    Reason = "Expression.Error",
    Message = "We cannot convert the value null to type Number.",
    Detail = [
        Value = null,
        Type = type number
    ]
]
```

# UDF
```text
( x as number ) as number => x
( ( x as number ) as number => x )(1) = 1   // true
( ( x as nullable number ) => x )( null ) = null    // true
( ( x as nullable number ) as nullable number => x )( null )    // true
```
```text
( ( x as number ) as number => x )( null )  // Expression.Error
------
[
    Reason = "Expression.Error",
    Message = "We cannot convert the value null to type Number.",
    Detail = [
        Value = null,
        Type = type number
    ]
]
```

```text
( x as any ) =>
    List.First(
        List.Skip(
            {
                [Type = type table,   Value = "Table"],
                [Type = type record,  Value = "Record"],
                [Type = type list,    Value = "List"],
                [Type = type number,  Value = "Number"],
                [Type = type logical, Value = "Logical"],
                [Type = type text,    Value = "Text"],
                [Type = type null,    Value = "Null"],
                [Type = type any,     Value = "Any"]
            },
            each not Type.Is( Value.Type( x ), _[Type] )
        )
    )[Value]
```

# その他
https://powerbi.connpass.com/

- [Power BI 勉強会 - connpass](https://powerbi.connpass.com/)
- [Power BI Desktop を使い始めるには](http://qiita.com/PowerBIxyz/items/6c4735cb5b5712e70981)
- [Excel で Power Query を使い始めるには](http://qiita.com/PowerBIxyz/items/52e563037b855a099ca9)