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
sasdfasdfsad
Helper II
Helper 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

watkinnc
Super User
Super User

You could just do 

 

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

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

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.

watkinnc
Super User
Super User

You could just do 

 

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

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
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
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.