![見出し画像](https://assets.st-note.com/production/uploads/images/72230281/rectangle_large_type_2_27a59d232365aeb16cbe1cc7426db612.png?width=800)
【エクセル パワークエリ】列名を指定せずにすべての列をトリミングする方法【Excel Power Query】
****** 動画による解説 ******
![](https://assets.st-note.com/img/1685762950695-02NdD9lal7.png?width=1200)
Power Query では、『トリミング』という機能を使うことで、列のデータの前後に含まれる空白スペースを削除することができます。
![画像1](https://assets.st-note.com/production/uploads/images/72227583/picture_pc_ec0ae45d31041ee7c3cf187cec040859.png?width=1200)
![画像2](https://assets.st-note.com/production/uploads/images/72227724/picture_pc_4c84a311df089bdb4801c912235fe23b.png?width=1200)
![画像3](https://assets.st-note.com/production/uploads/images/72227877/picture_pc_f9340bcf8fbedd2bb6ed5e13f702ee6f.png?width=1200)
もし、特定の列だけではなく、テーブルに存在するすべての列のデータをトリミングしたいときは、 Ctrl + A ですべての列を選択してからトリミングを実行することで、テーブル全体でトリミングを一度に行うことができます。
![画像6](https://assets.st-note.com/production/uploads/images/72228460/picture_pc_9eeb3c6f0d43aebdd39acb4bd17196f6.png?width=1200)
![画像4](https://assets.st-note.com/production/uploads/images/72228211/picture_pc_7f43c16c3aa5e19dfcf067669e5c124f.png?width=1200)
![画像5](https://assets.st-note.com/production/uploads/images/72228216/picture_pc_2169f932498ccb1be0fdeb661f0460e6.png?width=1200)
![画像7](https://assets.st-note.com/production/uploads/images/72228538/picture_pc_29c1854e3888d06af30980cc3a44200d.png?width=1200)
ただ、この方法はひとつ問題があります。
Ctrl + A ですべての列を選択してから、トリミングを実行したときに生成されるステップの数式は以下の通りです。
![画像8](https://assets.st-note.com/production/uploads/images/72228661/picture_pc_16e538d3a67b47f717c23c9d190c83ec.png?width=1200)
= Table.TransformColumns(ソース,{{"列A", Text.Trim, type text}, {"列B", Text.Trim, type text}, {"列C", Text.Trim, type text}, {"列D", Text.Trim, type text}, {"列E", Text.Trim, type text}})
数式を見るとわかりますが、テーブルに含まれるすべての列の名前を指定してしてデータをトリミングする、というものになっています。
このクエリで取得するテーブルの列構成が常に
『列A、列B、列C、列D、列E』
の5列であるなら問題ありませんが、クエリで取得するテーブルの列数が増えたり減ったり、列名が異なったりする場合、数式で指定する名前の列が存在しないことになりますので、このステップではエラーが発生してしまいます。
![画像9](https://assets.st-note.com/production/uploads/images/72228992/picture_pc_1e114311d954552a2ba893deb9c02307.png?width=1200)
![画像10](https://assets.st-note.com/production/uploads/images/72229004/picture_pc_cf929e6198d5f98a7c017784b08ef24b.png?width=1200)
このように、それぞれの列名を指定した形でトリミングする数式では、列構成が変動するようなテーブルに対応することができません。
そこで、列の名前を指定することなく、テーブルのすべての列でトリミングするためには、『トリムテキスト』のステップの数式を、、、
【変更前】
= Table.TransformColumns(ソース,
{{"列A", Text.Trim, type text}, {"列B", Text.Trim, type text}, {"列C", Text.Trim, type text}, {"列D", Text.Trim, type text}, {"列E", Text.Trim, type text}}
)
以下のように変更します。
よければサポートしていただけると嬉しいです😀 有用な記事や動画を制作していけるように頑張ります❗️