A Power BI Technique Mined from the Power Pivot Archives
Below, you will find one of our all-time favorite Power BI techniques. In the ten (yes ten) years we’ve been operating this site, we’ve written over 1,000 articles on Power BI. Hard to imagine - even for us - but true.
Years ago, we first wrote up this technique in the context of Power Pivot – Power BI’s cousin and predecessor, which we like to call “Power BI in Excel.”
Since the two products share the same brains (DAX and M), this technique is more relevant today than ever. Everything below is 100% legit for Power BI – the same data model “shape,” the same DAX, etc. – but now you can use it to power up your Power BI visuals, and not just PivotTables. Enjoy!
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.
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)
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]=
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?
OK, now we slap a values filter on Rows so that we only see months with 3 or more negative stores…
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!
[SameStoreSales] and [Store Name] Back on the Pivot,
[Negative Growth Stores] Removed but Still Filtered!
I’ll extend this technique with some additional tricks next week.