**Nice Pivot, But I Only Want to See Months Where Eightor 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.

Very nice 🙂 how did you ever come up with this approach!

Necessity is the mother of invention 🙂

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.

Yeah the report is 33 columns wide – there are 33 stores total. So yes, many negative stores are missing from the screenshots. I really need to switch this blog to a widescreen format 🙂

The *measure* loops through the stores and counts them up. But the *filter* removes *months* that don’t have 8 stores negative. All stores are displayed no matter what. Does that clarify?

Do you have a workbook you can share for download?

Workbook is now available as part of the followup post:

http://powerpivotpro.com/2013/04/adding-a-minimum-threshold-slicer-to-stores-that-went-negative-technique/

For the calculation you provided you could also simply replace COUNTX() by SUMX()

[Negative Growth Stores]=

SUMX(VALUES(Stores[StoreID]),

IF([SameStoreSales]<0,1,BLANK())

)

thats how we optimize COUNT(FILTER()) in multidimensional models:

http://sqlblog.com/blogs/mosha/archive/2007/11/22/optimizing-count-filter-expressions-in-mdx.aspx

I am unable to use this workbook in my Excel 2013 as it says something about needing to upgrade. My presumption is that the workbook starts with the raw data and I can follow through with your post to achieve your results?

thanks