transcript

excelisfun - Power Query Running Count Based on Row Condition – Excel Magic Trick 1588

Thumbnail of the video “excelisfun - Power Query Running Count Based on Row Condition – Excel Magic Trick 1588”
Download Markdown
Community upload. AI output has not been independently verified.

If your data is already in an Excel worksheet, it is much easier to use COUNTIFS than Power Query. But if you are building a Power Pivot, Power BI solution, or importing a lot of complicated data, then Power Query is the right tool. So let's see how to do this.

A running count, like a formula in an Excel worksheet that you copy down so each row gets a count for particular items, is easy to do in a worksheet. But how do we do it in Power Query? The first thing about a running count is that there has to be some order to the items in the column for it to make sense. We have a transaction number, but it could be a date, a time, or something similar.

This data set is already in an Excel table, so we go to Data, Get and Transform from Table. That opens the Power Query window. We name the query something like "Product Running Count" and press Enter. We have two steps, Source and Changed Type. We are going to create a custom column and build some custom Power Query functions.

Here is the thing about Power Query: we don't have cell references that let us look at the whole column, part of the column, or an individual row. In Excel we can just use cell references, an expandable range that gets bigger as we copy down. In Power Query, to create an expandable range, we have to use Table.SelectRows. That means for every single row where we make a calculation, we need the full table and must actually filter it. When we try to get a conditional criteria from the row, we have to do something different than look at the inside table; we have to look back to the outside table. These concepts are foreign to Excel people because it is so easy to simply use certain types of cell references.

Step one is that we have to sort this column so that when we refresh, the sort in the Excel worksheet will carry through correctly. I click the drop-down and choose Sort Descending, and I can see my new step.

Next, this is the outside table, but we have to repeat this entire table for every row. If we just put the name of the table and copied it down, every row would have to look back to the outside table. So we add a step and buffer the table. Buffering means it loads once and then uses that loaded table for every row. I use the fx button to add a new step. There is the name of the previous step, which is a full table, Table., and from the drop-down I choose Table.Buffer with its parentheses. I click over to Applied Steps, press F2, rename the step "Buffer Table," and press Enter.

Now we go to Add Column, Custom Column. We name the new column and, to remind you how custom columns work, we fill the column with ones and click OK. In the formula bar, we can see that the custom column uses the Table.AddColumn function. "Buffer Table" here means it refers to the full table from the previous step. That is the name of this new column.

The word "each" is a special word in M code that is shorthand for a custom function. Because we are going to use two custom functions back to back, we want to explicitly define the functions rather than use "each." We define the variable in parentheses, then use the go-to operator, the equals sign followed by greater than. All of that explicitly does what "each" did. If I press Enter right now, it says: for that function, go to each row and put a 1. That is not what we want, because first we want to filter a table. So I type Table.SelectRows, and the first argument is the buffer table. The condition is where we need to get a column from the inside buffer table. Remember, that table is the full table in each row, and we have to get a column from it and compare it to whatever is in the row from the outside table.

We define our second function, which will be the inside table, with the go-to operator. We need a full column from this table, then the condition from the outside table. We say "inside table" with square brackets around the column name, and we need to spell "Transaction Number" correctly. I click Expand. Now we run our condition with our comparison operators: for the full column, how many values are less than or equal to the outside table's value? We repeat the same column name. Now for every row, because we have each row, it always sees the actual number, and for the inside table it always has the full column. When we press Enter and click down through the rows, there is the expandable range: the first rows show Quad, then Carlota, and so on. That is not quite what we want. For example, here we only want Quad to count Quad. So after our first condition, we use the "and" logical operator, all lowercase, and add: for the inside table's Product column, how many values are equal to the outside table's Product? Now when I press Enter, it looks right: Carlota, and our two Quads. All the way down, there should be just four records with Quad.

Now we need to count how many rows there are. Before Table.SelectRows, I type Table. and check whether it is Table.RowCount or Table.Count. The IntelliSense helps, so I choose the row count function, open parentheses, and close parentheses at the end. Press Enter, and there is the expandable running count column.

The last step is to add a data type of whole number. Then we choose Home, Close and Load, closing Query2 onto the existing sheet, and click OK. This is much more difficult than our simple cell references and COUNTIFS, but sometimes you need to perform this same calculation using M code in Power Query. If you are new to Power Query or M code, check out my MSBI class, free on YouTube, videos 3 to 12, which teach you what you need to know about Power Query and M code.