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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
sasdfasdfsad
Advocate II
Advocate II

Count rows in all other queries and return as a single M query?

Is there a way to add a query in Power Query in Transform Data for Power BI where it will count the rows in all the other queries in the data set?

Ideally it would be a way to iterate through and return all the row counts for all the queries.  I'd like to have a way to see row counts after a data refresh all in one location (one query).

Thank you

3 ACCEPTED SOLUTIONS
ToddChitt
Super User
Super User

Do you absolutely need this in Power Query? DAX can do a simple COUNTROWS measure.

But if you insist (and I do NOT recommend):

Duplicate one of the tables. Add a Custom Column with a literal value as the name of the table, like "Table 1". Now do a GROUP BY on that column, and Aggregate of COUNT. Do this for all tables. Finally, do a UNION of these tables (assuming all the column names are identical.

WHY is this approach NOT recommended? Because it DOUBLES your query load! You have to read each table source TWICE!

In DAX, you could try this: Create a DAX table using SUMMARIZE function that does a COUNTROWS. The add to that a ADDCOLUMNS DAX function. Finally, wrap this in a DAX UNION statement with other SUMMARIZE statements. 

Sample DAX table that counts rows in 'Table1':

Table Counts  = ADDCOLUMNS(SUMMARIZE('Table 1', "My Count", COUNTROWS('Table 1')),"Table Name", "Table 1")
I leave it to you to investigate the use of the UNION DAX function.



Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





View solution in original post

Anonymous
Not applicable

You could just do 

 

= Table.RowCount(Query1)+Table.RowCount(Query2)+...+Table.RowCount(QueryN)

 

--Nate

View solution in original post

ronrsnfld
Super User
Super User

As a single line of M-Code:

 

List.Accumulate(
        Record.FieldNames(#sections[Section1]),
        0,
        (x,y)=> try x + Table.RowCount(Expression.Evaluate(y,#shared)) otherwise x)

 

Depending on how you are using this, you may need to subtract Table.RowCount(#"Previous Step") from the value.

 

#sections[Section1] returns a list of the queries as a record

The try ... otherwise is to handle those queries that might not be returning Tables.

View solution in original post

3 REPLIES 3
ronrsnfld
Super User
Super User

As a single line of M-Code:

 

List.Accumulate(
        Record.FieldNames(#sections[Section1]),
        0,
        (x,y)=> try x + Table.RowCount(Expression.Evaluate(y,#shared)) otherwise x)

 

Depending on how you are using this, you may need to subtract Table.RowCount(#"Previous Step") from the value.

 

#sections[Section1] returns a list of the queries as a record

The try ... otherwise is to handle those queries that might not be returning Tables.

Anonymous
Not applicable

You could just do 

 

= Table.RowCount(Query1)+Table.RowCount(Query2)+...+Table.RowCount(QueryN)

 

--Nate

ToddChitt
Super User
Super User

Do you absolutely need this in Power Query? DAX can do a simple COUNTROWS measure.

But if you insist (and I do NOT recommend):

Duplicate one of the tables. Add a Custom Column with a literal value as the name of the table, like "Table 1". Now do a GROUP BY on that column, and Aggregate of COUNT. Do this for all tables. Finally, do a UNION of these tables (assuming all the column names are identical.

WHY is this approach NOT recommended? Because it DOUBLES your query load! You have to read each table source TWICE!

In DAX, you could try this: Create a DAX table using SUMMARIZE function that does a COUNTROWS. The add to that a ADDCOLUMNS DAX function. Finally, wrap this in a DAX UNION statement with other SUMMARIZE statements. 

Sample DAX table that counts rows in 'Table1':

Table Counts  = ADDCOLUMNS(SUMMARIZE('Table 1', "My Count", COUNTROWS('Table 1')),"Table Name", "Table 1")
I leave it to you to investigate the use of the UNION DAX function.



Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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