1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

国民経済計算(GDP統計)をPower Queryでそれっぽく変形してみた(小ネタ)

Posted at

Twitter上で話題になってたので叩いて伸ばしてみた

今回は内閣府の
https://www.esri.cao.go.jp/jp/sna/menu.html
四半期GDP実額 実質※をPower Queryが直接アクセスして
叩いて伸ばしてテーブル化します
「民間最終消費支出:除く持ち家の帰属家賃」あたりの処理がおかしい気がしますが
それなりにBIに入れられそうなものになりました

本題(コードレベルで最適化してないので勘弁してね)

image.png

let
    ソース = Csv.Document(Web.Contents("https://www.esri.cao.go.jp/jp/sna/data/data_list/sokuhou/files/2019/qe194/tables/gaku-jk1941.csv"),[Delimiter=",", Columns=33, Encoding=932, QuoteStyle=QuoteStyle.None]),
    変更された型 = Table.TransformColumnTypes(ソース,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}, {"Column13", type text}, {"Column14", type text}, {"Column15", type text}, {"Column16", type text}, {"Column17", type text}, {"Column18", type text}, {"Column19", type text}, {"Column20", type text}, {"Column21", type text}, {"Column22", type text}, {"Column23", type text}, {"Column24", type text}, {"Column25", type text}, {"Column26", type text}, {"Column27", type text}, {"Column28", type text}, {"Column29", type text}, {"Column30", type text}, {"Column31", type text}, {"Column32", type text}, {"Column33", type text}}),
    転置されたテーブル = Table.Transpose(変更された型),
    結合された列 = Table.CombineColumns(転置されたテーブル,{"Column4", "Column5"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"結合済み"),
    置き換えられた値 = Table.ReplaceValue(結合された列,"",null,Replacer.ReplaceValue,{"Column3"}),
    下方向へコピー済み = Table.FillDown(置き換えられた値,{"Column3"}),
    フィルターされた行 = Table.SelectRows(下方向へコピー済み, each ([Column9] <> "")),
    結合された列1 = Table.CombineColumns(フィルターされた行,{"Column3", "結合済み"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"結合済み.1"),
    転置されたテーブル1 = Table.Transpose(結合された列1),
    区切り記号の前に挿入されたテキスト = Table.AddColumn(転置されたテーブル1, "区切り記号の前のテキスト", each Text.BeforeDelimiter([Column1], "/"), type text),
    削除された列 = Table.RemoveColumns(区切り記号の前に挿入されたテキスト,{"区切り記号の前のテキスト"}),
    区切り記号の前に挿入されたテキスト1 = Table.AddColumn(削除された列, "区切り記号の前のテキスト", each Text.BeforeDelimiter([Column1], "/", {0, RelativePosition.FromEnd}), type text),
    置き換えられた値1 = Table.ReplaceValue(区切り記号の前に挿入されたテキスト1,"",null,Replacer.ReplaceValue,{"区切り記号の前のテキスト"}),
    下方向へコピー済み1 = Table.FillDown(置き換えられた値1,{"区切り記号の前のテキスト"}),
    区切り記号の後の抽出されたテキスト = Table.TransformColumns(下方向へコピー済み1, {{"Column1", each Text.AfterDelimiter(_, "/", {0, RelativePosition.FromEnd}), type text}}),
    区切り記号の間の抽出されたテキスト = Table.TransformColumns(区切り記号の後の抽出されたテキスト, {{"Column1", each Text.BetweenDelimiters(_, "-", "."), type text}}),
    変更された型1 = Table.TransformColumnTypes(区切り記号の間の抽出されたテキスト,{{"Column1", Int64.Type}}),
    除算済みの列 = Table.TransformColumns(変更された型1, {{"Column1", each _ / 3, type number}}),
    追加されたプレフィックス = Table.TransformColumns(除算済みの列, {{"Column1", each "第" & Text.From(_, "ja-JP"), type text}}),
    追加されたサフィックス = Table.TransformColumns(追加されたプレフィックス, {{"Column1", each _ & "四半期", type text}}),
    並べ替えられた列 = Table.ReorderColumns(追加されたサフィックス,{"区切り記号の前のテキスト", "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25"}),
    削除された最初の行 = Table.Skip(並べ替えられた列,2),
    フィルターされた行1 = Table.SelectRows(削除された最初の行, each ([Column2] <> "")),
    昇格されたヘッダー数 = Table.PromoteHeaders(フィルターされた行1, [PromoteAllScalars=true]),
    変更された型2 = Table.TransformColumnTypes(昇格されたヘッダー数,{{"Column1", Int64.Type}, {"Column2", type text}, {"国内総生産(支出側):", type text}, {"民間最終消費支出:", type text}, {"民間最終消費支出:家計最終消費支出", type text}, {"民間最終消費支出:除く持ち家の帰属家賃", type text}, {"民間住宅:", type text}, {"民間企業設備:", type text}, {"民間在庫変動:", type text}, {"政府最終消費支出:", type text}, {"公的固定資本形成:", type text}, {"公的在庫変動:", type text}, {"財貨・サービス:純輸出", type text}, {"財貨・サービス:輸出", type number}, {"財貨・サービス:輸入", type number}, {"交易利得:", type text}, {"国内総所得:", type text}, {"海外からの所得:純受取", type text}, {"海外からの所得:受取", type number}, {"海外からの所得:支払", type number}, {"国民総所得:", type text}, {"国内需要:", type text}, {"民間需要:", type text}, {"公的需要:", type text}, {"総固定資本形成:", type text}, {"最終需要:", type text}})
in
    変更された型2

コードの使用法

image.png
空クエリの作成を選択して(今回は宛先がテーブルじゃないのでテーブル範囲は使いません)
image.png
Power Query開いて「表示>詳細エディタ」を開いてコードをすべて上記のものに入れ替えると動きます

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?