Nice Pivot, But I Only Want to See Months Where Eight
or More of My Stores Went Negative!

### ***Update:  Technique Extended, Workbook available

In a followup post I have added a slicer that lets the report user control the minimum number of stores required, rather than fixing it at 8 like this post does.  Also, the workbook is now available for download.

Find both in the followup post, located here.

### Tales from Remote Consulting

Awhile back I left my job to start a new company.  I’m not yet ready to announce what that new company is about – I’m working hard on that and you folks will be the first to know.  Spoiler:  it’s about PowerPivot and Excel.

But in addition to hard work, there’s also a lot of waiting involved in all of that.  I’ve been filling the gaps with training and remote consulting to keep my head in the PowerPivot game.

Remote consulting in particular is a lot of fun – people send me a workbook, I spend 1-3 hours and build what they want, then send it back.  Gives me a good sampling of the problems that are “out there.”

One of those remote consulting jobs featured the problem pictured above (except that they had real data, and what I’m showing is 100% fake).

### How Many Stores Fell Below Zero Each Month?

“In the pivot above, I only want to show Months where at least eight stores were negative.”

That’s one of the problems I solved for a reader whose initials are PR

So how do we count how many stores went negative?

### COUNTX Says “I’m a Strange Function, Only Use Me With Blanks!”

The first time I figured out SUMX (aka the 5-point palm exploding function technique), I remember looking at COUNTX and thinking “how the HECK is THAT function ever going to be useful?”

I mean, SUMX basically says “go evaluate an expression a bunch of times and sum up the result.”  So COUNTX means “go evaluate an expression a bunch of times and then count how many times you did it???”

I mean, aren’t these two formulas going to return the EXACT SAME RESULT?

COUNTX(VALUES(Stores[StoreName]), any measure you choose)

COUNTROWS(VALUES(Stores[StoreName])

Remember, the X functions are “loops” – they run through every “row” in that first argument – VALUES(Stores[StoreName]) in this case – and evaluate the second argument, which is typically a measure expression.

So if you have 15 total stores, COUNTX is going to evaluate your measure expression 15 times.  Won’t COUNTX always return 15 then?

Yeah, except when your measure sometimes returns BLANK().  Blanks will NOT be counted.  And we can do sinister, amazing things with that.  Get your evil scientist laugh ready.

### Putting COUNTX to Work

Let’s write a new measure:

[Negative Growth Stores]=
COUNTX(VALUES(Stores[StoreID]),
IF([SameStoreSales]<0,1,BLANK())
)

This says “loop through Stores and count the number of times [SameStoreSales] was negative.

Let’s try that out on a pivot:

It’s Working, But Now How Do I Use It?

### Filtering!

OK, now we slap a values filter on Rows so that we only see months with 3 or more negative stores…

Yielding…

So Far So Good – Filtered to Just Months With 8 or More Negative Stores!

### Remove that Measure From Pivot, Add [SameStoreSales] Back

But I don’t want to see the number of negative stores, I just want to filter by it.  I want to see the [SameStoreSales] value for each store!

No problem:

[SameStoreSales] and [Store Name] Back on the Pivot,
[Negative Growth Stores] Removed but Still Filtered!

Neat huh?

I’ll extend this technique with some additional tricks next week.

#### Rob Collie

One of the founding engineers behind Power Pivot during his 14-year career at Microsoft, and creator of the world’s first cloud Power Pivot service, Rob is one of the foremost authorities on self-service business intelligence and next-generation spreadsheet technology.

## This Post Has 7 Comments

1. John says:

Looks nice, but I’m hoping the snapshot you’re showing isn’t the whole picture (width-wise) as the months shown on the bottom picture have fewer than 8 stores in the negative for some months. Also that formula and the picture are misleading (in my interpretation). Your description was to show (loop) the number of times same store was negative. I’m seeing 0% and positive values. So why is it showing everything? I ain’t no expert (yes that’s fancy speak) but that just don’t look right for what I think you should see. BUT.. I have been known to be wrong before. Just don’t tell my wife.

2. SF says: