Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
hiroki
Regular Visitor

functions to smartly assign sequential numbers for each group

Hello guys.

I have wrote functions to assign sequential numbers for each group that everyone wanted but hadn't yet invented.

https://github.com/takeyamajp/PowerQuery-IndexRowsForEachGroup

 

In this library, it is possible to assign sequential numbers in the exact and intended order.
Also, This library fully preserves column type information.

 

Custom Functions

  • IndexRowsForEachGroup(table as table, key as anynonnull, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table
  • CountRowsForEachGroup(table as table, key as anynonnull, optional newColumnName as nullable text) as table
  • IndexGroups(table as table, key as anynonnull, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table
  • CountGroups(table as table, key as anynonnull, optional newColumnName as nullable text) as table
  • IndexRows(table as table, comparisonCriteria as anynonnull, optional newColumnName as nullable text) as table
  • CountRows(table as table, optional newColumnName as nullable text) as table

 

Code Sample

 

let
    Source = #table(type table[Region = text, Country = text], {
        {"Asia", "Australia"}, 
        {"Asia", "China"}, 
        {"Asia", "India"}, 
        {"Asia", "Japan"}, 
        {"Europe", "France"}, 
        {"Europe", "Germany"}, 
        {"Europe", "United Kingdom"}, 
        {"North America", "Canada"}, 
        {"North America", "United States"}
    }),
    Custom1 = IndexRowsForEachGroup(Source, "Region", {"Region", "Country"}, "INDEX1"),
    Custom2 = CountRowsForEachGroup(Custom1, "Region", "COUNT1"),
    Custom3 = IndexGroups(Custom2, "Region", {"Region", "Country"}, "INDEX2"),
    Custom4 = CountGroups(Custom3, "Region", "COUNT2"),
    Custom5 = IndexRows(Custom4, {"Region", "Country"}, "INDEX3"),
    Custom6 = CountRows(Custom5, "COUNT3")
in
    Custom6

 

takeyamajp_0-1678533199330.png

 

1 REPLY 1
Peter_Beck
Resolver II
Resolver II

Thanks for these, I think they will be useful.

 

Cheers,


Peter

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.