{"id":21522630,"url":"https://github.com/takeyamajp/powerquery-indexrowsforeachgroup","last_synced_at":"2026-02-20T15:32:02.223Z","repository":{"id":165087503,"uuid":"612487126","full_name":"takeyamajp/PowerQuery-IndexRowsForEachGroup","owner":"takeyamajp","description":"Power Query library to smartly assign sequential numbers for each group.","archived":false,"fork":false,"pushed_at":"2023-04-09T13:34:15.000Z","size":221,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T15:39:43.420Z","etag":null,"topics":["m","powerbi","powerquery"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/takeyamajp.png","metadata":{"files":{"readme":"README.ja.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-11T04:48:17.000Z","updated_at":"2023-11-14T07:39:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"b564c786-e393-45bc-89fb-106758a4c744","html_url":"https://github.com/takeyamajp/PowerQuery-IndexRowsForEachGroup","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/takeyamajp/PowerQuery-IndexRowsForEachGroup","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeyamajp%2FPowerQuery-IndexRowsForEachGroup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeyamajp%2FPowerQuery-IndexRowsForEachGroup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeyamajp%2FPowerQuery-IndexRowsForEachGroup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeyamajp%2FPowerQuery-IndexRowsForEachGroup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takeyamajp","download_url":"https://codeload.github.com/takeyamajp/PowerQuery-IndexRowsForEachGroup/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takeyamajp%2FPowerQuery-IndexRowsForEachGroup/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29655304,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-20T09:27:29.698Z","status":"ssl_error","status_checked_at":"2026-02-20T09:26:12.373Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["m","powerbi","powerquery"],"created_at":"2024-11-24T01:11:12.595Z","updated_at":"2026-02-20T15:32:02.208Z","avatar_url":"https://github.com/takeyamajp.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PowerQuery-IndexRowsForEachGroup\n\n[`English`](https://github.com/takeyamajp/PowerQuery-IndexRowsForEachGroup)  \n`Japanese (日本語)`\n\nもし有用なら、このリポジトリーにスターを付けていただけると励みになります。   \n\nこのライブラリーでは、正確に思った通りの並び順で連番を振ることが出来ます。  \nさらに、このライブラリーは列の型を完全に保持します。\n\n- [サンプルコード](#サンプルコード)\n- [パラメータ説明](#パラメータ説明)\n  - [table](#table)\n  - [key](#key)\n  - [comparisonCriteria](#comparisoncriteria)\n  - [newColumnName](#newcolumnname)\n- [カスタム関数（構文と定義）](#カスタム関数構文と定義)\n  - [IndexRowsForEachGroup](#indexrowsforeachgroupグループ毎に指定した並び順で連番を振ります)\n  - [CountRowsForEachGroup](#countrowsforeachgroupグループ毎の行数をカウントします)\n  - [IndexGroups](#indexgroups指定した並び順でグループに連番を振ります)\n  - [CountGroups](#countgroupsグループ数をカウントします)\n  - [IndexRows](#indexrows各行に連番を振ります)\n  - [CountRows](#countrows行数をカウントします)\n\n# サンプルコード\n[先にカスタム関数を登録しておいてください。](#カスタム関数構文と定義)  \nこのサンプルコードを実行するには、Power Query エディターで空のクエリを作成して、それを詳細エディターで開いてください。  \nそして、既存のコードを以下のサンプル コードで置き換えてください。\n~~~\nlet\n    Source = #table(type table[Region = text, Country = text], {\n        {\"Asia\", \"Australia\"}, \n        {\"Asia\", \"China\"}, \n        {\"Asia\", \"India\"}, \n        {\"Asia\", \"Japan\"}, \n        {\"Europe\", \"France\"}, \n        {\"Europe\", \"Germany\"}, \n        {\"Europe\", \"United Kingdom\"}, \n        {\"North America\", \"Canada\"}, \n        {\"North America\", \"United States\"}\n    }),\n    Custom1 = IndexRowsForEachGroup(Source, \"Region\", {\"Region\", \"Country\"}, \"INDEX1\"),\n    Custom2 = CountRowsForEachGroup(Custom1, \"Region\", \"COUNT1\"),\n    Custom3 = IndexGroups(Custom2, \"Region\", {\"Region\", \"Country\"}, \"INDEX2\"),\n    Custom4 = CountGroups(Custom3, \"Region\", \"COUNT2\"),\n    Custom5 = IndexRows(Custom4, {\"Region\", \"Country\"}, \"INDEX3\"),\n    Custom6 = CountRows(Custom5, \"COUNT3\")\nin\n    Custom6\n~~~\n![Sample Image](sample.png)\n\n# パラメータ説明\n### table\n処理対象のテーブルを指定してください。\n\n### key\n指定したキーで処理するグループを判断します。\n\n単一のキーを指定する場合。\n`\"Region\"`\n\n複数のキーを指定する場合。\n`{\"Region\", \"Country\"}`\n\n### comparisonCriteria\n指定した条件で連番を振ります。\n\n単一の列を指定する場合。\n`\"Region\"` or `{{\"Region\", Order.Ascending}}`\n\n複数の列を指定する場合。\n`{\"Region\", \"Country\"}` or `{{\"Region\", Order.Ascending}, {\"Country\", Order.Descending}}`\n\n### newColumnName\n追加される列の名前です。省略した場合は `@INDEX` or `@COUNT` になります。\n\n\n# カスタム関数（構文と定義）\nカスタム関数を作成するには、Power Query エディターで空のクエリを作成して、以下の関数定義を数式バーに貼り付けてください。\n\n### IndexRowsForEachGroup（グループ毎に、指定した並び順で連番を振ります。）\nIndexRowsForEachGroup(`table` as table, `key` as anynonnull, `comparisonCriteria` as anynonnull, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, key as anynonnull, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table =\u003e Table.Sort(Table.TransformColumnTypes(Table.ExpandTableColumn(Table.SelectColumns(Table.AddColumn(Table.Group(table, key, {{\"_GTABLE\", each _}}), \"_ITABLE\", each Table.AddIndexColumn(Table.Sort([_GTABLE], comparisonCriteria), newColumnName ?? \"@INDEX\", 1, 1)), \"_ITABLE\"), \"_ITABLE\", Table.ColumnNames(table) \u0026 {newColumnName ?? \"@INDEX\"}, Table.ColumnNames(table) \u0026 {newColumnName ?? \"@INDEX\"}), Table.ToRows(Table.SelectColumns(Table.AddColumn(Table.Schema(table), \"Type\", each if [TypeName] = \"Number.Type\" then type number else if [TypeName] = \"Currency.Type\" then Currency.Type else if [TypeName] = \"Int64.Type\" then Int64.Type else if [TypeName] = \"Percentage.Type\" then Percentage.Type else if [TypeName] = \"DateTime.Type\" then type datetime else if [TypeName] = \"Date.Type\" then type date else if [TypeName] = \"Time.Type\" then type time else if [TypeName] = \"DateTimeZone.Type\" then type datetimezone else if [TypeName] = \"Duration.Type\" then type duration else if [TypeName] = \"Text.Type\" then type text else if [TypeName] = \"Logical.Type\" then type logical else if [TypeName] = \"Binary.Type\" then type binary else type any, type any), {\"Name\", \"Type\"})) \u0026 {{newColumnName ?? \"@INDEX\", Int64.Type}}), (if comparisonCriteria is list then comparisonCriteria else {comparisonCriteria}) \u0026 {newColumnName ?? \"@INDEX\"})\n~~~\n\n### CountRowsForEachGroup（グループ毎の行数をカウントします。）\nCountRowsForEachGroup(`table` as table, `key` as anynonnull, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, key as anynonnull, optional newColumnName as nullable text) as table =\u003e Table.TransformColumnTypes(Table.ExpandTableColumn(Table.SelectColumns(Table.AddColumn(Table.Group(table, key, {{\"_GTABLE\", each _}}), newColumnName ?? \"@COUNT\", each Table.RowCount([_GTABLE]), Int64.Type), {\"_GTABLE\", newColumnName ?? \"@COUNT\"}), \"_GTABLE\", Table.ColumnNames(table), Table.ColumnNames(table)), Table.ToRows(Table.SelectColumns(Table.AddColumn(Table.Schema(table), \"Type\", each if [TypeName] = \"Number.Type\" then type number else if [TypeName] = \"Currency.Type\" then Currency.Type else if [TypeName] = \"Int64.Type\" then Int64.Type else if [TypeName] = \"Percentage.Type\" then Percentage.Type else if [TypeName] = \"DateTime.Type\" then type datetime else if [TypeName] = \"Date.Type\" then type date else if [TypeName] = \"Time.Type\" then type time else if [TypeName] = \"DateTimeZone.Type\" then type datetimezone else if [TypeName] = \"Duration.Type\" then type duration else if [TypeName] = \"Text.Type\" then type text else if [TypeName] = \"Logical.Type\" then type logical else if [TypeName] = \"Binary.Type\" then type binary else type any, type any), {\"Name\", \"Type\"})))\n~~~\n\n### IndexGroups（指定した並び順でグループに連番を振ります。）\nIndexGroups(`table` as table, `key` as anynonnull, `comparisonCriteria` as anynonnull, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, key as anynonnull, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table =\u003e Table.Sort(Table.TransformColumnTypes(Table.ExpandTableColumn(Table.SelectColumns(Table.AddIndexColumn(Table.Sort(Table.Group(table, key, {{\"_GTABLE\", each _}}), List.Select((if comparisonCriteria is list then comparisonCriteria else {comparisonCriteria}) \u0026 (if key is list then key else {key}), each List.Contains(if key is list then key else {key}, if _ is list then _{0} else _))), newColumnName ?? \"@INDEX\", 1, 1, Int64.Type), {\"_GTABLE\", newColumnName ?? \"@INDEX\"}), \"_GTABLE\", Table.ColumnNames(table), Table.ColumnNames(table)), Table.ToRows(Table.SelectColumns(Table.AddColumn(Table.Schema(table), \"Type\", each if [TypeName] = \"Number.Type\" then type number else if [TypeName] = \"Currency.Type\" then Currency.Type else if [TypeName] = \"Int64.Type\" then Int64.Type else if [TypeName] = \"Percentage.Type\" then Percentage.Type else if [TypeName] = \"DateTime.Type\" then type datetime else if [TypeName] = \"Date.Type\" then type date else if [TypeName] = \"Time.Type\" then type time else if [TypeName] = \"DateTimeZone.Type\" then type datetimezone else if [TypeName] = \"Duration.Type\" then type duration else if [TypeName] = \"Text.Type\" then type text else if [TypeName] = \"Logical.Type\" then type logical else if [TypeName] = \"Binary.Type\" then type binary else type any, type any), {\"Name\", \"Type\"}))), (if comparisonCriteria is list then comparisonCriteria else {comparisonCriteria}) \u0026 {newColumnName ?? \"@INDEX\"})\n~~~\n\n### CountGroups（グループ数をカウントします。）\nCountGroups(`table` as table, `key` as anynonnull, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, key as anynonnull, optional newColumnName as nullable text) as table =\u003e Table.AddColumn(table, newColumnName ?? \"@COUNT\", each Table.RowCount(Table.Group(table, key, {{\"_GTABLE\", each _}})), Int64.Type)\n~~~\n\n### IndexRows（各行に連番を振ります。）\nIndexRows(`table` as table, `comparisonCriteria` as anynonnull, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table =\u003e Table.AddIndexColumn(Table.Sort(table, comparisonCriteria), newColumnName ?? \"@INDEX\", 1, 1, Int64.Type)\n~~~\n\n### CountRows（行数をカウントします。）\nCountRows(`table` as table, optional `newColumnName` as nullable text) as table\n~~~\n= (table as table, optional newColumnName as nullable text) as table =\u003e Table.AddColumn(table, newColumnName ?? \"@COUNT\", each Table.RowCount(table), Int64.Type)\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakeyamajp%2Fpowerquery-indexrowsforeachgroup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakeyamajp%2Fpowerquery-indexrowsforeachgroup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakeyamajp%2Fpowerquery-indexrowsforeachgroup/lists"}