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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
user35131
Helper III
Helper III

How to group by field and create a row count column based on break?

Say i have a table that looks like this

 

Column Alphabet

A
A
A
B
B

B

C

C

D

 

I would like to create a table that looks like this

 

Column Alphabet

Observation

A1
A2
A3
B1
B2

B

3

C

1

C

2

D

1

 

 

1 ACCEPTED SOLUTION
karnold
Resolver I
Resolver I

If you can calculate the column in the source, that would be the best. For example, if the source is SQL Server you could use ROW_NUMBER() OVER (partition by [Column Alphabet] order by (select null)). See Roche’s Maxim of Data Transformation for why this is important. 

 

Here is a method for doing this with a calculated column, which way downstream. First add an index column to your table in Power Query. You can hide this col in the model. So you now have a table that looks like:

A1
A2
A3
B4
B5
B6
C7
C8
D9

 

Then add a calculated column with this DAX:

 

 

Column Count = 
VAR _c1value = CALCULATE( SELECTEDVALUE( 'Table'[Column1] ) )
VAR _Ind = CALCULATE( SELECTEDVALUE( 'Table'[Index] ) )
VAR Result =
    COUNTROWS(
        CALCULATETABLE(
            'Table',
            REMOVEFILTERS( ),
            'Table'[Column1] = _c1value,
            'Table'[Index] <= _Ind
        )
    )
RETURN
    Result

 

 

karnold_0-1653074672618.png

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    Partition = Table.Group(Source, {"Column Alphabet"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Index"}, {"Index"})
in
    #"Expanded Partition"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
CNENFRNL
Community Champion
Community Champion

For fun only,

 

PQ solution,

CNENFRNL_0-1653077095359.png

 

Calculated column solution,

CNENFRNL_1-1653077152143.png

 

Measure solution,

CNENFRNL_2-1653077210988.png

 

SQL solution,

CNENFRNL_0-1653108853094.png

 


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

karnold
Resolver I
Resolver I

If you can calculate the column in the source, that would be the best. For example, if the source is SQL Server you could use ROW_NUMBER() OVER (partition by [Column Alphabet] order by (select null)). See Roche’s Maxim of Data Transformation for why this is important. 

 

Here is a method for doing this with a calculated column, which way downstream. First add an index column to your table in Power Query. You can hide this col in the model. So you now have a table that looks like:

A1
A2
A3
B4
B5
B6
C7
C8
D9

 

Then add a calculated column with this DAX:

 

 

Column Count = 
VAR _c1value = CALCULATE( SELECTEDVALUE( 'Table'[Column1] ) )
VAR _Ind = CALCULATE( SELECTEDVALUE( 'Table'[Index] ) )
VAR Result =
    COUNTROWS(
        CALCULATETABLE(
            'Table',
            REMOVEFILTERS( ),
            'Table'[Column1] = _c1value,
            'Table'[Index] <= _Ind
        )
    )
RETURN
    Result

 

 

karnold_0-1653074672618.png

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.