LoginSignup
1
2

PowerQueryで文字列から型を返す

Last updated at Posted at 2023-11-03

例えば、通貨型を返す式の文字列から、通貨型そのものを得るには...
Expression.Evaluate("Currency.Type", #shared)
とすれば良い。

#sharedが必要なことがわからなくてハマったのでメモ。

列名と型の組み合わせをExcelのテーブルに保存しておき、取得して利用したかったのだが、型を直接表現できないので、このようなことが必要になった。

Name TypeExpression
Id Int64.Type
品名 Text.Type
単価 Currency.Type

これを利用して、既存のテーブルから、Table.TransformColumnTypes関数の第2引数typeTransformationsに入力するリストを生成する関数を作ってみた。

// TypeTransformationsFromTable
(table as table) =>
let
    schema = Table.Schema(table),
    transform = (document) => Expression.Evaluate(document, #shared)
in
    List.Zip({schema[Name], List.Transform(schema[TypeName], transform)})
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2