Web上のフィードをPower Queryで直接取り込み、タイトルや概要などの主要項目だけを抽出して整形する方法をご説明いたします。サイトURLや列名はダミーに変更し、実データと重ならないよう配慮しております。
目次
Mコード例(Atomフィードを想定)
「詳細エディター」に下記を記述してください。Atomの代表的な要素である title、summary、updated を取り出し、入れ子構造のテキストを平坦化します。
let
// 1) フィード取得
SourceBinary = Web.Contents("https://example.org/sample-atom.xml"),
// 2) XMLをテーブル化
RootTable = Xml.Tables(SourceBinary),
// 3) feed → entry テーブルへ到達
FeedTable = RootTable{0}[feed],
EntryTable = FeedTable{0}[entry],
// 4) 必要な列だけを選択
Picked = Table.SelectColumns(EntryTable, {"title", "summary", "updated"}),
// 5) 入れ子のXMLテーブルからテキストを取り出す補助関数
GetText = (x as any) as nullable text =>
if x is table then
Text.Combine(Table.Column(x, "Text"), "")
else
try Text.From(x) otherwise null,
// 6) テキスト化
ToText = Table.TransformColumns(
Picked,
{
{"title", each GetText(_), type text},
{"summary", each GetText(_), type text},
{"updated", each GetText(_), type text}
}
),
// 7) 日付型が必要な場合は変換(ISO8601想定)
Typed = Table.TransformColumnTypes(ToText, {{"updated", type datetimezone}})
in
Typed
Mコード例(RSSフィードを想定)
RSSでは channel/item 配下に記事が並ぶ構造が一般的です。title、description、pubDate、link を抽出します。
let
// 1) フィード取得
SourceBinary = Web.Contents("https://example.org/sample-rss.xml"),
// 2) XMLをテーブル化
RootTable = Xml.Tables(SourceBinary),
// 3) rss → channel → item へ到達
ChannelTable = RootTable{0}[channel],
ItemTable = ChannelTable{0}[item],
// 4) 必要列の選択
Picked = Table.SelectColumns(ItemTable, {"title", "description", "pubDate", "link"}),
// 5) 入れ子テキスト抽出の補助関数
GetText = (x as any) as nullable text =>
if x is table then
Text.Combine(Table.Column(x, "Text"), "")
else
try Text.From(x) otherwise null,
// 6) テキスト化
ToText = Table.TransformColumns(
Picked,
{
{"title", each GetText(_), type text},
{"description", each GetText(_), type text},
{"pubDate", each GetText(_), type text},
{"link", each GetText(_), type text}
}
),
// 7) 日付型が必要な場合は変換(RFC822等の表記はFromTextで取り込める場合があります)
Final = Table.TransformColumnTypes(ToText, {{"pubDate", type datetime}})
in
Final
実務上の注意点
- 入れ子の列
Xml.Tablesは要素ごとに小さなテーブルを返します。上記のGetTextのような補助関数でText列を連結し、平坦化します。 - 列名の差異
サイトによりsummaryがcontentであったり、updatedがpublishedであったりします。Table.SelectColumnsの列名は対象フィードに合わせて調整してください。 - 型の明示
並べ替えや集計を行う場合は、日時や数値の型をTable.TransformColumnTypesで明示します。 - 認証やリダイレクト
認証が必要なURLやリダイレクトが多いURLではWeb.Contentsのオプション設定や組織のデータ接続ポリシーが影響する場合がございます。必要に応じて管理者にご確認ください。
まとめ
Web.ContentsとXml.Tablesを組み合わせ、AtomやRSSからテーブルを取得できます。- 入れ子構造は補助関数でテキストを抽出し、必要な列だけを平坦化します。
- 列名や型を環境に合わせて調整することで、安定した取り込みと後段の分析が容易になります。
