DAX COUNT and COUNTX - Impact of Using Measures and Columns


In a previous post we looked at DAX SUM and SUMX  expressions and you were introduced to a lot.  We mentioned table functions, iterators, row and filter context, measure, columns, and we also quickly introduced you to the RELATE function.

In this article we are going to slow things down a little and look at DAX COUNT and COUNTX – Impact of Using Measures and Columns.  Measures and columns work very different and this is a perfect example of how using measures and columns can impact the results in a table or visualization.

COUNT comes directly from Excel and will count the number of cells in a column that contain a number.  If a field contains a 0 it will be included in the count but if a field contains blank it will not be counted.  Text & true/false are not counted. So if you want to count text or logical functions you need to use COUNTA.

COUNTX on the other hand cannot be found in Excel. It works very like SUMX.  COUNTX takes two arguments, the first must return a table and the second must contain the column that is searched by COUNTX. Like COUNT, COUNTX counts numbers so if you want to count text or logical functions you need to use COUNTAX.

Let’s hop into an example and have further look at COUNT and COUNTX and the impact of creating a calculated column v’s a measure.

COUNT by Calculated Column


First we will add a calculated column to our products table, and we will enter the expression

=COUNT (products[Cost Price])

Now before we hit enter, think for a moment about what this formula will return? Will it return 1 in all of the rows because it’s looking at the cost price for that row and that row only; so can only count 1? Or will it return 12 because there are 12 cost prices in the table?  Maybe is it going to return something else?

DAX COUNT and COUNTX – Impact of Using Measures and Columns

The function returns 12. Although this is a calculated column (which defines the row), by using the count function, we are telling the DAX engine to count all of the fields in this column that have a number. The rows are assumed, or implied to be all the rows in the cost price column by virtue of the fact we used an Aggregation function

COUNT and COUNTX in DAX

COUNT by Measure


Let’s now create a measure using the same function

=COUNT (products[Cost Price])

Again we get 12 because the row context was defined by the fact COUNT is an aggregation function.

DAX SSAS

COUNT Measures and Columns in Pivot Tables


To look at this further let’s put this into a pivot table.

On the row labels we will drop in the products name. Then into the values field we will drop in our calculated column ‘count of cost price’.

count in dax

The first thing you will note is that the counts are not correct.  This is because in a calculated column the values are being aggregated by sum.  If we change this from sum to count then we get the correct value.

countx in dax and pivot tables

But who want to go through this extra step?  There is no real need for the calculated column. You could just drop the sales price into the value and change the aggregation from SUM to COUNT!

Next we will drop in our measure for the count of cost price. Immediately, without the need to change the aggregation, we have been given the correct value.



What We Learned So Far


What seems to be very evident from this is that if you use a calculated column the values are calculated within the column, row by row.  Then these values are aggregated based on the filter placed on the pivot table which is ‘products name’.

Whereas when you create a measure the values are not created in the table. The rows of the calculation are defined by the measure and by the filters placed on the pivot table.  It is also much easier to slice and dice a value created by a measure than a values created in a calculated column.

If you are coming from an Excel background, the logical thing to do would be produce an extra column in your data. But when you are using DAX, things are a little different.




COUNTX By Calculated Column


Now let’s look at COUNTX. Remember we said COUNTX is an iterator.  That means it will work its way through the table and evaluate an expression for each row and at the end aggregate the result.

What we will try to do next is count the number of a set of values in the products table filtered for just the product category Shoes.

First we will create a calculated column and we will enter the DAX function

=COUNTX (FILTER(products,products[Product Name]="Shoes"),products[Cost Price]) and we will name this column CCcountshoes.


What we can see is that for each row, the value of two is returned.  You see by using a calculated column, the row context is defined. So in the first row DAX is saying, okay let’s filter the product name just to give me shoes.  Then count the amount of product cost prices.  The answer is 2.  In the next row and the subsequent rows DAX does exactly the same thing.  It’s says to its self, lets filter the products table to only give us shoes and then count the cost price.  So for each row you will get the same.



COUNTX By Measure


So let’s now create a measure and we will use the same syntax that we use for the calculated column

=COUNTX (FILTER(products,products[Product Name]="Shoes"),products[Cost Price]) and we will name this measure Count Shoes.


The result we get is two and the calculation was carried out only once on the table. You see COUNTX is an iterator.  So what happens is DAX say to its self, lets filter the product name to just shoes.  Then using the tables that filter returns, lets then count the fields in the products cost price column that contains a value. So once the table is returned, COUNTX irritates over each row of the table and counts the values.

COUNTX Columns and Measures in Pivot Tables


Now let’s have a look at the difference in a pivot table of the use of both a calculated column and a calculated measure using an iterator.

We will use our products name in the rows and drop in the calculated column we just created to the value.  What we would expect to see as our expression filters the table to only shoe is a count for the shoes cost price. But instead first we get a sum aggregation, like we did with the count calculated column.


Let’s try changing the aggregation to COUNT.  We see we get 1 in Boots, but we don’t have any Boots that are Shoes.  So these values don’t really mean too much.

Let’s drop in our measure. Now we can see that we have a value for Shoes and no value under any of the other product names.


In this pivot table, with the measure, DAX says to its self, let’s go to the products table and the first item is Boots.  So our table is first filtered by Boots.  Then when it has this table it says to its self, okay in this table of Boot, find all the Shoes and count the cost price.  But there are no Shoes that are Boots and so there is no value to return. Do you get it!?

With the calculated column in the pivot table, the expression is first calculated in the table, row by row, where a count of 2 is returned. In the pivot table DAX is then told, to aggregate the column by the products name, well there is 1 product name Boots, and that does contain a value within the selected calculated column so that value is included.

DAX COUNT and COUNTX – Impact of Using Measures and Columns


Don’t worry if you are finding this a little complicated as we will cover it many times thought out the course.

What you need to understand now is that calculated columns and measure work different. This must be taken this into consideration when preparing your DAX calculations.

It is also important to understand the difference between aggregation functions and iterating functions. Aggregation functions such as SUM, COUNT and AVERAGE will take all the rows in the selection as implicated rows. Iteration functions will explicitly state the rows to be used.

We mentioned earlier that if you need to count text or logical functions you need to use COUNTA and COUNTAX and these count function are part of the DAX statistical functions.  There are also a number of other count functions that can be used in the mix and we will see more of them as we progress but for the moment just so you know.

DISTINCTCOUNT will count the distinct values in the selected data.

COUNTROWS will count the rows of data in a table.

COUNTBLANKS will count all the blank rows in a table.

Sign up for my newsletter – Don’t worry, I wont spam. Just useful Excel and Power BI tips and tricks to your inbox



Sign up now

In return for this Tip - DAX COUNT and COUNTX – Impact of Using Measures and Columns - I ask that you share this post with your friends and colleagues



Posted from my blog with SteemPress : http://theexcelclub.com/dax-count-and-countx-impact-of-using-measures-and-columns/

Sort:  

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 3 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 8 SBD worth and should receive 217 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

Congratulations @theexcelclub! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 100 as payout for your posts. Your next target is to reach a total payout of 250

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Christmas Challenge - Send a gift to to your friends

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @theexcelclub! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 300 upvotes. Your next target is to reach 400 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Christmas Challenge - The party continues
Christmas Challenge - Send a gift to to your friends

Support SteemitBoard's project! Vote for its witness and get one more award!

i'll have to go way back to read this blog. I like what i see

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.030
BTC 59715.05
ETH 3186.24
USDT 1.00
SBD 2.42